mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2025-03-29 13:40:07 +08:00
chore: cleanup addr book patch
1. Remove duplicate import. 2. Lift empty check to simplify code. 3. Use a more efficient map key (go will optimize this to not allocate).
This commit is contained in:
parent
8c02e08cd3
commit
517a48433f
@ -7,8 +7,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/libp2p/go-libp2p-core/record"
|
|
||||||
|
|
||||||
ds "github.com/ipfs/go-datastore"
|
ds "github.com/ipfs/go-datastore"
|
||||||
"github.com/ipfs/go-datastore/query"
|
"github.com/ipfs/go-datastore/query"
|
||||||
logging "github.com/ipfs/go-log"
|
logging "github.com/ipfs/go-log"
|
||||||
@ -454,6 +452,10 @@ func (ab *dsAddrBook) ClearAddrs(p peer.ID) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ab *dsAddrBook) setAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration, mode ttlWriteMode, signed bool) (err error) {
|
func (ab *dsAddrBook) setAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration, mode ttlWriteMode, signed bool) (err error) {
|
||||||
|
if len(addrs) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
pr, err := ab.loadRecord(p, true, false)
|
pr, err := ab.loadRecord(p, true, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to load peerstore entry for peer %v while setting addrs, err: %v", p, err)
|
return fmt.Errorf("failed to load peerstore entry for peer %v while setting addrs, err: %v", p, err)
|
||||||
@ -468,16 +470,13 @@ func (ab *dsAddrBook) setAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duratio
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
newExp := time.Now().Add(ttl).Unix()
|
newExp := time.Now().Add(ttl).Unix()
|
||||||
var addrsMap map[string]*pb.AddrBookRecord_AddrEntry
|
addrsMap := make(map[string]*pb.AddrBookRecord_AddrEntry, len(pr.Addrs))
|
||||||
if len(addrs) > 0 {
|
for _, addr := range pr.Addrs {
|
||||||
addrsMap = make(map[string]*pb.AddrBookRecord_AddrEntry, len(pr.Addrs))
|
addrsMap[string(addr.Addr.Bytes())] = addr
|
||||||
for _, addr := range pr.Addrs {
|
|
||||||
addrsMap[addr.Addr.String()] = addr
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
updateExisting := func(incoming ma.Multiaddr) *pb.AddrBookRecord_AddrEntry {
|
updateExisting := func(incoming ma.Multiaddr) *pb.AddrBookRecord_AddrEntry {
|
||||||
existingEntry := addrsMap[incoming.String()]
|
existingEntry := addrsMap[string(incoming.Bytes())]
|
||||||
if existingEntry == nil {
|
if existingEntry == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user