Eric van Gyzen
2015-10-07 21:02:39 UTC
I would like to change the libc resolver to automatically reload
/etc/resolv.conf when the latter changes. I would like to hear opinions
about the implementation. Broadly, I see two approaches.
== "stat" ==
When loading the file, record the mod time. Before each query, stat()
the file to see if it has changed.
Advantage: It uses no extra persistently allocated objects.
Disadvantage: It incurs a stat() on every query. I don't see this as a
major disadvantage, since the resolver already does a lot of work on
every query. (For example, it creates and destroys a kqueue and a socket.)
OpenBSD uses this approach. It also uses clock_gettime(CLOCK_MONOTONIC)
to rate-limit the stat() calls to one per several seconds.
== "kqueue" ==
When loading the file, open a kqueue and register for the appropriate
events. Before each query, check for kevents.
Advantage: The per-query overhead is fairly small.
Disadvantage: This would persistently allocate an open file and a
kqueue for every thread that ever uses the resolver, for the life of the
thread. This seems fairly expensive.
NetBSD uses this approach. It mitigates most of the space-cost by using
a shared pool of res_state objects, instead of one per thread [that uses
the resolver]. On each query, a thread allocates/borrows a res_state
from the pool, uses it, and returns it. So, the number of objects is
only the high water mark of the number of threads _concurrently_ issuing
resolver queries.
There are probably several variations on each theme, of course. I would
appreciate your thoughts on these approaches and others I missed, as
well as variations and details.
FYI, I'm leaning toward the "stat" approach.
Cheers,
Eric
/etc/resolv.conf when the latter changes. I would like to hear opinions
about the implementation. Broadly, I see two approaches.
== "stat" ==
When loading the file, record the mod time. Before each query, stat()
the file to see if it has changed.
Advantage: It uses no extra persistently allocated objects.
Disadvantage: It incurs a stat() on every query. I don't see this as a
major disadvantage, since the resolver already does a lot of work on
every query. (For example, it creates and destroys a kqueue and a socket.)
OpenBSD uses this approach. It also uses clock_gettime(CLOCK_MONOTONIC)
to rate-limit the stat() calls to one per several seconds.
== "kqueue" ==
When loading the file, open a kqueue and register for the appropriate
events. Before each query, check for kevents.
Advantage: The per-query overhead is fairly small.
Disadvantage: This would persistently allocate an open file and a
kqueue for every thread that ever uses the resolver, for the life of the
thread. This seems fairly expensive.
NetBSD uses this approach. It mitigates most of the space-cost by using
a shared pool of res_state objects, instead of one per thread [that uses
the resolver]. On each query, a thread allocates/borrows a res_state
from the pool, uses it, and returns it. So, the number of objects is
only the high water mark of the number of threads _concurrently_ issuing
resolver queries.
There are probably several variations on each theme, of course. I would
appreciate your thoughts on these approaches and others I missed, as
well as variations and details.
FYI, I'm leaning toward the "stat" approach.
Cheers,
Eric