DNSCache manages DNS data that will expire after a certain TTL. Information is tracked per-IP address, retaining the latest-expiring DNS data for each address. For most real-world DNS data, the entry per name remains small because newer lookups replace older ones. Large TTLs may cause entries to gro
| 105 | // called. |
| 106 | // Redundant entries are removed on insert. |
| 107 | type DNSCache struct { |
| 108 | mu lock.RWMutex |
| 109 | |
| 110 | // forward DNS lookups name -> IPEntries |
| 111 | // IPEntries maps IP -> entry that provides it. An entry may provide multiple IPs. |
| 112 | forward map[string]ipEntries |
| 113 | |
| 114 | // IP->dnsNames lookup |
| 115 | // This map is subordinate to forward, above. An IP inserted into forward, or |
| 116 | // expired in forward, should also be added/removed in reverse. |
| 117 | reverse map[netip.Addr]nameEntries |
| 118 | |
| 119 | // LastCleanup is the latest time for which entries have been expired. It is |
| 120 | // used as "now" when doing lookups and advanced by calls to .GC |
| 121 | // When an entry is added with an expiration time before lastCleanup, it is |
| 122 | // set to that value. |
| 123 | lastCleanup time.Time |
| 124 | |
| 125 | // cleanup maps the TTL expiration times (in seconds since the epoch) to |
| 126 | // DNS names that expire in that second. On every new insertion where the |
| 127 | // new data is actually inserted into the cache (i.e. it expires later than |
| 128 | // an existing entry) cleanup will be updated. CleanupExpiredEntries cleans |
| 129 | // up these entries on demand. |
| 130 | // Note: Lookup functions will not return expired entries, and this is used |
| 131 | // to proactively enforce expirations. |
| 132 | // Note: It is important to periodically call CleanupExpiredEntries |
| 133 | // otherwise this map will grow forever. |
| 134 | cleanup map[int64][]string |
| 135 | |
| 136 | // overLimit is a set of DNS names that were over the per-host configured |
| 137 | // limit when they received an update. The excess IPs will be removed when |
| 138 | // cleanupOverLimitEntries is called, but will continue to be returned by |
| 139 | // Lookup until then. |
| 140 | // Note: It is important to periodically call GC otherwise this map will |
| 141 | // grow forever (it is very bounded, however). |
| 142 | overLimit map[string]bool |
| 143 | |
| 144 | // perHostLimit is the number of maximum number of IP per host. |
| 145 | perHostLimit int |
| 146 | |
| 147 | // minTTL is the minimum TTL value that a cache entry can have, if the TTL |
| 148 | // sent in the Update is lower, the TTL will be overwritten to this value. |
| 149 | // Due is only read-only is not protected by the mutex. |
| 150 | minTTL int |
| 151 | |
| 152 | // updated is a set tracking the other DNSCaches that have contributed to the given DNSCache |
| 153 | // since last GC round. This is used during GC to ensure no per-endpoint changes are lost. |
| 154 | updated sets.Set[*DNSCache] |
| 155 | } |
| 156 | |
| 157 | // NewDNSCache returns an initialized DNSCache |
| 158 | func NewDNSCache(minTTL int) *DNSCache { |
nothing calls this directly
no outgoing calls
no test coverage detected