]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: action: do-resolve now use cached response
authorBaptiste Assmann <bedis9@gmail.com>
Wed, 30 Oct 2019 15:06:53 +0000 (16:06 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 7 Nov 2019 17:46:55 +0000 (18:46 +0100)
As reported by David Birdsong on the ML, the HTTP action do-resolve does
not use the DNS cache.
Actually, the action is "registred" to the resolution for said name to
be resolved and wait until an other requester triggers the it. Once the
resolution is finished, then the action is updated with the result.
To trigger this, you must have a server with runtime DNS resolution
enabled and run a do-resolve action with the same fqdn AND they use the
same resolvers section.

This patch fixes this behavior by ensuring the resolution associated to
the action has a valid answer which is not considered as expired. If
those conditions are valid, then we can use it (it's the "cache").

Backport status: 2.0

src/dns.c

index 15d40a13a2142134cedd862d4a096874a3fe45cf..00012896065f16548f7e502a077e23f1651766fb 100644 (file)
--- a/src/dns.c
+++ b/src/dns.c
@@ -2150,8 +2150,13 @@ enum act_return dns_action_do_resolve(struct act_rule *rule, struct proxy *px,
        struct dns_resolution *resolution;
        struct sample *smp;
        char *fqdn;
+       struct dns_requester *req;
+       struct dns_resolvers  *resolvers;
+       struct dns_resolution *res;
+       int exp;
 
        /* we have a response to our DNS resolution */
+ use_cache:
        if (s->dns_ctx.dns_requester && s->dns_ctx.dns_requester->resolution != NULL) {
                resolution = s->dns_ctx.dns_requester->resolution;
                if (resolution->step == RSLV_STEP_RUNNING) {
@@ -2211,6 +2216,22 @@ enum act_return dns_action_do_resolve(struct act_rule *rule, struct proxy *px,
 
        s->dns_ctx.parent = rule;
        dns_link_resolution(s, OBJ_TYPE_STREAM, 0);
+
+       /* Check if there is a fresh enough response in the cache of our associated resolution */
+       req = s->dns_ctx.dns_requester;
+       if (!req || !req->resolution) {
+               dns_trigger_resolution(s->dns_ctx.dns_requester);
+               return ACT_RET_YIELD;
+       }
+       res       = req->resolution;
+       resolvers = res->resolvers;
+
+       exp = tick_add(res->last_resolution, resolvers->hold.valid);
+       if (resolvers->t && res->status == RSLV_STATUS_VALID && tick_isset(res->last_resolution)
+                      && !tick_is_expired(exp, now_ms)) {
+               goto use_cache;
+       }
+
        dns_trigger_resolution(s->dns_ctx.dns_requester);
        return ACT_RET_YIELD;
 }