mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2024-12-28 23:50:12 +08:00
commit
3291020401
10
peerstore.go
10
peerstore.go
@ -21,7 +21,7 @@ type peerstore struct {
|
|||||||
PeerMetadata
|
PeerMetadata
|
||||||
|
|
||||||
// lock for protocol information, separate from datastore lock
|
// lock for protocol information, separate from datastore lock
|
||||||
protolock sync.Mutex
|
protolock sync.RWMutex
|
||||||
internedProtocols map[string]string
|
internedProtocols map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,8 +142,8 @@ func (ps *peerstore) getProtocolMap(p peer.ID) (map[string]struct{}, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ps *peerstore) GetProtocols(p peer.ID) ([]string, error) {
|
func (ps *peerstore) GetProtocols(p peer.ID) ([]string, error) {
|
||||||
ps.protolock.Lock()
|
ps.protolock.RLock()
|
||||||
defer ps.protolock.Unlock()
|
defer ps.protolock.RUnlock()
|
||||||
pmap, err := ps.getProtocolMap(p)
|
pmap, err := ps.getProtocolMap(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -158,8 +158,8 @@ func (ps *peerstore) GetProtocols(p peer.ID) ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ps *peerstore) SupportsProtocols(p peer.ID, protos ...string) ([]string, error) {
|
func (ps *peerstore) SupportsProtocols(p peer.ID, protos ...string) ([]string, error) {
|
||||||
ps.protolock.Lock()
|
ps.protolock.RLock()
|
||||||
defer ps.protolock.Unlock()
|
defer ps.protolock.RUnlock()
|
||||||
pmap, err := ps.getProtocolMap(p)
|
pmap, err := ps.getProtocolMap(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -30,7 +30,7 @@ var _ pstore.AddrBook = (*memoryAddrBook)(nil)
|
|||||||
|
|
||||||
// memoryAddrBook manages addresses.
|
// memoryAddrBook manages addresses.
|
||||||
type memoryAddrBook struct {
|
type memoryAddrBook struct {
|
||||||
addrmu sync.Mutex
|
addrmu sync.RWMutex
|
||||||
// Use pointers to save memory. Maps always leave some fraction of their
|
// Use pointers to save memory. Maps always leave some fraction of their
|
||||||
// space unused. storing the *values* directly in the map will
|
// space unused. storing the *values* directly in the map will
|
||||||
// drastically increase the space waste. In our case, by 6x.
|
// drastically increase the space waste. In our case, by 6x.
|
||||||
@ -68,8 +68,8 @@ func (mab *memoryAddrBook) gc() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (mab *memoryAddrBook) PeersWithAddrs() peer.IDSlice {
|
func (mab *memoryAddrBook) PeersWithAddrs() peer.IDSlice {
|
||||||
mab.addrmu.Lock()
|
mab.addrmu.RLock()
|
||||||
defer mab.addrmu.Unlock()
|
defer mab.addrmu.RUnlock()
|
||||||
|
|
||||||
pids := make(peer.IDSlice, 0, len(mab.addrs))
|
pids := make(peer.IDSlice, 0, len(mab.addrs))
|
||||||
for pid := range mab.addrs {
|
for pid := range mab.addrs {
|
||||||
@ -178,8 +178,8 @@ func (mab *memoryAddrBook) UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL t
|
|||||||
|
|
||||||
// Addresses returns all known (and valid) addresses for a given
|
// Addresses returns all known (and valid) addresses for a given
|
||||||
func (mab *memoryAddrBook) Addrs(p peer.ID) []ma.Multiaddr {
|
func (mab *memoryAddrBook) Addrs(p peer.ID) []ma.Multiaddr {
|
||||||
mab.addrmu.Lock()
|
mab.addrmu.RLock()
|
||||||
defer mab.addrmu.Unlock()
|
defer mab.addrmu.RUnlock()
|
||||||
|
|
||||||
amap, found := mab.addrs[p]
|
amap, found := mab.addrs[p]
|
||||||
if !found {
|
if !found {
|
||||||
@ -210,8 +210,8 @@ func (mab *memoryAddrBook) ClearAddrs(p peer.ID) {
|
|||||||
// AddrStream returns a channel on which all new addresses discovered for a
|
// AddrStream returns a channel on which all new addresses discovered for a
|
||||||
// given peer ID will be published.
|
// given peer ID will be published.
|
||||||
func (mab *memoryAddrBook) AddrStream(ctx context.Context, p peer.ID) <-chan ma.Multiaddr {
|
func (mab *memoryAddrBook) AddrStream(ctx context.Context, p peer.ID) <-chan ma.Multiaddr {
|
||||||
mab.addrmu.Lock()
|
mab.addrmu.RLock()
|
||||||
defer mab.addrmu.Unlock()
|
defer mab.addrmu.RUnlock()
|
||||||
|
|
||||||
baseaddrslice := mab.addrs[p]
|
baseaddrslice := mab.addrs[p]
|
||||||
initial := make([]ma.Multiaddr, 0, len(baseaddrslice))
|
initial := make([]ma.Multiaddr, 0, len(baseaddrslice))
|
||||||
|
@ -16,7 +16,7 @@ type memoryPeerMetadata struct {
|
|||||||
// store other data, like versions
|
// store other data, like versions
|
||||||
//ds ds.ThreadSafeDatastore
|
//ds ds.ThreadSafeDatastore
|
||||||
ds map[metakey]interface{}
|
ds map[metakey]interface{}
|
||||||
dslock sync.Mutex
|
dslock sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ pstore.PeerMetadata = (*memoryPeerMetadata)(nil)
|
var _ pstore.PeerMetadata = (*memoryPeerMetadata)(nil)
|
||||||
@ -35,8 +35,8 @@ func (ps *memoryPeerMetadata) Put(p peer.ID, key string, val interface{}) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ps *memoryPeerMetadata) Get(p peer.ID, key string) (interface{}, error) {
|
func (ps *memoryPeerMetadata) Get(p peer.ID, key string) (interface{}, error) {
|
||||||
ps.dslock.Lock()
|
ps.dslock.RLock()
|
||||||
defer ps.dslock.Unlock()
|
defer ps.dslock.RUnlock()
|
||||||
i, ok := ps.ds[metakey{p, key}]
|
i, ok := ps.ds[metakey{p, key}]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, pstore.ErrNotFound
|
return nil, pstore.ErrNotFound
|
||||||
|
Loading…
Reference in New Issue
Block a user