Skip to content

Commit

Permalink
chore: make mem addr book public
Browse files Browse the repository at this point in the history
  • Loading branch information
2color committed Nov 25, 2024
1 parent 288868c commit 1992262
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
54 changes: 27 additions & 27 deletions p2p/host/peerstore/pstoremem/addr_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ const (
defaultMaxUnconnectedAddrs = 1_000_000
)

// memoryAddrBook manages addresses.
type memoryAddrBook struct {
// MemoryAddrBook manages addresses.
type MemoryAddrBook struct {
mu sync.RWMutex
addrs peerAddrs
signedPeerRecords map[peer.ID]*peerRecordState
Expand All @@ -198,13 +198,13 @@ type memoryAddrBook struct {
clock clock
}

var _ peerstore.AddrBook = (*memoryAddrBook)(nil)
var _ peerstore.CertifiedAddrBook = (*memoryAddrBook)(nil)
var _ peerstore.AddrBook = (*MemoryAddrBook)(nil)
var _ peerstore.CertifiedAddrBook = (*MemoryAddrBook)(nil)

func NewAddrBook() *memoryAddrBook {
func NewAddrBook() *MemoryAddrBook {
ctx, cancel := context.WithCancel(context.Background())

ab := &memoryAddrBook{
ab := &MemoryAddrBook{
addrs: newPeerAddrs(),
signedPeerRecords: make(map[peer.ID]*peerRecordState),
subManager: NewAddrSubManager(),
Expand All @@ -218,10 +218,10 @@ func NewAddrBook() *memoryAddrBook {
return ab
}

type AddrBookOption func(book *memoryAddrBook) error
type AddrBookOption func(book *MemoryAddrBook) error

func WithClock(clock clock) AddrBookOption {
return func(book *memoryAddrBook) error {
return func(book *MemoryAddrBook) error {
book.clock = clock
return nil
}
Expand All @@ -231,21 +231,21 @@ func WithClock(clock clock) AddrBookOption {
// The maximum number of connected addresses is bounded by the connection
// limits in the Connection Manager and Resource Manager.
func WithMaxAddresses(n int) AddrBookOption {
return func(b *memoryAddrBook) error {
return func(b *MemoryAddrBook) error {
b.maxUnconnectedAddrs = n
return nil
}
}

func WithMaxSignedPeerRecords(n int) AddrBookOption {
return func(b *memoryAddrBook) error {
return func(b *MemoryAddrBook) error {
b.maxSignedPeerRecords = n
return nil
}
}

// background periodically schedules a gc
func (mab *memoryAddrBook) background(ctx context.Context) {
func (mab *MemoryAddrBook) background(ctx context.Context) {
defer mab.refCount.Done()
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
Expand All @@ -260,14 +260,14 @@ func (mab *memoryAddrBook) background(ctx context.Context) {
}
}

func (mab *memoryAddrBook) Close() error {
func (mab *MemoryAddrBook) Close() error {
mab.cancel()
mab.refCount.Wait()
return nil
}

// gc garbage collects the in-memory address book.
func (mab *memoryAddrBook) gc() {
func (mab *MemoryAddrBook) gc() {
now := mab.clock.Now()
mab.mu.Lock()
defer mab.mu.Unlock()
Expand All @@ -281,7 +281,7 @@ func (mab *memoryAddrBook) gc() {
}
}

func (mab *memoryAddrBook) PeersWithAddrs() peer.IDSlice {
func (mab *MemoryAddrBook) PeersWithAddrs() peer.IDSlice {
mab.mu.RLock()
defer mab.mu.RUnlock()
peers := make(peer.IDSlice, 0, len(mab.addrs.Addrs))
Expand All @@ -292,19 +292,19 @@ func (mab *memoryAddrBook) PeersWithAddrs() peer.IDSlice {
}

// AddAddr calls AddAddrs(p, []ma.Multiaddr{addr}, ttl)
func (mab *memoryAddrBook) AddAddr(p peer.ID, addr ma.Multiaddr, ttl time.Duration) {
func (mab *MemoryAddrBook) AddAddr(p peer.ID, addr ma.Multiaddr, ttl time.Duration) {
mab.AddAddrs(p, []ma.Multiaddr{addr}, ttl)
}

// AddAddrs adds `addrs` for peer `p`, which will expire after the given `ttl`.
// This function never reduces the TTL or expiration of an address.
func (mab *memoryAddrBook) AddAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration) {
func (mab *MemoryAddrBook) AddAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration) {
mab.addAddrs(p, addrs, ttl)
}

// ConsumePeerRecord adds addresses from a signed peer.PeerRecord, which will expire after the given TTL.
// See https://godoc.org/github.com/libp2p/go-libp2p/core/peerstore#CertifiedAddrBook for more details.
func (mab *memoryAddrBook) ConsumePeerRecord(recordEnvelope *record.Envelope, ttl time.Duration) (bool, error) {
func (mab *MemoryAddrBook) ConsumePeerRecord(recordEnvelope *record.Envelope, ttl time.Duration) (bool, error) {
r, err := recordEnvelope.Record()
if err != nil {
return false, err
Expand Down Expand Up @@ -337,20 +337,20 @@ func (mab *memoryAddrBook) ConsumePeerRecord(recordEnvelope *record.Envelope, tt
return true, nil
}

func (mab *memoryAddrBook) maybeDeleteSignedPeerRecordUnlocked(p peer.ID) {
func (mab *MemoryAddrBook) maybeDeleteSignedPeerRecordUnlocked(p peer.ID) {
if len(mab.addrs.Addrs[p]) == 0 {
delete(mab.signedPeerRecords, p)
}
}

func (mab *memoryAddrBook) addAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration) {
func (mab *MemoryAddrBook) addAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration) {
mab.mu.Lock()
defer mab.mu.Unlock()

mab.addAddrsUnlocked(p, addrs, ttl)
}

func (mab *memoryAddrBook) addAddrsUnlocked(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration) {
func (mab *MemoryAddrBook) addAddrsUnlocked(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration) {
defer mab.maybeDeleteSignedPeerRecordUnlocked(p)

// if ttl is zero, exit. nothing to do.
Expand Down Expand Up @@ -401,13 +401,13 @@ func (mab *memoryAddrBook) addAddrsUnlocked(p peer.ID, addrs []ma.Multiaddr, ttl
}

// SetAddr calls mgr.SetAddrs(p, addr, ttl)
func (mab *memoryAddrBook) SetAddr(p peer.ID, addr ma.Multiaddr, ttl time.Duration) {
func (mab *MemoryAddrBook) SetAddr(p peer.ID, addr ma.Multiaddr, ttl time.Duration) {
mab.SetAddrs(p, []ma.Multiaddr{addr}, ttl)
}

// SetAddrs sets the ttl on addresses. This clears any TTL there previously.
// This is used when we receive the best estimate of the validity of an address.
func (mab *memoryAddrBook) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration) {
func (mab *MemoryAddrBook) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration) {
mab.mu.Lock()
defer mab.mu.Unlock()

Expand Down Expand Up @@ -457,7 +457,7 @@ func (mab *memoryAddrBook) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Du

// UpdateAddrs updates the addresses associated with the given peer that have
// the given oldTTL to have the given newTTL.
func (mab *memoryAddrBook) UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL time.Duration) {
func (mab *MemoryAddrBook) UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL time.Duration) {
mab.mu.Lock()
defer mab.mu.Unlock()

Expand Down Expand Up @@ -485,7 +485,7 @@ func (mab *memoryAddrBook) UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL t
}

// Addrs returns all known (and valid) addresses for a given peer
func (mab *memoryAddrBook) Addrs(p peer.ID) []ma.Multiaddr {
func (mab *MemoryAddrBook) Addrs(p peer.ID) []ma.Multiaddr {
mab.mu.RLock()
defer mab.mu.RUnlock()
if _, ok := mab.addrs.Addrs[p]; !ok {
Expand All @@ -510,7 +510,7 @@ func validAddrs(now time.Time, amap map[string]*expiringAddr) []ma.Multiaddr {
// GetPeerRecord returns a Envelope containing a PeerRecord for the
// given peer id, if one exists.
// Returns nil if no signed PeerRecord exists for the peer.
func (mab *memoryAddrBook) GetPeerRecord(p peer.ID) *record.Envelope {
func (mab *MemoryAddrBook) GetPeerRecord(p peer.ID) *record.Envelope {
mab.mu.RLock()
defer mab.mu.RUnlock()

Expand All @@ -530,7 +530,7 @@ func (mab *memoryAddrBook) GetPeerRecord(p peer.ID) *record.Envelope {
}

// ClearAddrs removes all previously stored addresses
func (mab *memoryAddrBook) ClearAddrs(p peer.ID) {
func (mab *MemoryAddrBook) ClearAddrs(p peer.ID) {
mab.mu.Lock()
defer mab.mu.Unlock()

Expand All @@ -543,7 +543,7 @@ func (mab *memoryAddrBook) ClearAddrs(p peer.ID) {

// AddrStream returns a channel on which all new addresses discovered for a
// given peer ID will be published.
func (mab *memoryAddrBook) AddrStream(ctx context.Context, p peer.ID) <-chan ma.Multiaddr {
func (mab *MemoryAddrBook) AddrStream(ctx context.Context, p peer.ID) <-chan ma.Multiaddr {
var initial []ma.Multiaddr

mab.mu.RLock()
Expand Down
8 changes: 4 additions & 4 deletions p2p/host/peerstore/pstoremem/peerstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
type pstoremem struct {
peerstore.Metrics

*MemoryAddrBook
*memoryKeyBook
*memoryAddrBook
*memoryProtoBook
*memoryPeerMetadata
}
Expand Down Expand Up @@ -51,7 +51,7 @@ func NewPeerstore(opts ...Option) (ps *pstoremem, err error) {
return &pstoremem{
Metrics: pstore.NewMetrics(),
memoryKeyBook: NewKeyBook(),
memoryAddrBook: ab,
MemoryAddrBook: ab,
memoryProtoBook: pb,
memoryPeerMetadata: NewPeerMetadata(),
}, nil
Expand All @@ -67,7 +67,7 @@ func (ps *pstoremem) Close() (err error) {
}
}
weakClose("keybook", ps.memoryKeyBook)
weakClose("addressbook", ps.memoryAddrBook)
weakClose("addressbook", ps.MemoryAddrBook)
weakClose("protobook", ps.memoryProtoBook)
weakClose("peermetadata", ps.memoryPeerMetadata)

Expand Down Expand Up @@ -96,7 +96,7 @@ func (ps *pstoremem) Peers() peer.IDSlice {
func (ps *pstoremem) PeerInfo(p peer.ID) peer.AddrInfo {
return peer.AddrInfo{
ID: p,
Addrs: ps.memoryAddrBook.Addrs(p),
Addrs: ps.MemoryAddrBook.Addrs(p),
}
}

Expand Down

0 comments on commit 1992262

Please sign in to comment.