]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: resolvers: Use milliseconds for cached items in resolver responses
authorChristopher Faulet <cfaulet@haproxy.com>
Thu, 11 Mar 2021 08:36:05 +0000 (09:36 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 12 Mar 2021 16:41:28 +0000 (17:41 +0100)
The last time when an item was seen in a resolver responses is now stored in
milliseconds instead of seconds. This avoid some corner-cases at the
edges. This also simplifies time comparisons.

include/haproxy/resolvers-t.h
src/resolvers.c

index ba373707cf79794ea69974c9b4cccf33b135e74c..96874aabe216019abb28b8bcb6e5c72cf5f4197c 100644 (file)
@@ -113,7 +113,7 @@ struct resolv_answer_item {
        uint16_t        data_len;                    /* number of bytes in target below */
        struct sockaddr address;                     /* IPv4 or IPv6, network format */
        char            target[DNS_MAX_NAME_SIZE+1]; /* Response data: SRV or CNAME type target */
-       time_t          last_seen;                   /* When was the answer was last seen */
+       unsigned int    last_seen;                   /* When was the answer was last seen */
        struct resolv_answer_item *ar_item;          /* pointer to a RRset from the additional section, if exists */
        struct list     list;
 };
index 38c3588ca7112e508566b117d4771f3760ec7f84..8c09b7b09c644e8720d24609a8c7b9630af4ed38 100644 (file)
@@ -582,7 +582,7 @@ static void resolv_check_response(struct resolv_resolution *res)
                struct resolv_answer_item *ar_item = item->ar_item;
 
                /* clean up obsolete Additional record */
-               if (ar_item && (ar_item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
+               if (ar_item && tick_is_lt(tick_add(ar_item->last_seen, resolvers->hold.obsolete), now_ms)) {
                        /* Cleaning up the AR item will trigger an extra DNS  resolution, except if the SRV
                         * item is also obsolete.
                         */
@@ -591,7 +591,7 @@ static void resolv_check_response(struct resolv_resolution *res)
                }
 
                /* Remove obsolete items */
-               if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
+               if (tick_is_lt(tick_add(item->last_seen, resolvers->hold.obsolete), now_ms)) {
                        if (item->type != DNS_RTYPE_SRV)
                                goto rm_obselete_item;
 
@@ -901,6 +901,7 @@ static int resolv_validate_dns_response(unsigned char *resp, unsigned char *bufe
 
                /* initialization */
                answer_record->ar_item = NULL;
+               answer_record->last_seen = TICK_ETERNITY;
 
                offset = 0;
                len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
@@ -1096,12 +1097,12 @@ static int resolv_validate_dns_response(unsigned char *resp, unsigned char *bufe
                }
 
                if (found == 1) {
-                       tmp_record->last_seen = now.tv_sec;
+                       tmp_record->last_seen = now_ms;
                        pool_free(resolv_answer_item_pool, answer_record);
                        answer_record = NULL;
                }
                else {
-                       answer_record->last_seen = now.tv_sec;
+                       answer_record->last_seen = now_ms;
                        answer_record->ar_item = NULL;
                        LIST_ADDQ(&r_res->answer_list, &answer_record->list);
                        answer_record = NULL;
@@ -1151,6 +1152,7 @@ static int resolv_validate_dns_response(unsigned char *resp, unsigned char *bufe
                answer_record = pool_alloc(resolv_answer_item_pool);
                if (answer_record == NULL)
                        goto invalid_resp;
+               answer_record->last_seen = TICK_ETERNITY;
 
                offset = 0;
                len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0);
@@ -1277,12 +1279,12 @@ static int resolv_validate_dns_response(unsigned char *resp, unsigned char *bufe
                }
 
                if (found == 1) {
-                       tmp_record->ar_item->last_seen = now.tv_sec;
+                       tmp_record->ar_item->last_seen = now_ms;
                        pool_free(resolv_answer_item_pool, answer_record);
                        answer_record = NULL;
                }
                else {
-                       answer_record->last_seen = now.tv_sec;
+                       answer_record->last_seen = now_ms;
                        answer_record->ar_item = NULL;
 
                        // looking for the SRV record in the response list linked to this additional record