Skip to content

Commit

Permalink
feat: add env var for recently connected ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
2color committed Dec 17, 2024
1 parent 6b4b40d commit fe7ad54
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 21 deletions.
40 changes: 26 additions & 14 deletions cached_addr_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import (

const (
Subsystem = "cached_addr_book"
// The TTL to keep recently connected peers for. Same as [amino.DefaultProvideValidity] in go-libp2p-kad-dht
RecentlyConnectedAddrTTL = amino.DefaultProvideValidity

// The default TTL to keep recently connected peers' multiaddrs for
DefaultRecentlyConnectedAddrTTL = amino.DefaultProvideValidity

// Connected peers don't expire until they disconnect
ConnectedAddrTTL = peerstore.ConnectedAddrTTL
Expand All @@ -48,8 +49,9 @@ const (
// 1_000_000 is 10x the default number of signed peer records cached by the memory address book.
PeerCacheSize = 1_000_000

// Maximum backoff duration for probing a peer
MaxBackoffDuration = time.Hour * 48
// Maximum backoff duration for probing a peer. After this duration, we will stop
// trying to connect to the peer and remove it from the cache.
MaxBackoffDuration = amino.DefaultProvideValidity
)

var (
Expand Down Expand Up @@ -84,10 +86,11 @@ 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
isProbing atomic.Bool
allowPrivateIPs bool // for testing
addrBook peerstore.AddrBook // memory address book
peerCache *lru.Cache[peer.ID, peerState] // LRU cache with additional metadata about peer
isProbing atomic.Bool
allowPrivateIPs bool // for testing
recentlyConnectedTTL time.Duration
}

type AddrBookOption func(*cachedAddrBook) error
Expand All @@ -99,15 +102,23 @@ func WithAllowPrivateIPs() AddrBookOption {
}
}

func WithRecentlyConnectedTTL(ttl time.Duration) AddrBookOption {
return func(cab *cachedAddrBook) error {
cab.recentlyConnectedTTL = ttl
return nil
}
}

func newCachedAddrBook(opts ...AddrBookOption) (*cachedAddrBook, error) {
peerCache, err := lru.New[peer.ID, peerState](PeerCacheSize)
if err != nil {
return nil, err
}

cab := &cachedAddrBook{
peerCache: peerCache,
addrBook: pstoremem.NewAddrBook(),
peerCache: peerCache,
addrBook: pstoremem.NewAddrBook(),
recentlyConnectedTTL: DefaultRecentlyConnectedAddrTTL, // Set default value
}

for _, opt := range opts {
Expand All @@ -116,6 +127,7 @@ func newCachedAddrBook(opts ...AddrBookOption) (*cachedAddrBook, error) {
return nil, err
}
}
logger.Infof("cachedAddrBook: Using TTL of %s for recently connected peers", cab.recentlyConnectedTTL)
return cab, nil
}

Expand Down Expand Up @@ -157,7 +169,7 @@ func (cab *cachedAddrBook) background(ctx context.Context, host host.Host) {
cab.peerCache.Add(ev.Peer, pState)
peerStateSize.Set(float64(cab.peerCache.Len())) // update metric

ttl := getTTL(host.Network().Connectedness(ev.Peer))
ttl := cab.getTTL(host.Network().Connectedness(ev.Peer))
if ev.SignedPeerRecord != nil {
logger.Debug("Caching signed peer record")
cab, ok := peerstore.GetCertifiedAddrBook(cab.addrBook)
Expand All @@ -175,7 +187,7 @@ func (cab *cachedAddrBook) background(ctx context.Context, host host.Host) {
case event.EvtPeerConnectednessChanged:
// If the peer is not connected or limited, we update the TTL
if !hasValidConnectedness(ev.Connectedness) {
cab.addrBook.UpdateAddrs(ev.Peer, ConnectedAddrTTL, RecentlyConnectedAddrTTL)
cab.addrBook.UpdateAddrs(ev.Peer, ConnectedAddrTTL, cab.recentlyConnectedTTL)
}
}
case <-probeTicker.C:
Expand Down Expand Up @@ -313,9 +325,9 @@ func hasValidConnectedness(connectedness network.Connectedness) bool {
return connectedness == network.Connected || connectedness == network.Limited
}

func getTTL(connectedness network.Connectedness) time.Duration {
func (cab *cachedAddrBook) getTTL(connectedness network.Connectedness) time.Duration {
if hasValidConnectedness(connectedness) {
return ConnectedAddrTTL
}
return RecentlyConnectedAddrTTL
return cab.recentlyConnectedTTL
}
12 changes: 12 additions & 0 deletions docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ Whether or not the Accelerated DHT is enabled or not.

Default: `true`

### `SOMEGUY_CACHED_ADDR_BOOK`

Whether or not the Cached Address Book is enabled or not.

Default: `true`

### `SOMEGUY_CACHED_ADDR_BOOK_RECENT_TTL`

The TTL for recently connected peers' multiaddrs in the cached address book.

Default: `48h`

### `SOMEGUY_PROVIDER_ENDPOINTS`

Comma-separated list of other Delegated Routing V1 endpoints to proxy provider requests to.
Expand Down
14 changes: 11 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ func main() {
EnvVars: []string{"SOMEGUY_CACHED_ADDR_BOOK"},
Usage: "use a cached address book to improve provider lookup responses",
},
&cli.DurationFlag{
Name: "cached-addr-book-recent-ttl",
DefaultText: DefaultRecentlyConnectedAddrTTL.String(),
Value: DefaultRecentlyConnectedAddrTTL,
EnvVars: []string{"SOMEGUY_CACHED_ADDR_BOOK_RECENT_TTL"},
Usage: "TTL for recently connected peers' multiaddrs in the cached address book",
},
&cli.StringSliceFlag{
Name: "provider-endpoints",
Value: cli.NewStringSlice(cidContactEndpoint),
Expand Down Expand Up @@ -121,9 +128,10 @@ 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"),
listenAddress: ctx.String("listen-address"),
acceleratedDHTClient: ctx.Bool("accelerated-dht"),
cachedAddrBook: ctx.Bool("cached-addr-book"),
cachedAddrBookRecentTTL: ctx.Duration("cached-addr-book-recent-ttl"),

contentEndpoints: ctx.StringSlice("provider-endpoints"),
peerEndpoints: ctx.StringSlice("peer-endpoints"),
Expand Down
15 changes: 11 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ func withRequestLogger(next http.Handler) http.Handler {
}

type config struct {
listenAddress string
acceleratedDHTClient bool
cachedAddrBook bool
listenAddress string
acceleratedDHTClient bool
cachedAddrBook bool
cachedAddrBookRecentTTL time.Duration

contentEndpoints []string
peerEndpoints []string
Expand Down Expand Up @@ -85,7 +86,13 @@ func start(ctx context.Context, cfg *config) error {

if cfg.cachedAddrBook {
fmt.Println("Using cached address book to speed up provider discovery")
cachedAddrBook, err = newCachedAddrBook()
opts := []AddrBookOption{}

if cfg.cachedAddrBookRecentTTL > 0 {
opts = append(opts, WithRecentlyConnectedTTL(cfg.cachedAddrBookRecentTTL))
}

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

0 comments on commit fe7ad54

Please sign in to comment.