Expand description
DNS resolution and cache.
DNS cache in Delta Chat has two layers:
in-memory cache and persistent dns_cache
SQL table.
In-memory cache is using a βstale-while-revalidateβ strategy.
If there is a cached value, it is returned immediately
and revalidation task is started in the background
to replace old cached IP addresses with new ones.
If there is no cached value yet,
lookup only finishes when lookup_host
returns first results.
In-memory cache is shared between all accounts
and is never stored on the disk.
It can be thought of as an extension
of the system resolver.
Persistent dns_cache
SQL table is used to collect
all IP addresses ever seen for the hostname
together with the timestamp
of the last time IP address has been seen.
Note that this timestamp reflects the time
IP address was returned by the in-memory cache
rather than the underlying system resolver.
Unused entries are removed after 30 days
(CACHE_TTL
constant) to avoid having
old non-working IP addresses in the cache indefinitely.
When Delta Chat needs an IP address for the host, it queries in-memory cache for the next result and merges the list of IP addresses with the list of IP addresses from persistent cache. Resulting list is constructed by taking the first two results from the resolver followed up by persistent cache results and terminated by the rest of resolver results.
Persistent cache results are sorted by the time of the most recent successful connection using the result. For results that have never been used for successful connection timestamp of retrieving them from in-memory cache is used.
Statics§
- DNS_
PRELOAD πPreloaded DNS results that can be used in case of DNS server failures. - LOOKUP_
HOST_ πCACHE Map from hostname to IP addresses.
Functions§
- lookup_
cache π - Looks up the hostname and updates persistent DNS cache on success.
- Looks up hostname and port using DNS and updates the address resolution cache.
- lookup_
ips πWrapper forlookup_host
that returns IP addresses. - merge_
with_ πcache Merges results received from DNS with cached results. - prune_
dns_ πcache - Sorts DNS resolution results by connection timestamp in descending order so IP addresses that we recently connected to successfully are tried first.
- update_
cache πInserts entry into DNS cache or updates existing one with a new timestamp.