From ebcb5797417cd1ff96cb0eff2c3eb7bdb9ae0b78 Mon Sep 17 00:00:00 2001
From: Cody Cutrer <cody@instructure.com>
Date: Thu, 19 Sep 2013 13:31:37 -0600
Subject: [PATCH] add a separate config for controlling negative dns caching

---
 doc/config.txt    | 7 +++++++
 include/bouncer.h | 1 +
 src/dnslookup.c   | 3 ++-
 src/main.c        | 2 ++
 4 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/doc/config.txt b/doc/config.txt
index e8b4809..2b990d4 100644
--- a/doc/config.txt
+++ b/doc/config.txt
@@ -361,6 +361,13 @@ Actual DNS TTL is ignored.  [seconds]
 
 Default: 15.0
 
+==== dns_nxdomain_ttl ====
+
+How long error and NXDOMAIN DNS lookups can be cached. [seconds]
+
+Default: 15.0
+
+
 ==== dns_zone_check_period ====
 
 Period to check if zone serial has changed.
diff --git a/include/bouncer.h b/include/bouncer.h
index b3091d6..f0a840b 100644
--- a/include/bouncer.h
+++ b/include/bouncer.h
@@ -377,6 +377,7 @@ extern usec_t cf_idle_transaction_timeout;
 extern int cf_server_round_robin;
 extern int cf_disable_pqexec;
 extern usec_t cf_dns_max_ttl;
+extern usec_t cf_dns_nxdomain_ttl;
 extern usec_t cf_dns_zone_check_period;
 
 extern int cf_auth_type;
diff --git a/src/dnslookup.c b/src/dnslookup.c
index 403fe85..da0a431 100644
--- a/src/dnslookup.c
+++ b/src/dnslookup.c
@@ -1394,13 +1394,14 @@ static void got_result_gai(int result, struct addrinfo *res, void *arg)
 				ai = ai->ai_next;
 			}
 		}
+		req->res_ttl = get_cached_time() + cf_dns_max_ttl;
 	} else {
 		/* lookup failed */
 		log_warning("lookup failed: %s: result=%d", req->name, result);
+		req->res_ttl = get_cached_time() + cf_dns_nxdomain_ttl;
 	}
 
 	req->done = true;
-	req->res_ttl = get_cached_time() + cf_dns_max_ttl;
 
 	deliver_info(req);
 }
diff --git a/src/main.c b/src/main.c
index 1b83d6a..b4c5ae1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -99,6 +99,7 @@ usec_t cf_server_check_delay;
 int cf_server_round_robin;
 int cf_disable_pqexec;
 usec_t cf_dns_max_ttl;
+usec_t cf_dns_nxdomain_ttl;
 usec_t cf_dns_zone_check_period;
 unsigned int cf_max_packet_size;
 
@@ -207,6 +208,7 @@ CF_ABS("suspend_timeout", CF_TIME_USEC, cf_suspend_timeout, 0, "10"),
 CF_ABS("ignore_startup_parameters", CF_STR, cf_ignore_startup_params, 0, ""),
 CF_ABS("disable_pqexec", CF_INT, cf_disable_pqexec, CF_NO_RELOAD, "0"),
 CF_ABS("dns_max_ttl", CF_TIME_USEC, cf_dns_max_ttl, 0, "15"),
+CF_ABS("dns_nxdomain_ttl", CF_TIME_USEC, cf_dns_nxdomain_ttl, 0, "15"),
 CF_ABS("dns_zone_check_period", CF_TIME_USEC, cf_dns_zone_check_period, 0, "0"),
 
 CF_ABS("max_packet_size", CF_UINT, cf_max_packet_size, 0, "2147483647"),
-- 
2.40.0