Skip to content

Commit ca9b09f

Browse files
authored
Revert "rpc: attempt to fix ping/pong logic race (ethereum#27733)"
This reverts commit 7588f79.
1 parent f05afac commit ca9b09f

File tree

1 file changed

+17
-26
lines changed

1 file changed

+17
-26
lines changed

rpc/websocket.go

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -278,21 +278,24 @@ type websocketCodec struct {
278278
conn *websocket.Conn
279279
info PeerInfo
280280

281-
wg sync.WaitGroup
282-
pingReset chan struct{}
283-
pongReceived chan struct{}
281+
wg sync.WaitGroup
282+
pingReset chan struct{}
284283
}
285284

286285
func newWebsocketCodec(conn *websocket.Conn, host string, req http.Header) ServerCodec {
287286
conn.SetReadLimit(wsMessageSizeLimit)
287+
conn.SetPongHandler(func(appData string) error {
288+
conn.SetReadDeadline(time.Time{})
289+
return nil
290+
})
291+
288292
encode := func(v interface{}, isErrorResponse bool) error {
289293
return conn.WriteJSON(v)
290294
}
291295
wc := &websocketCodec{
292-
jsonCodec: NewFuncCodec(conn, encode, conn.ReadJSON).(*jsonCodec),
293-
conn: conn,
294-
pingReset: make(chan struct{}, 1),
295-
pongReceived: make(chan struct{}),
296+
jsonCodec: NewFuncCodec(conn, encode, conn.ReadJSON).(*jsonCodec),
297+
conn: conn,
298+
pingReset: make(chan struct{}, 1),
296299
info: PeerInfo{
297300
Transport: "ws",
298301
RemoteAddr: conn.RemoteAddr().String(),
@@ -303,13 +306,6 @@ func newWebsocketCodec(conn *websocket.Conn, host string, req http.Header) Serve
303306
wc.info.HTTP.Origin = req.Get("Origin")
304307
wc.info.HTTP.UserAgent = req.Get("User-Agent")
305308
// Start pinger.
306-
conn.SetPongHandler(func(appData string) error {
307-
select {
308-
case wc.pongReceived <- struct{}{}:
309-
case <-wc.closed():
310-
}
311-
return nil
312-
})
313309
wc.wg.Add(1)
314310
go wc.pingLoop()
315311
return wc
@@ -338,31 +334,26 @@ func (wc *websocketCodec) writeJSON(ctx context.Context, v interface{}, isError
338334

339335
// pingLoop sends periodic ping frames when the connection is idle.
340336
func (wc *websocketCodec) pingLoop() {
341-
var pingTimer = time.NewTimer(wsPingInterval)
337+
var timer = time.NewTimer(wsPingInterval)
342338
defer wc.wg.Done()
343-
defer pingTimer.Stop()
339+
defer timer.Stop()
344340

345341
for {
346342
select {
347343
case <-wc.closed():
348344
return
349-
350345
case <-wc.pingReset:
351-
if !pingTimer.Stop() {
352-
<-pingTimer.C
346+
if !timer.Stop() {
347+
<-timer.C
353348
}
354-
pingTimer.Reset(wsPingInterval)
355-
356-
case <-pingTimer.C:
349+
timer.Reset(wsPingInterval)
350+
case <-timer.C:
357351
wc.jsonCodec.encMu.Lock()
358352
wc.conn.SetWriteDeadline(time.Now().Add(wsPingWriteTimeout))
359353
wc.conn.WriteMessage(websocket.PingMessage, nil)
360354
wc.conn.SetReadDeadline(time.Now().Add(wsPongTimeout))
361355
wc.jsonCodec.encMu.Unlock()
362-
pingTimer.Reset(wsPingInterval)
363-
364-
case <-wc.pongReceived:
365-
wc.conn.SetReadDeadline(time.Time{})
356+
timer.Reset(wsPingInterval)
366357
}
367358
}
368359
}

0 commit comments

Comments
 (0)