Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
191 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package network | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
host "github.com/libp2p/go-libp2p-host" | ||
inet "github.com/libp2p/go-libp2p-net" | ||
peer "github.com/libp2p/go-libp2p-peer" | ||
pstore "github.com/libp2p/go-libp2p-peerstore" | ||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
type PeerCache interface { | ||
GetPeerForKey(string) (pstore.PeerInfo, bool) | ||
UpdateCache(string, pstore.PeerInfo) | ||
} | ||
|
||
type cpeer struct { | ||
key string | ||
lastUpdate time.Time | ||
connCount int | ||
info pstore.PeerInfo | ||
} | ||
|
||
type peerCache struct { | ||
peers map[peer.ID]*cpeer | ||
store map[string]*cpeer | ||
muStore sync.RWMutex | ||
} | ||
|
||
type noopCache struct{} | ||
|
||
func (nc *noopCache) GetPeerForKey(_ string) (_ pstore.PeerInfo, _ bool) { return } | ||
func (nc *noopCache) UpdateCache(_ string, _ pstore.PeerInfo) {} | ||
|
||
func NewNoopCache() PeerCache { | ||
return &noopCache{} | ||
} | ||
|
||
func NewPeerCache(h host.Host) PeerCache { | ||
pc := &peerCache{ | ||
peers: make(map[peer.ID]*cpeer), | ||
store: make(map[string]*cpeer), | ||
} | ||
|
||
h.Network().Notify(pc) | ||
return pc | ||
} | ||
|
||
func (pc *peerCache) GetPeerForKey(key string) (pinfo pstore.PeerInfo, ok bool) { | ||
pc.muStore.RLock() | ||
defer pc.muStore.RUnlock() | ||
|
||
var p *cpeer | ||
if p, ok = pc.store[key]; ok { | ||
pinfo = p.info | ||
} | ||
|
||
return | ||
} | ||
|
||
func (pc *peerCache) UpdateCache(key string, pi pstore.PeerInfo) { | ||
pc.muStore.Lock() | ||
|
||
p, ok := pc.peers[pi.ID] | ||
if !ok { | ||
p = &cpeer{ | ||
key: key, | ||
info: pi, | ||
connCount: 0, | ||
lastUpdate: time.Now(), | ||
} | ||
|
||
pc.peers[pi.ID] = p | ||
} | ||
|
||
p.key = key | ||
p.info = pi | ||
pc.store[key] = p | ||
|
||
pc.muStore.Unlock() | ||
} | ||
|
||
func (pc *peerCache) Connected(net inet.Network, c inet.Conn) { | ||
pc.muStore.Lock() | ||
|
||
peerID := c.RemotePeer() | ||
p, ok := pc.peers[peerID] | ||
if !ok { | ||
p = &cpeer{ | ||
connCount: 0, | ||
lastUpdate: time.Now(), | ||
} | ||
|
||
pc.peers[peerID] = p | ||
} | ||
|
||
p.connCount++ | ||
|
||
pc.muStore.Unlock() | ||
} | ||
|
||
func (pc *peerCache) Disconnected(net inet.Network, c inet.Conn) { | ||
pc.muStore.Lock() | ||
|
||
peerID := c.RemotePeer() | ||
if p, ok := pc.peers[peerID]; ok { | ||
p.connCount-- | ||
|
||
if p.connCount == 0 { | ||
if p.key != "" { | ||
delete(pc.store, p.key) | ||
} | ||
|
||
delete(pc.peers, peerID) | ||
} | ||
} | ||
|
||
pc.muStore.Unlock() | ||
} | ||
|
||
// Listen is no-op in this implementation. | ||
func (pc *peerCache) Listen(n inet.Network, addr ma.Multiaddr) { | ||
} | ||
|
||
// ListenClose is no-op in this implementation. | ||
func (pc *peerCache) ListenClose(n inet.Network, addr ma.Multiaddr) { | ||
|
||
} | ||
|
||
// OpenedStream is no-op in this implementation. | ||
func (pc *peerCache) OpenedStream(n inet.Network, s inet.Stream) { | ||
|
||
} | ||
|
||
// ClosedStream is no-op in this implementation. | ||
func (pc *peerCache) ClosedStream(n inet.Network, s inet.Stream) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.