address review comments.

This commit is contained in:
Raúl Kripalani 2018-09-12 13:44:14 +01:00
parent 4e0c2169c7
commit c7d99489fd
3 changed files with 14 additions and 12 deletions

View File

@ -36,7 +36,7 @@ type dsAddrBook struct {
// NewAddrBook initializes a new address book given a // NewAddrBook initializes a new address book given a
// Datastore instance, a context for managing the TTL manager, // Datastore instance, a context for managing the TTL manager,
// and the interval at which the TTL manager should sweep the Datastore. // 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 ( var (
cache cache = &noopCache{} cache cache = &noopCache{}
err error err error
@ -163,7 +163,7 @@ func (mgr *dsAddrBook) setAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Durati
return err return err
} }
// Successful. Update cache and broadcast event. // Update was successful, so broadcast event only for new addresses.
for i, _ := range keys { for i, _ := range keys {
if !existed[i] { if !existed[i] {
mgr.subsManager.BroadcastAddr(p, addrs[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 { if ttlReset {
mgr.ttlManager.setTTLs(keys, ttl) mgr.ttlManager.setTTLs(keys, ttl)
} else { } else {
mgr.ttlManager.insertTTLs(keys, ttl) mgr.ttlManager.insertOrExtendTTLs(keys, ttl)
} }
return nil return nil
@ -294,7 +294,8 @@ func (mgr *dsAddrBook) PeersWithAddrs() peer.IDSlice {
return 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 { for id := range idset {
pid, _ := peer.IDB58Decode(id) pid, _ := peer.IDB58Decode(id)
ids[i] = pid 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() mgr.Lock()
defer mgr.Unlock() defer mgr.Unlock()

View File

@ -170,7 +170,7 @@ func badgerStore(t testing.TB) (ds.TxnDatastore, func()) {
return ds, closer 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()) { return func() (pstore.Peerstore, func()) {
ds, closeFunc := badgerStore(tb) 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()) { return func() (pstore.AddrBook, func()) {
ds, closeDB := badgerStore(tb) ds, closeDB := badgerStore(tb)

View File

@ -11,11 +11,12 @@ import (
) )
// Configuration object for the peerstore. // 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. // The size of the in-memory cache. A value of 0 or lower disables the cache.
CacheSize uint 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 TTLInterval time.Duration
// Number of times to retry transactional writes. // Number of times to retry transactional writes.
@ -26,8 +27,8 @@ type PeerstoreOpts struct {
// * Cache size: 1024 // * Cache size: 1024
// * TTL sweep interval: 1 second // * TTL sweep interval: 1 second
// * WriteRetries: 5 // * WriteRetries: 5
func DefaultOpts() PeerstoreOpts { func DefaultOpts() Options {
return PeerstoreOpts{ return Options{
CacheSize: 1024, CacheSize: 1024,
TTLInterval: time.Second, TTLInterval: time.Second,
WriteRetries: 5, WriteRetries: 5,
@ -35,7 +36,7 @@ func DefaultOpts() PeerstoreOpts {
} }
// NewPeerstore creates a peerstore backed by the provided persistent datastore. // 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) addrBook, err := NewAddrBook(ctx, store, opts)
if err != nil { if err != nil {
return nil, err return nil, err