mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-24 21:00:49 +08:00
fix(p2p): tcp fast fail to main queue
This commit is contained in:
@@ -100,13 +100,14 @@ type subconn struct {
|
|||||||
|
|
||||||
// Conn 伪装成无状态的有状态连接
|
// Conn 伪装成无状态的有状态连接
|
||||||
type Conn struct {
|
type Conn struct {
|
||||||
addr *EndPoint
|
addr *EndPoint
|
||||||
lstn *net.TCPListener
|
lstn *net.TCPListener
|
||||||
peers *ttl.Cache[string, *net.TCPConn]
|
peers *ttl.Cache[string, *net.TCPConn]
|
||||||
recv chan *connrecv
|
recv chan *connrecv
|
||||||
cplk *sync.Mutex
|
cplk *sync.Mutex
|
||||||
sblk *sync.RWMutex
|
sblk *sync.RWMutex
|
||||||
subs []*subconn
|
subs []*subconn
|
||||||
|
suberr bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *Conn) accept() {
|
func (conn *Conn) accept() {
|
||||||
@@ -440,8 +441,13 @@ func (conn *Conn) WriteToPeer(b []byte, ep p2p.EndPoint) (n int, err error) {
|
|||||||
if len(b) >= 65536 {
|
if len(b) >= 65536 {
|
||||||
return 0, errors.New("data size " + strconv.Itoa(len(b)) + " is too large")
|
return 0, errors.New("data size " + strconv.Itoa(len(b)) + " is too large")
|
||||||
}
|
}
|
||||||
if !conn.cplk.TryLock() {
|
if !conn.suberr && !conn.cplk.TryLock() {
|
||||||
return conn.writeToPeer(b, tcpep, true)
|
n, err = conn.writeToPeer(b, tcpep, true) // try sub write
|
||||||
|
if err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
conn.suberr = true // fast fail
|
||||||
|
conn.cplk.Lock() // add to main queue
|
||||||
}
|
}
|
||||||
defer conn.cplk.Unlock()
|
defer conn.cplk.Unlock()
|
||||||
return conn.writeToPeer(b, tcpep, false)
|
return conn.writeToPeer(b, tcpep, false)
|
||||||
|
|||||||
Reference in New Issue
Block a user