mirror of
https://github.com/sigcn/pg.git
synced 2025-10-04 17:22:39 +08:00
p2p: add cache for func PacketConn.relayPeer
This commit is contained in:
2
cache/cache.go
vendored
2
cache/cache.go
vendored
@@ -33,7 +33,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
defaultCache = lru.New[string, *CacheValue[any]](1024)
|
defaultCache = lru.New[string, *CacheValue[any]](2048)
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadTTL load value by key from default cache pool
|
// LoadTTL load value by key from default cache pool
|
||||||
|
@@ -13,6 +13,7 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/sigcn/pg/cache"
|
||||||
"github.com/sigcn/pg/cache/lru"
|
"github.com/sigcn/pg/cache/lru"
|
||||||
"github.com/sigcn/pg/disco"
|
"github.com/sigcn/pg/disco"
|
||||||
"github.com/sigcn/pg/disco/udp"
|
"github.com/sigcn/pg/disco/udp"
|
||||||
@@ -265,6 +266,7 @@ func (c *PacketConn) PeerMeta(peerID disco.PeerID) url.Values {
|
|||||||
|
|
||||||
// relayPeer find the suitable relay peer
|
// relayPeer find the suitable relay peer
|
||||||
func (c *PacketConn) relayPeer(peerID disco.PeerID) disco.PeerID {
|
func (c *PacketConn) relayPeer(peerID disco.PeerID) disco.PeerID {
|
||||||
|
selectRelayPeer := func(_ string) disco.PeerID {
|
||||||
peers := c.PeerStore().Peers()
|
peers := c.PeerStore().Peers()
|
||||||
for range len(peers) {
|
for range len(peers) {
|
||||||
index := c.relayPeerIndex.Add(1) % uint64(len(peers))
|
index := c.relayPeerIndex.Add(1) % uint64(len(peers))
|
||||||
@@ -287,6 +289,8 @@ func (c *PacketConn) relayPeer(peerID disco.PeerID) disco.PeerID {
|
|||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
return cache.LoadTTL(peerID.String(), time.Millisecond, selectRelayPeer)
|
||||||
|
}
|
||||||
|
|
||||||
// networkChangeDetect listen network change and restart udp and websocket listener
|
// networkChangeDetect listen network change and restart udp and websocket listener
|
||||||
func (c *PacketConn) networkChangeDetect() {
|
func (c *PacketConn) networkChangeDetect() {
|
||||||
|
Reference in New Issue
Block a user