Skip to content

Commit

Permalink
feat: add env var to control active probing
Browse files Browse the repository at this point in the history
  • Loading branch information
2color committed Dec 17, 2024
1 parent fe7ad54 commit 49efe9b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
15 changes: 14 additions & 1 deletion cached_addr_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -109,6 +110,13 @@ func WithRecentlyConnectedTTL(ttl time.Duration) AddrBookOption {
}

Check warning on line 110 in cached_addr_book.go

View check run for this annotation

Codecov / codecov/patch

cached_addr_book.go#L106-L110

Added lines #L106 - L110 were not covered by tests
}

func WithActiveProbing(enabled bool) AddrBookOption {
return func(cab *cachedAddrBook) error {
cab.probingEnabled = enabled
return nil
}

Check warning on line 117 in cached_addr_book.go

View check run for this annotation

Codecov / codecov/patch

cached_addr_book.go#L113-L117

Added lines #L113 - L117 were not covered by tests
}

func newCachedAddrBook(opts ...AddrBookOption) (*cachedAddrBook, error) {
peerCache, err := lru.New[peer.ID, peerState](PeerCacheSize)
if err != nil {
Expand All @@ -127,7 +135,8 @@ func newCachedAddrBook(opts ...AddrBookOption) (*cachedAddrBook, error) {
return nil, err
}

Check warning on line 136 in cached_addr_book.go

View check run for this annotation

Codecov / codecov/patch

cached_addr_book.go#L135-L136

Added lines #L135 - L136 were not covered by tests
}
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
}

Expand Down Expand Up @@ -191,6 +200,10 @@ func (cab *cachedAddrBook) background(ctx context.Context, host host.Host) {
}

Check warning on line 200 in cached_addr_book.go

View check run for this annotation

Codecov / codecov/patch

cached_addr_book.go#L191-L200

Added lines #L191 - L200 were not covered by tests
}
case <-probeTicker.C:
if !cab.probingEnabled {
logger.Debug("Probing disabled, skipping")
continue

Check warning on line 205 in cached_addr_book.go

View check run for this annotation

Codecov / codecov/patch

cached_addr_book.go#L202-L205

Added lines #L202 - L205 were not covered by tests
}
if cab.isProbing.Load() {
logger.Debug("Skipping peer probe, still running")
continue

Check warning on line 209 in cached_addr_book.go

View check run for this annotation

Codecov / codecov/patch

cached_addr_book.go#L207-L209

Added lines #L207 - L209 were not covered by tests
Expand Down
6 changes: 6 additions & 0 deletions docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 11 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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"),

Check warning on line 141 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L137-L141

Added lines #L137 - L141 were not covered by tests

contentEndpoints: ctx.StringSlice("provider-endpoints"),
peerEndpoints: ctx.StringSlice("peer-endpoints"),
Expand Down
13 changes: 8 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}

Check warning on line 94 in server.go

View check run for this annotation

Codecov / codecov/patch

server.go#L86-L94

Added lines #L86 - L94 were not covered by tests

opts = append(opts, WithActiveProbing(cfg.cachedAddrBookActiveProbing))

cachedAddrBook, err = newCachedAddrBook(opts...)
if err != nil {
return err
Expand Down

0 comments on commit 49efe9b

Please sign in to comment.