mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-04 23:40:26 +08:00
feat(icmp): use fifo instead of pool
This commit is contained in:
@@ -27,20 +27,51 @@ var (
|
|||||||
echoid = os.Getpid()
|
echoid = os.Getpid()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// seqFIFO is a FIFO queue that generates new sequence numbers when empty.
|
||||||
|
type seqFIFO struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
q []uintptr
|
||||||
|
next *atomic.Uintptr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *seqFIFO) Get() uintptr {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
if len(f.q) > 0 {
|
||||||
|
v := f.q[0]
|
||||||
|
copy(f.q, f.q[1:])
|
||||||
|
f.q = f.q[:len(f.q)-1]
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return f.next.Add(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *seqFIFO) Put(v uintptr) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
if len(f.q) == 0 {
|
||||||
|
f.q = make([]uintptr, 1, 128)
|
||||||
|
f.q[0] = v
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(f.q) < cap(f.q) {
|
||||||
|
f.q = append(f.q, v)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
copy(f.q, f.q[1:])
|
||||||
|
f.q[len(f.q)-1] = v
|
||||||
|
}
|
||||||
|
|
||||||
// peerState holds per-peer ICMP echo state within a Conn.
|
// peerState holds per-peer ICMP echo state within a Conn.
|
||||||
type peerState struct {
|
type peerState struct {
|
||||||
id int
|
id int
|
||||||
seq atomic.Uintptr
|
seq atomic.Uintptr
|
||||||
seqpool *sync.Pool
|
seqfifo seqFIFO
|
||||||
}
|
}
|
||||||
|
|
||||||
func newPeerState() *peerState {
|
func newPeerState() *peerState {
|
||||||
ps := &peerState{}
|
ps := &peerState{}
|
||||||
ps.seqpool = &sync.Pool{
|
ps.seqfifo.next = &ps.seq
|
||||||
New: func() any {
|
|
||||||
return ps.seq.Add(1)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
return ps
|
return ps
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,7 +211,7 @@ func (conn *Conn) ReadFromPeer(b []byte) (n int, ep p2p.EndPoint, err error) {
|
|||||||
ps := conn.getOrCreatePeerState(ipaddr)
|
ps := conn.getOrCreatePeerState(ipaddr)
|
||||||
ps.id = body.ID
|
ps.id = body.ID
|
||||||
ps.seq.Store(uintptr(body.Seq))
|
ps.seq.Store(uintptr(body.Seq))
|
||||||
ps.seqpool.Put(uintptr(body.Seq))
|
ps.seqfifo.Put(uintptr(body.Seq))
|
||||||
}
|
}
|
||||||
n = copy(b, body.Data)
|
n = copy(b, body.Data)
|
||||||
if config.ShowDebugLog {
|
if config.ShowDebugLog {
|
||||||
@@ -197,7 +228,7 @@ func (conn *Conn) WriteToPeer(b []byte, ep p2p.EndPoint) (int, error) {
|
|||||||
}
|
}
|
||||||
addr := (*netip.Addr)(icmpep)
|
addr := (*netip.Addr)(icmpep)
|
||||||
ps := conn.getOrCreatePeerState(*addr)
|
ps := conn.getOrCreatePeerState(*addr)
|
||||||
seq := int(ps.seqpool.Get().(uintptr))
|
seq := int(ps.seqfifo.Get())
|
||||||
id := ps.id
|
id := ps.id
|
||||||
isrequest := id == 0
|
isrequest := id == 0
|
||||||
if isrequest {
|
if isrequest {
|
||||||
|
|||||||
Reference in New Issue
Block a user