mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
Revert "rpc: attempt to fix ping/pong logic race (#27733)"
This reverts commit 7588f79bd8.
This commit is contained in:
parent
f05afac643
commit
ca9b09f72b
1 changed files with 17 additions and 26 deletions
|
|
@ -278,21 +278,24 @@ type websocketCodec struct {
|
||||||
conn *websocket.Conn
|
conn *websocket.Conn
|
||||||
info PeerInfo
|
info PeerInfo
|
||||||
|
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
pingReset chan struct{}
|
pingReset chan struct{}
|
||||||
pongReceived chan struct{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newWebsocketCodec(conn *websocket.Conn, host string, req http.Header) ServerCodec {
|
func newWebsocketCodec(conn *websocket.Conn, host string, req http.Header) ServerCodec {
|
||||||
conn.SetReadLimit(wsMessageSizeLimit)
|
conn.SetReadLimit(wsMessageSizeLimit)
|
||||||
|
conn.SetPongHandler(func(appData string) error {
|
||||||
|
conn.SetReadDeadline(time.Time{})
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
encode := func(v interface{}, isErrorResponse bool) error {
|
encode := func(v interface{}, isErrorResponse bool) error {
|
||||||
return conn.WriteJSON(v)
|
return conn.WriteJSON(v)
|
||||||
}
|
}
|
||||||
wc := &websocketCodec{
|
wc := &websocketCodec{
|
||||||
jsonCodec: NewFuncCodec(conn, encode, conn.ReadJSON).(*jsonCodec),
|
jsonCodec: NewFuncCodec(conn, encode, conn.ReadJSON).(*jsonCodec),
|
||||||
conn: conn,
|
conn: conn,
|
||||||
pingReset: make(chan struct{}, 1),
|
pingReset: make(chan struct{}, 1),
|
||||||
pongReceived: make(chan struct{}),
|
|
||||||
info: PeerInfo{
|
info: PeerInfo{
|
||||||
Transport: "ws",
|
Transport: "ws",
|
||||||
RemoteAddr: conn.RemoteAddr().String(),
|
RemoteAddr: conn.RemoteAddr().String(),
|
||||||
|
|
@ -303,13 +306,6 @@ func newWebsocketCodec(conn *websocket.Conn, host string, req http.Header) Serve
|
||||||
wc.info.HTTP.Origin = req.Get("Origin")
|
wc.info.HTTP.Origin = req.Get("Origin")
|
||||||
wc.info.HTTP.UserAgent = req.Get("User-Agent")
|
wc.info.HTTP.UserAgent = req.Get("User-Agent")
|
||||||
// Start pinger.
|
// Start pinger.
|
||||||
conn.SetPongHandler(func(appData string) error {
|
|
||||||
select {
|
|
||||||
case wc.pongReceived <- struct{}{}:
|
|
||||||
case <-wc.closed():
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
wc.wg.Add(1)
|
wc.wg.Add(1)
|
||||||
go wc.pingLoop()
|
go wc.pingLoop()
|
||||||
return wc
|
return wc
|
||||||
|
|
@ -338,31 +334,26 @@ func (wc *websocketCodec) writeJSON(ctx context.Context, v interface{}, isError
|
||||||
|
|
||||||
// pingLoop sends periodic ping frames when the connection is idle.
|
// pingLoop sends periodic ping frames when the connection is idle.
|
||||||
func (wc *websocketCodec) pingLoop() {
|
func (wc *websocketCodec) pingLoop() {
|
||||||
var pingTimer = time.NewTimer(wsPingInterval)
|
var timer = time.NewTimer(wsPingInterval)
|
||||||
defer wc.wg.Done()
|
defer wc.wg.Done()
|
||||||
defer pingTimer.Stop()
|
defer timer.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-wc.closed():
|
case <-wc.closed():
|
||||||
return
|
return
|
||||||
|
|
||||||
case <-wc.pingReset:
|
case <-wc.pingReset:
|
||||||
if !pingTimer.Stop() {
|
if !timer.Stop() {
|
||||||
<-pingTimer.C
|
<-timer.C
|
||||||
}
|
}
|
||||||
pingTimer.Reset(wsPingInterval)
|
timer.Reset(wsPingInterval)
|
||||||
|
case <-timer.C:
|
||||||
case <-pingTimer.C:
|
|
||||||
wc.jsonCodec.encMu.Lock()
|
wc.jsonCodec.encMu.Lock()
|
||||||
wc.conn.SetWriteDeadline(time.Now().Add(wsPingWriteTimeout))
|
wc.conn.SetWriteDeadline(time.Now().Add(wsPingWriteTimeout))
|
||||||
wc.conn.WriteMessage(websocket.PingMessage, nil)
|
wc.conn.WriteMessage(websocket.PingMessage, nil)
|
||||||
wc.conn.SetReadDeadline(time.Now().Add(wsPongTimeout))
|
wc.conn.SetReadDeadline(time.Now().Add(wsPongTimeout))
|
||||||
wc.jsonCodec.encMu.Unlock()
|
wc.jsonCodec.encMu.Unlock()
|
||||||
pingTimer.Reset(wsPingInterval)
|
timer.Reset(wsPingInterval)
|
||||||
|
|
||||||
case <-wc.pongReceived:
|
|
||||||
wc.conn.SetReadDeadline(time.Time{})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue