Break nesting via continues
@@ -4,12 +4,12 @@ import (
"context"
"time"
+ "bytes"
+ "encoding/gob"
+ "github.com/dgraph-io/badger"
"github.com/libp2p/go-libp2p-peer"
ma "github.com/multiformats/go-multiaddr"
- "github.com/dgraph-io/badger"
"github.com/multiformats/go-multihash"
- "encoding/gob"
- "bytes"
)
type BadgerAddrManager struct {
@@ -99,7 +99,6 @@ func (mgr *BadgerAddrManager) AddAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time
txn := mgr.DB.NewTransaction(true)
defer txn.Discard()
-
go mgr.sendSubscriptionUpdates(&p, addrs)
addAddrs(p, addrs, ttl, txn)
@@ -126,8 +125,9 @@ func (mgr *BadgerAddrManager) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time
if ttl <= 0 {
if err := txn.Delete(key); err != nil {
log.Error(err)
+ continue
+ }
}
- } else {
entry := &addrentry{Addr: addr.Bytes(), TTL: ttl}
buf := &bytes.Buffer{}
enc := gob.NewEncoder(buf)
@@ -137,7 +137,6 @@ func (mgr *BadgerAddrManager) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time
}
txn.SetWithTTL(key, buf.Bytes(), ttl)
}
- }
txn.Commit(nil)
}
@@ -192,14 +191,17 @@ func (mgr *BadgerAddrManager) Addrs(p peer.ID) []ma.Multiaddr {
var addrs []ma.Multiaddr
- for iter.ValidForPrefix(prefix) {
+ for ; iter.ValidForPrefix(prefix); iter.Next() {
item := iter.Item()
+ if item.IsDeletedOrExpired() {
+ continue
+ }
- if !item.IsDeletedOrExpired() {
value, err := item.Value()
if err != nil {
log.Error(err)
- } else {
+ continue
+ }
entry := &addrentry{}
buf := bytes.NewBuffer(value)
dec := gob.NewDecoder(buf)
@@ -210,10 +212,6 @@ func (mgr *BadgerAddrManager) Addrs(p peer.ID) []ma.Multiaddr {
addrs = append(addrs, ma.Cast(entry.Addr))
}
}
- }
-
- iter.Next()
- }
txn.Commit(nil)
