dnscache.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package dnscache
  2. import (
  3. "context"
  4. "net"
  5. "net/http/httptrace"
  6. "sync"
  7. "time"
  8. "golang.org/x/sync/singleflight"
  9. )
  10. type DNSResolver interface {
  11. LookupHost(ctx context.Context, host string) (addrs []string, err error)
  12. LookupAddr(ctx context.Context, addr string) (names []string, err error)
  13. }
  14. type Resolver struct {
  15. // Timeout defines the maximum allowed time allowed for a lookup.
  16. Timeout time.Duration
  17. // Resolver is used to perform actual DNS lookup. If nil,
  18. // net.DefaultResolver is used instead.
  19. Resolver DNSResolver
  20. once sync.Once
  21. mu sync.RWMutex
  22. cache map[string]*cacheEntry
  23. // OnCacheMiss is executed if the host or address is not included in
  24. // the cache and the default lookup is executed.
  25. OnCacheMiss func()
  26. }
  27. type ResolverRefreshOptions struct {
  28. ClearUnused bool
  29. PersistOnFailure bool
  30. }
  31. type cacheEntry struct {
  32. rrs []string
  33. err error
  34. used bool
  35. }
  36. // LookupAddr performs a reverse lookup for the given address, returning a list
  37. // of names mapping to that address.
  38. func (r *Resolver) LookupAddr(ctx context.Context, addr string) (names []string, err error) {
  39. r.once.Do(r.init)
  40. return r.lookup(ctx, "r"+addr)
  41. }
  42. // LookupHost looks up the given host using the local resolver. It returns a
  43. // slice of that host's addresses.
  44. func (r *Resolver) LookupHost(ctx context.Context, host string) (addrs []string, err error) {
  45. r.once.Do(r.init)
  46. return r.lookup(ctx, "h"+host)
  47. }
  48. // refreshRecords refreshes cached entries which have been used at least once since
  49. // the last Refresh. If clearUnused is true, entries which haven't be used since the
  50. // last Refresh are removed from the cache. If persistOnFailure is true, stale
  51. // entries will not be removed on failed lookups
  52. func (r *Resolver) refreshRecords(clearUnused bool, persistOnFailure bool) {
  53. r.once.Do(r.init)
  54. r.mu.RLock()
  55. update := make([]string, 0, len(r.cache))
  56. del := make([]string, 0, len(r.cache))
  57. for key, entry := range r.cache {
  58. if entry.used {
  59. update = append(update, key)
  60. } else if clearUnused {
  61. del = append(del, key)
  62. }
  63. }
  64. r.mu.RUnlock()
  65. if len(del) > 0 {
  66. r.mu.Lock()
  67. for _, key := range del {
  68. delete(r.cache, key)
  69. }
  70. r.mu.Unlock()
  71. }
  72. for _, key := range update {
  73. r.update(context.Background(), key, false, persistOnFailure)
  74. }
  75. }
  76. func (r *Resolver) Refresh(clearUnused bool) {
  77. r.refreshRecords(clearUnused, false)
  78. }
  79. func (r *Resolver) RefreshWithOptions(options ResolverRefreshOptions) {
  80. r.refreshRecords(options.ClearUnused, options.PersistOnFailure)
  81. }
  82. func (r *Resolver) init() {
  83. r.cache = make(map[string]*cacheEntry)
  84. }
  85. // lookupGroup merges lookup calls together for lookups for the same host. The
  86. // lookupGroup key is is the LookupIPAddr.host argument.
  87. var lookupGroup singleflight.Group
  88. func (r *Resolver) lookup(ctx context.Context, key string) (rrs []string, err error) {
  89. var found bool
  90. rrs, err, found = r.load(key)
  91. if !found {
  92. if r.OnCacheMiss != nil {
  93. r.OnCacheMiss()
  94. }
  95. rrs, err = r.update(ctx, key, true, false)
  96. }
  97. return
  98. }
  99. func (r *Resolver) update(ctx context.Context, key string, used bool, persistOnFailure bool) (rrs []string, err error) {
  100. c := lookupGroup.DoChan(key, r.lookupFunc(ctx, key))
  101. select {
  102. case <-ctx.Done():
  103. err = ctx.Err()
  104. if err == context.DeadlineExceeded {
  105. // If DNS request timed out for some reason, force future
  106. // request to start the DNS lookup again rather than waiting
  107. // for the current lookup to complete.
  108. lookupGroup.Forget(key)
  109. }
  110. case res := <-c:
  111. if res.Shared {
  112. // We had concurrent lookups, check if the cache is already updated
  113. // by a friend.
  114. var found bool
  115. rrs, err, found = r.load(key)
  116. if found {
  117. return
  118. }
  119. }
  120. err = res.Err
  121. if err == nil {
  122. rrs, _ = res.Val.([]string)
  123. }
  124. if err != nil && persistOnFailure {
  125. var found bool
  126. rrs, err, found = r.load(key)
  127. if found {
  128. return
  129. }
  130. }
  131. r.mu.Lock()
  132. r.storeLocked(key, rrs, used, err)
  133. r.mu.Unlock()
  134. }
  135. return
  136. }
  137. // lookupFunc returns lookup function for key. The type of the key is stored as
  138. // the first char and the lookup subject is the rest of the key.
  139. func (r *Resolver) lookupFunc(ctx context.Context, key string) func() (interface{}, error) {
  140. if len(key) == 0 {
  141. panic("lookupFunc with empty key")
  142. }
  143. var resolver DNSResolver = defaultResolver
  144. if r.Resolver != nil {
  145. resolver = r.Resolver
  146. }
  147. switch key[0] {
  148. case 'h':
  149. return func() (interface{}, error) {
  150. ctx, cancel := r.prepareCtx(ctx)
  151. defer cancel()
  152. return resolver.LookupHost(ctx, key[1:])
  153. }
  154. case 'r':
  155. return func() (interface{}, error) {
  156. ctx, cancel := r.prepareCtx(ctx)
  157. defer cancel()
  158. return resolver.LookupAddr(ctx, key[1:])
  159. }
  160. default:
  161. panic("lookupFunc invalid key type: " + key)
  162. }
  163. }
  164. func (r *Resolver) prepareCtx(origContext context.Context) (ctx context.Context, cancel context.CancelFunc) {
  165. ctx = context.Background()
  166. if r.Timeout > 0 {
  167. ctx, cancel = context.WithTimeout(ctx, r.Timeout)
  168. } else {
  169. cancel = func() {}
  170. }
  171. // If a httptrace has been attached to the given context it will be copied over to the newly created context. We only need to copy pointers
  172. // to DNSStart and DNSDone hooks
  173. if trace := httptrace.ContextClientTrace(origContext); trace != nil {
  174. derivedTrace := &httptrace.ClientTrace{
  175. DNSStart: trace.DNSStart,
  176. DNSDone: trace.DNSDone,
  177. }
  178. ctx = httptrace.WithClientTrace(ctx, derivedTrace)
  179. }
  180. return
  181. }
  182. func (r *Resolver) load(key string) (rrs []string, err error, found bool) {
  183. r.mu.RLock()
  184. var entry *cacheEntry
  185. entry, found = r.cache[key]
  186. if !found {
  187. r.mu.RUnlock()
  188. return
  189. }
  190. rrs = entry.rrs
  191. err = entry.err
  192. used := entry.used
  193. r.mu.RUnlock()
  194. if !used {
  195. r.mu.Lock()
  196. entry.used = true
  197. r.mu.Unlock()
  198. }
  199. return rrs, err, true
  200. }
  201. func (r *Resolver) storeLocked(key string, rrs []string, used bool, err error) {
  202. if entry, found := r.cache[key]; found {
  203. // Update existing entry in place
  204. entry.rrs = rrs
  205. entry.err = err
  206. entry.used = used
  207. return
  208. }
  209. r.cache[key] = &cacheEntry{
  210. rrs: rrs,
  211. err: err,
  212. used: used,
  213. }
  214. }
  215. var defaultResolver = &defaultResolverWithTrace{}
  216. // defaultResolverWithTrace calls `LookupIP` instead of `LookupHost` on `net.DefaultResolver` in order to cause invocation of the `DNSStart`
  217. // and `DNSDone` hooks. By implementing `DNSResolver`, backward compatibility can be ensured.
  218. type defaultResolverWithTrace struct{}
  219. func (d *defaultResolverWithTrace) LookupHost(ctx context.Context, host string) (addrs []string, err error) {
  220. // `net.Resolver#LookupHost` does not cause invocation of `net.Resolver#lookupIPAddr`, therefore the `DNSStart` and `DNSDone` tracing hooks
  221. // built into the stdlib are never called. `LookupIP`, despite it's name, can also be used to lookup a hostname but does cause these hooks to be
  222. // triggered. The format of the reponse is different, therefore it needs this thin wrapper converting it.
  223. rawIPs, err := net.DefaultResolver.LookupIP(ctx, "ip", host)
  224. if err != nil {
  225. return nil, err
  226. }
  227. cookedIPs := make([]string, len(rawIPs))
  228. for i, v := range rawIPs {
  229. cookedIPs[i] = v.String()
  230. }
  231. return cookedIPs, nil
  232. }
  233. func (d *defaultResolverWithTrace) LookupAddr(ctx context.Context, addr string) (names []string, err error) {
  234. return net.DefaultResolver.LookupAddr(ctx, addr)
  235. }