p2p/enode: use atomic.Pointer in LocalNode (#32360)

This commit is contained in:
cui 2025-08-07 21:03:18 +08:00 committed by GitHub
parent f9f85d0227
commit e979438a55
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -45,7 +45,7 @@ const (
// current process. Setting ENR entries via the Set method updates the record. A new version // current process. Setting ENR entries via the Set method updates the record. A new version
// of the record is signed on demand when the Node method is called. // of the record is signed on demand when the Node method is called.
type LocalNode struct { type LocalNode struct {
cur atomic.Value // holds a non-nil node pointer while the record is up-to-date cur atomic.Pointer[Node] // holds a non-nil node pointer while the record is up-to-date
id ID id ID
key *ecdsa.PrivateKey key *ecdsa.PrivateKey
@ -82,7 +82,7 @@ func NewLocalNode(db *DB, key *ecdsa.PrivateKey) *LocalNode {
} }
ln.seq = db.localSeq(ln.id) ln.seq = db.localSeq(ln.id)
ln.update = time.Now() ln.update = time.Now()
ln.cur.Store((*Node)(nil)) ln.cur.Store(nil)
return ln return ln
} }
@ -94,7 +94,7 @@ func (ln *LocalNode) Database() *DB {
// Node returns the current version of the local node record. // Node returns the current version of the local node record.
func (ln *LocalNode) Node() *Node { func (ln *LocalNode) Node() *Node {
// If we have a valid record, return that // If we have a valid record, return that
n := ln.cur.Load().(*Node) n := ln.cur.Load()
if n != nil { if n != nil {
return n return n
} }
@ -105,7 +105,7 @@ func (ln *LocalNode) Node() *Node {
// Double check the current record, since multiple goroutines might be waiting // Double check the current record, since multiple goroutines might be waiting
// on the write mutex. // on the write mutex.
if n = ln.cur.Load().(*Node); n != nil { if n = ln.cur.Load(); n != nil {
return n return n
} }
@ -121,7 +121,7 @@ func (ln *LocalNode) Node() *Node {
ln.sign() ln.sign()
ln.update = time.Now() ln.update = time.Now()
return ln.cur.Load().(*Node) return ln.cur.Load()
} }
// Seq returns the current sequence number of the local node record. // Seq returns the current sequence number of the local node record.
@ -276,11 +276,11 @@ func (e *lnEndpoint) get() (newIP net.IP, newPort uint16) {
} }
func (ln *LocalNode) invalidate() { func (ln *LocalNode) invalidate() {
ln.cur.Store((*Node)(nil)) ln.cur.Store(nil)
} }
func (ln *LocalNode) sign() { func (ln *LocalNode) sign() {
if n := ln.cur.Load().(*Node); n != nil { if n := ln.cur.Load(); n != nil {
return // no changes return // no changes
} }