mirror of
https://github.com/syncthing/syncthing.git
synced 2026-11-20 00:51:22 -08:00
refactor(discover): simplify cache handling by moving to the cachedFinder (#10956)
Where it should have been to begin with --------- Signed-off-by: Jakob Borg <[email protected]>
This commit is contained in:
1 parent
b0df1033fe
commit
f2ed52347e
3 files changed
+60
-57
No files matched your search
@@ -278,7 +278,8 @@ jobs:
|
||||
|
||||
package-linux:
|
||||
name: Package for Linux (${{ matrix.group }})
|
||||
runs-on: ubuntu-latest
|
||||
# Ubuntu 26.04 no longer ships the MIPS64 GCC cross-compilers.
|
||||
runs-on: ${{ matrix.group == 'mips' && 'ubuntu-24.04' || 'ubuntu-latest' }}
|
||||
needs:
|
||||
- facts
|
||||
strategy:
|
||||
@@ -330,7 +331,9 @@ jobs:
|
||||
|
||||
- name: Install extra packages
|
||||
if: matrix.extra-packages != ''
|
||||
run: sudo apt-get install -y ${{ matrix.extra-packages }}
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y ${{ matrix.extra-packages }}
|
||||
|
||||
- name: Create packages
|
||||
run: |
|
||||
@@ -735,7 +738,7 @@ jobs:
|
||||
go-version: ${{ needs.facts.outputs.go-version }}
|
||||
cache: false
|
||||
|
||||
- uses: ruby/setup-ruby@eab2afb99481ca09a4e91171a8e0aee0e89bfedd # v1
|
||||
- uses: ruby/setup-ruby@f9c4492e86def3e644ab32a0ef185eda5b0c20d0 # v1.342.0
|
||||
with:
|
||||
ruby-version: '3.0'
|
||||
|
||||
|
||||
+45
-2
@@ -7,12 +7,13 @@
|
||||
package discover
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"maps"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/protocol"
|
||||
"github.com/thejerf/suture/v4"
|
||||
)
|
||||
|
||||
// A cachedFinder is a Finder with associated cache timeouts.
|
||||
@@ -22,7 +23,6 @@ type cachedFinder struct {
|
||||
cacheTime time.Duration
|
||||
negCacheTime time.Duration
|
||||
cache *cache
|
||||
token *suture.ServiceToken
|
||||
}
|
||||
|
||||
// An error may implement cachedError, in which case it will be interrogated
|
||||
@@ -32,6 +32,49 @@ type cachedError interface {
|
||||
CacheFor() time.Duration
|
||||
}
|
||||
|
||||
func (f *cachedFinder) Lookup(ctx context.Context, deviceID protocol.DeviceID) ([]string, error) {
|
||||
if cacheEntry, ok := f.cache.Get(deviceID); ok {
|
||||
// We have a cache entry. Lets see what it says.
|
||||
|
||||
if cacheEntry.found && time.Since(cacheEntry.when) < f.cacheTime {
|
||||
// It's a positive, valid entry. Use it.
|
||||
slog.DebugContext(ctx, "Found cached discovery entry", "device", deviceID, "finder", f, "entry", cacheEntry)
|
||||
return cacheEntry.Addresses, nil
|
||||
}
|
||||
|
||||
valid := time.Now().Before(cacheEntry.validUntil) || time.Since(cacheEntry.when) < f.negCacheTime
|
||||
if !cacheEntry.found && valid {
|
||||
// It's a negative, valid entry. We should not make another
|
||||
// attempt right now.
|
||||
slog.DebugContext(ctx, "Negative cache entry", "device", deviceID, "finder", f, "until1", cacheEntry.when.Add(f.negCacheTime), "until2", cacheEntry.validUntil)
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Perform the actual lookup and cache the result.
|
||||
addrs, err := f.Finder.Lookup(ctx, deviceID)
|
||||
if err == nil {
|
||||
slog.DebugContext(ctx, "Got finder result", "device", deviceID, "finder", f, "address", addrs)
|
||||
f.cache.Set(deviceID, CacheEntry{
|
||||
Addresses: addrs,
|
||||
when: time.Now(),
|
||||
found: len(addrs) > 0,
|
||||
})
|
||||
} else {
|
||||
// Lookup returned error, add a negative cache entry.
|
||||
entry := CacheEntry{
|
||||
when: time.Now(),
|
||||
found: false,
|
||||
}
|
||||
if err, ok := err.(cachedError); ok {
|
||||
entry.validUntil = time.Now().Add(err.CacheFor())
|
||||
}
|
||||
f.cache.Set(deviceID, entry)
|
||||
}
|
||||
|
||||
return addrs, err
|
||||
}
|
||||
|
||||
// A cache can be embedded wherever useful
|
||||
|
||||
type cache struct {
|
||||
|
||||
+9
-52
@@ -49,8 +49,9 @@ type manager struct {
|
||||
addressLister AddressLister
|
||||
registry *registry.Registry
|
||||
|
||||
finders map[string]cachedFinder
|
||||
mut sync.RWMutex
|
||||
finders map[string]cachedFinder
|
||||
tokens map[string]suture.ServiceToken
|
||||
}
|
||||
|
||||
func NewManager(myID protocol.DeviceID, cfg config.Wrapper, cert tls.Certificate, evLogger events.Logger, lister AddressLister, registry *registry.Registry) Manager {
|
||||
@@ -62,8 +63,8 @@ func NewManager(myID protocol.DeviceID, cfg config.Wrapper, cert tls.Certificate
|
||||
evLogger: evLogger,
|
||||
addressLister: lister,
|
||||
registry: registry,
|
||||
|
||||
finders: make(map[string]cachedFinder),
|
||||
finders: make(map[string]cachedFinder),
|
||||
tokens: make(map[string]suture.ServiceToken),
|
||||
}
|
||||
m.Add(svcutil.AsService(m.serve, m.String()))
|
||||
return m
|
||||
@@ -78,33 +79,27 @@ func (m *manager) serve(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (m *manager) addLocked(identity string, finder Finder, cacheTime, negCacheTime time.Duration) {
|
||||
entry := cachedFinder{
|
||||
m.finders[identity] = cachedFinder{
|
||||
Finder: finder,
|
||||
cacheTime: cacheTime,
|
||||
negCacheTime: negCacheTime,
|
||||
cache: newCache(),
|
||||
token: nil,
|
||||
}
|
||||
if service, ok := finder.(suture.Service); ok {
|
||||
token := m.Supervisor.Add(service)
|
||||
entry.token = &token
|
||||
m.tokens[identity] = token
|
||||
}
|
||||
m.finders[identity] = entry
|
||||
slog.Info("Using discovery mechanism", "identity", identity)
|
||||
}
|
||||
|
||||
func (m *manager) removeLocked(identity string) {
|
||||
entry, ok := m.finders[identity]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if entry.token != nil {
|
||||
err := m.Supervisor.Remove(*entry.token)
|
||||
if err != nil {
|
||||
if token, ok := m.tokens[identity]; ok {
|
||||
if err := m.Supervisor.Remove(token); err != nil {
|
||||
slog.Warn("Failed to remove discovery mechanism", slog.String("identity", identity), slogutil.Error(err))
|
||||
}
|
||||
}
|
||||
delete(m.finders, identity)
|
||||
delete(m.tokens, identity)
|
||||
slog.Info("Stopped using discovery mechanism", "identity", identity)
|
||||
}
|
||||
|
||||
@@ -113,46 +108,8 @@ func (m *manager) removeLocked(identity string) {
|
||||
func (m *manager) Lookup(ctx context.Context, deviceID protocol.DeviceID) (addresses []string, err error) {
|
||||
m.mut.RLock()
|
||||
for _, finder := range m.finders {
|
||||
if cacheEntry, ok := finder.cache.Get(deviceID); ok {
|
||||
// We have a cache entry. Lets see what it says.
|
||||
|
||||
if cacheEntry.found && time.Since(cacheEntry.when) < finder.cacheTime {
|
||||
// It's a positive, valid entry. Use it.
|
||||
slog.DebugContext(ctx, "Found cached discovery entry", "device", deviceID, "finder", finder, "entry", cacheEntry)
|
||||
addresses = append(addresses, cacheEntry.Addresses...)
|
||||
continue
|
||||
}
|
||||
|
||||
valid := time.Now().Before(cacheEntry.validUntil) || time.Since(cacheEntry.when) < finder.negCacheTime
|
||||
if !cacheEntry.found && valid {
|
||||
// It's a negative, valid entry. We should not make another
|
||||
// attempt right now.
|
||||
slog.DebugContext(ctx, "Negative cache entry", "device", deviceID, "finder", finder, "until1", cacheEntry.when.Add(finder.negCacheTime), "until2", cacheEntry.validUntil)
|
||||
continue
|
||||
}
|
||||
|
||||
// It's expired. Ignore and continue.
|
||||
}
|
||||
|
||||
// Perform the actual lookup and cache the result.
|
||||
if addrs, err := finder.Lookup(ctx, deviceID); err == nil {
|
||||
slog.DebugContext(ctx, "Got finder result", "device", deviceID, "finder", finder, "address", addrs)
|
||||
addresses = append(addresses, addrs...)
|
||||
finder.cache.Set(deviceID, CacheEntry{
|
||||
Addresses: addrs,
|
||||
when: time.Now(),
|
||||
found: len(addrs) > 0,
|
||||
})
|
||||
} else {
|
||||
// Lookup returned error, add a negative cache entry.
|
||||
entry := CacheEntry{
|
||||
when: time.Now(),
|
||||
found: false,
|
||||
}
|
||||
if err, ok := err.(cachedError); ok {
|
||||
entry.validUntil = time.Now().Add(err.CacheFor())
|
||||
}
|
||||
finder.cache.Set(deviceID, entry)
|
||||
}
|
||||
}
|
||||
m.mut.RUnlock()
|
||||
|
||||
Reference in new issue
Block a user