cache.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright (C) 2015 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package discover
  7. import (
  8. stdsync "sync"
  9. "time"
  10. "github.com/syncthing/protocol"
  11. "github.com/syncthing/syncthing/lib/sync"
  12. "github.com/thejerf/suture"
  13. )
  14. // The CachingMux aggregates results from multiple Finders. Each Finder has
  15. // an associated cache time and negative cache time. The cache time sets how
  16. // long we cache and return successfull lookup results, the negative cache
  17. // time sets how long we refrain from asking about the same device ID after
  18. // receiving a negative answer. The value of zero disables caching (positive
  19. // or negative).
  20. type CachingMux struct {
  21. *suture.Supervisor
  22. finders []cachedFinder
  23. caches []*cache
  24. mut sync.Mutex
  25. }
  26. // A cachedFinder is a Finder with associated cache timeouts.
  27. type cachedFinder struct {
  28. Finder
  29. cacheTime time.Duration
  30. negCacheTime time.Duration
  31. }
  32. func NewCachingMux() *CachingMux {
  33. return &CachingMux{
  34. Supervisor: suture.NewSimple("discover.cachingMux"),
  35. mut: sync.NewMutex(),
  36. }
  37. }
  38. // Add registers a new Finder, with associated cache timeouts.
  39. func (m *CachingMux) Add(finder Finder, cacheTime, negCacheTime time.Duration) {
  40. m.mut.Lock()
  41. m.finders = append(m.finders, cachedFinder{finder, cacheTime, negCacheTime})
  42. m.caches = append(m.caches, newCache())
  43. m.mut.Unlock()
  44. if svc, ok := finder.(suture.Service); ok {
  45. m.Supervisor.Add(svc)
  46. }
  47. }
  48. // Lookup attempts to resolve the device ID using any of the added Finders,
  49. // while obeying the cache settings.
  50. func (m *CachingMux) Lookup(deviceID protocol.DeviceID) (direct []string, relays []Relay, err error) {
  51. m.mut.Lock()
  52. for i, finder := range m.finders {
  53. if cacheEntry, ok := m.caches[i].Get(deviceID); ok {
  54. // We have a cache entry. Lets see what it says.
  55. if cacheEntry.found && time.Since(cacheEntry.when) < finder.cacheTime {
  56. // It's a positive, valid entry. Use it.
  57. if debug {
  58. l.Debugln("cached discovery entry for", deviceID, "at", finder.String())
  59. l.Debugln(" ", cacheEntry)
  60. }
  61. direct = append(direct, cacheEntry.Direct...)
  62. relays = append(relays, cacheEntry.Relays...)
  63. continue
  64. }
  65. if !cacheEntry.found && time.Since(cacheEntry.when) < finder.negCacheTime {
  66. // It's a negative, valid entry. We should not make another
  67. // attempt right now.
  68. if debug {
  69. l.Debugln("negative cache entry for", deviceID, "at", finder.String())
  70. }
  71. continue
  72. }
  73. // It's expired. Ignore and continue.
  74. }
  75. // Perform the actual lookup and cache the result.
  76. if td, tr, err := finder.Lookup(deviceID); err == nil {
  77. if debug {
  78. l.Debugln("lookup for", deviceID, "at", finder.String())
  79. l.Debugln(" ", td)
  80. l.Debugln(" ", tr)
  81. }
  82. direct = append(direct, td...)
  83. relays = append(relays, tr...)
  84. m.caches[i].Set(deviceID, CacheEntry{
  85. Direct: td,
  86. Relays: tr,
  87. when: time.Now(),
  88. found: len(td)+len(tr) > 0,
  89. })
  90. }
  91. }
  92. m.mut.Unlock()
  93. if debug {
  94. l.Debugln("lookup results for", deviceID)
  95. l.Debugln(" ", direct)
  96. l.Debugln(" ", relays)
  97. }
  98. return direct, relays, nil
  99. }
  100. func (m *CachingMux) String() string {
  101. return "discovery cache"
  102. }
  103. func (m *CachingMux) Error() error {
  104. return nil
  105. }
  106. func (m *CachingMux) ChildErrors() map[string]error {
  107. m.mut.Lock()
  108. children := make(map[string]error, len(m.finders))
  109. for _, f := range m.finders {
  110. children[f.String()] = f.Error()
  111. }
  112. m.mut.Unlock()
  113. return children
  114. }
  115. func (m *CachingMux) Cache() map[protocol.DeviceID]CacheEntry {
  116. // Res will be the "total" cache, i.e. the union of our cache and all our
  117. // children's caches.
  118. res := make(map[protocol.DeviceID]CacheEntry)
  119. m.mut.Lock()
  120. for i := range m.finders {
  121. // Each finder[i] has a corresponding cache at cache[i]. Go through it
  122. // and populate the total, if it's newer than what's already in there.
  123. // We skip any negative cache entries.
  124. for k, v := range m.caches[i].Cache() {
  125. if v.found && v.when.After(res[k].when) {
  126. res[k] = v
  127. }
  128. }
  129. // Then ask the finder itself for it's cache and do the same. If this
  130. // finder is a global discovery client, it will have no cache. If it's
  131. // a local discovery client, this will be it's current state.
  132. for k, v := range m.finders[i].Cache() {
  133. if v.found && v.when.After(res[k].when) {
  134. res[k] = v
  135. }
  136. }
  137. }
  138. m.mut.Unlock()
  139. return res
  140. }
  141. // A cache can be embedded wherever useful
  142. type cache struct {
  143. entries map[protocol.DeviceID]CacheEntry
  144. mut stdsync.Mutex
  145. }
  146. func newCache() *cache {
  147. return &cache{
  148. entries: make(map[protocol.DeviceID]CacheEntry),
  149. }
  150. }
  151. func (c *cache) Set(id protocol.DeviceID, ce CacheEntry) {
  152. c.mut.Lock()
  153. c.entries[id] = ce
  154. c.mut.Unlock()
  155. }
  156. func (c *cache) Get(id protocol.DeviceID) (CacheEntry, bool) {
  157. c.mut.Lock()
  158. ce, ok := c.entries[id]
  159. c.mut.Unlock()
  160. return ce, ok
  161. }
  162. func (c *cache) Cache() map[protocol.DeviceID]CacheEntry {
  163. c.mut.Lock()
  164. m := make(map[protocol.DeviceID]CacheEntry, len(c.entries))
  165. for k, v := range c.entries {
  166. m[k] = v
  167. }
  168. c.mut.Unlock()
  169. return m
  170. }