mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2025-01-14 02:30:05 +08:00
Merge pull request #161 from libp2p/fix/races
fix: fix some race conditions in the ds address book
This commit is contained in:
commit
87136c9300
@ -299,7 +299,16 @@ func (ab *dsAddrBook) ConsumePeerRecord(recordEnvelope *record.Envelope, ttl tim
|
||||
|
||||
func (ab *dsAddrBook) latestPeerRecordSeq(p peer.ID) uint64 {
|
||||
pr, err := ab.loadRecord(p, true, false)
|
||||
if err != nil || len(pr.Addrs) == 0 || pr.CertifiedRecord == nil || len(pr.CertifiedRecord.Raw) == 0 {
|
||||
if err != nil {
|
||||
// We ignore the error because we don't want to fail storing a new record in this
|
||||
// case.
|
||||
log.Errorw("unable to load record", "peer", p, "error", err)
|
||||
return 0
|
||||
}
|
||||
pr.RLock()
|
||||
defer pr.RUnlock()
|
||||
|
||||
if len(pr.Addrs) == 0 || pr.CertifiedRecord == nil || len(pr.CertifiedRecord.Raw) == 0 {
|
||||
return 0
|
||||
}
|
||||
return pr.CertifiedRecord.Seq
|
||||
@ -552,13 +561,13 @@ func (ab *dsAddrBook) deleteAddrs(p peer.ID, addrs []ma.Multiaddr) (err error) {
|
||||
return fmt.Errorf("failed to load peerstore entry for peer %v while deleting addrs, err: %v", p, err)
|
||||
}
|
||||
|
||||
pr.Lock()
|
||||
defer pr.Unlock()
|
||||
|
||||
if pr.Addrs == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
pr.Lock()
|
||||
defer pr.Unlock()
|
||||
|
||||
pr.Addrs = deleteInPlace(pr.Addrs, addrs)
|
||||
|
||||
pr.dirty = true
|
||||
|
Loading…
Reference in New Issue
Block a user