diff --git a/pstoreds/addr_book.go b/pstoreds/addr_book.go index 9bb5a45..f338cb4 100644 --- a/pstoreds/addr_book.go +++ b/pstoreds/addr_book.go @@ -36,7 +36,7 @@ type dsAddrBook struct { // NewAddrBook initializes a new address book given a // Datastore instance, a context for managing the TTL manager, // and the interval at which the TTL manager should sweep the Datastore. -func NewAddrBook(ctx context.Context, ds ds.TxnDatastore, opts PeerstoreOpts) (*dsAddrBook, error) { +func NewAddrBook(ctx context.Context, ds ds.TxnDatastore, opts Options) (*dsAddrBook, error) { var ( cache cache = &noopCache{} err error @@ -163,7 +163,7 @@ func (mgr *dsAddrBook) setAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Durati return err } - // Successful. Update cache and broadcast event. + // Update was successful, so broadcast event only for new addresses. for i, _ := range keys { if !existed[i] { mgr.subsManager.BroadcastAddr(p, addrs[i]) @@ -175,7 +175,7 @@ func (mgr *dsAddrBook) setAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Durati if ttlReset { mgr.ttlManager.setTTLs(keys, ttl) } else { - mgr.ttlManager.insertTTLs(keys, ttl) + mgr.ttlManager.insertOrExtendTTLs(keys, ttl) } return nil @@ -294,7 +294,8 @@ func (mgr *dsAddrBook) PeersWithAddrs() peer.IDSlice { return peer.IDSlice{} } - ids, i := make(peer.IDSlice, len(idset)), 0 + ids := make(peer.IDSlice, len(idset)) + i := 0 for id := range idset { pid, _ := peer.IDB58Decode(id) ids[i] = pid @@ -495,7 +496,7 @@ func (mgr *ttlManager) deleteTTLs(keys []ds.Key) { } } -func (mgr *ttlManager) insertTTLs(keys []ds.Key, ttl time.Duration) { +func (mgr *ttlManager) insertOrExtendTTLs(keys []ds.Key, ttl time.Duration) { mgr.Lock() defer mgr.Unlock() diff --git a/pstoreds/ds_test.go b/pstoreds/ds_test.go index 4a8d04c..20bef6a 100644 --- a/pstoreds/ds_test.go +++ b/pstoreds/ds_test.go @@ -170,7 +170,7 @@ func badgerStore(t testing.TB) (ds.TxnDatastore, func()) { return ds, closer } -func peerstoreFactory(tb testing.TB, opts PeerstoreOpts) pt.PeerstoreFactory { +func peerstoreFactory(tb testing.TB, opts Options) pt.PeerstoreFactory { return func() (pstore.Peerstore, func()) { ds, closeFunc := badgerStore(tb) @@ -183,7 +183,7 @@ func peerstoreFactory(tb testing.TB, opts PeerstoreOpts) pt.PeerstoreFactory { } } -func addressBookFactory(tb testing.TB, opts PeerstoreOpts) pt.AddrBookFactory { +func addressBookFactory(tb testing.TB, opts Options) pt.AddrBookFactory { return func() (pstore.AddrBook, func()) { ds, closeDB := badgerStore(tb) diff --git a/pstoreds/peerstore.go b/pstoreds/peerstore.go index 74576d0..a3b4d3f 100644 --- a/pstoreds/peerstore.go +++ b/pstoreds/peerstore.go @@ -11,11 +11,12 @@ import ( ) // Configuration object for the peerstore. -type PeerstoreOpts struct { +type Options struct { // The size of the in-memory cache. A value of 0 or lower disables the cache. CacheSize uint - // Sweep interval to expire entries when TTL is not managed by underlying datastore. + // Sweep interval to expire entries, only used when TTL is *not* natively managed + // by the underlying datastore. TTLInterval time.Duration // Number of times to retry transactional writes. @@ -26,8 +27,8 @@ type PeerstoreOpts struct { // * Cache size: 1024 // * TTL sweep interval: 1 second // * WriteRetries: 5 -func DefaultOpts() PeerstoreOpts { - return PeerstoreOpts{ +func DefaultOpts() Options { + return Options{ CacheSize: 1024, TTLInterval: time.Second, WriteRetries: 5, @@ -35,7 +36,7 @@ func DefaultOpts() PeerstoreOpts { } // NewPeerstore creates a peerstore backed by the provided persistent datastore. -func NewPeerstore(ctx context.Context, store ds.TxnDatastore, opts PeerstoreOpts) (pstore.Peerstore, error) { +func NewPeerstore(ctx context.Context, store ds.TxnDatastore, opts Options) (pstore.Peerstore, error) { addrBook, err := NewAddrBook(ctx, store, opts) if err != nil { return nil, err