diff --git a/bcs/network/p2pv2/client.go b/bcs/network/p2pv2/client.go index 6b08ae76..3043fd7f 100644 --- a/bcs/network/p2pv2/client.go +++ b/bcs/network/p2pv2/client.go @@ -7,6 +7,7 @@ import ( "sync" "time" + "github.com/patrickmn/go-cache" "github.com/xuperchain/xupercore/lib/metrics" xctx "github.com/xuperchain/xupercore/kernel/common/xcontext" @@ -301,7 +302,8 @@ func (p *P2PServerV2) GetPeerIdByAccount(account string) (peer.ID, error) { return "", fmt.Errorf("address error: %s, address=%s", err, value) } - // 当前节点缓存 account=>peerID 1小时,dht 中默认是 36h,如果超过 36h 可能导致节点重启时不能参与共识。 - p.accounts.Set(key, peerID, time.Hour*1) + // 此处设置不过期,在 putValue 时设置过期时间为最大值及不会过期 + // 即使重启程序,也不会影响从网络中查询数据。 + p.accounts.Set(key, peerID, cache.NoExpiration) return peerID, nil } diff --git a/bcs/network/p2pv2/server.go b/bcs/network/p2pv2/server.go index 15346195..3be29a73 100644 --- a/bcs/network/p2pv2/server.go +++ b/bcs/network/p2pv2/server.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "math" "strings" "time" @@ -132,6 +133,7 @@ func (p *P2PServerV2) Init(ctx *netCtx.NetCtx) error { // dht dhtOpts := []dht.Option{ + dht.MaxRecordAge(math.MaxInt64), // record never expire, default 36 hours. dht.Mode(dht.ModeServer), dht.RoutingTableRefreshPeriod(10 * time.Second), dht.ProtocolPrefix(protocol.ID(prefix)), @@ -383,8 +385,8 @@ func (p *P2PServerV2) PeerInfo() pb.PeerInfo { p.log.Warn("get account error", "peerID", peerID, "error", err) } else { accountStr = string(account) - // 更新缓存,过期时间设置为4小时 - p.peerIDs.Set(key, accountStr, time.Hour*4) + // 更新缓存,缓存数据不过期 + p.peerIDs.Set(key, accountStr, cache.NoExpiration) } }