Skip to content

Commit

Permalink
update dht record age and cache no expiration (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
godeamon authored Nov 23, 2023
1 parent 7a8dc17 commit d295a7a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions bcs/network/p2pv2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
6 changes: 4 additions & 2 deletions bcs/network/p2pv2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"math"
"strings"
"time"

Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit d295a7a

Please sign in to comment.