diff --git a/cached_addr_book.go b/cached_addr_book.go index 128a852..6b708b2 100644 --- a/cached_addr_book.go +++ b/cached_addr_book.go @@ -88,6 +88,7 @@ type peerState struct { type cachedAddrBook struct { addrBook peerstore.AddrBook // memory address book peerCache *lru.Cache[peer.ID, peerState] // LRU cache with additional metadata about peer + probingEnabled bool isProbing atomic.Bool allowPrivateIPs bool // for testing recentlyConnectedTTL time.Duration @@ -109,6 +110,13 @@ func WithRecentlyConnectedTTL(ttl time.Duration) AddrBookOption { } } +func WithActiveProbing(enabled bool) AddrBookOption { + return func(cab *cachedAddrBook) error { + cab.probingEnabled = enabled + return nil + } +} + func newCachedAddrBook(opts ...AddrBookOption) (*cachedAddrBook, error) { peerCache, err := lru.New[peer.ID, peerState](PeerCacheSize) if err != nil { @@ -127,7 +135,8 @@ func newCachedAddrBook(opts ...AddrBookOption) (*cachedAddrBook, error) { return nil, err } } - logger.Infof("cachedAddrBook: Using TTL of %s for recently connected peers", cab.recentlyConnectedTTL) + logger.Infof("Using TTL of %s for recently connected peers", cab.recentlyConnectedTTL) + logger.Infof("Probing enabled: %t", cab.probingEnabled) return cab, nil } @@ -191,6 +200,10 @@ func (cab *cachedAddrBook) background(ctx context.Context, host host.Host) { } } case <-probeTicker.C: + if !cab.probingEnabled { + logger.Debug("Probing disabled, skipping") + continue + } if cab.isProbing.Load() { logger.Debug("Skipping peer probe, still running") continue diff --git a/docs/environment-variables.md b/docs/environment-variables.md index 2710dda..bddd91b 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -49,6 +49,12 @@ The TTL for recently connected peers' multiaddrs in the cached address book. Default: `48h` +### `SOMEGUY_CACHED_ADDR_BOOK_ACTIVE_PROBING` + +Whether or not the Cached Address Book should actively probe peers in cache to keep their multiaddrs up to date. + +Default: `true` + ### `SOMEGUY_PROVIDER_ENDPOINTS` Comma-separated list of other Delegated Routing V1 endpoints to proxy provider requests to. diff --git a/main.go b/main.go index 8d193b9..94488bb 100644 --- a/main.go +++ b/main.go @@ -44,6 +44,12 @@ func main() { EnvVars: []string{"SOMEGUY_CACHED_ADDR_BOOK"}, Usage: "use a cached address book to improve provider lookup responses", }, + &cli.BoolFlag{ + Name: "cached-addr-book-active-probing", + Value: true, + EnvVars: []string{"SOMEGUY_CACHED_ADDR_BOOK_ACTIVE_PROBING"}, + Usage: "actively probe peers in cache to keep their multiaddrs up to date", + }, &cli.DurationFlag{ Name: "cached-addr-book-recent-ttl", DefaultText: DefaultRecentlyConnectedAddrTTL.String(), @@ -128,10 +134,11 @@ func main() { }, Action: func(ctx *cli.Context) error { cfg := &config{ - listenAddress: ctx.String("listen-address"), - acceleratedDHTClient: ctx.Bool("accelerated-dht"), - cachedAddrBook: ctx.Bool("cached-addr-book"), - cachedAddrBookRecentTTL: ctx.Duration("cached-addr-book-recent-ttl"), + listenAddress: ctx.String("listen-address"), + acceleratedDHTClient: ctx.Bool("accelerated-dht"), + cachedAddrBook: ctx.Bool("cached-addr-book"), + cachedAddrBookActiveProbing: ctx.Bool("cached-addr-book-active-probing"), + cachedAddrBookRecentTTL: ctx.Duration("cached-addr-book-recent-ttl"), contentEndpoints: ctx.StringSlice("provider-endpoints"), peerEndpoints: ctx.StringSlice("peer-endpoints"), diff --git a/server.go b/server.go index 7b4ef9e..4052c81 100644 --- a/server.go +++ b/server.go @@ -40,10 +40,11 @@ func withRequestLogger(next http.Handler) http.Handler { } type config struct { - listenAddress string - acceleratedDHTClient bool - cachedAddrBook bool - cachedAddrBookRecentTTL time.Duration + listenAddress string + acceleratedDHTClient bool + cachedAddrBook bool + cachedAddrBookActiveProbing bool + cachedAddrBookRecentTTL time.Duration contentEndpoints []string peerEndpoints []string @@ -85,13 +86,15 @@ func start(ctx context.Context, cfg *config) error { var cachedAddrBook *cachedAddrBook if cfg.cachedAddrBook { - fmt.Println("Using cached address book to speed up provider discovery") + fmt.Printf("Using cached address book to speed up provider discovery (active probing enabled: %t)\n", cfg.cachedAddrBookActiveProbing) opts := []AddrBookOption{} if cfg.cachedAddrBookRecentTTL > 0 { opts = append(opts, WithRecentlyConnectedTTL(cfg.cachedAddrBookRecentTTL)) } + opts = append(opts, WithActiveProbing(cfg.cachedAddrBookActiveProbing)) + cachedAddrBook, err = newCachedAddrBook(opts...) if err != nil { return err