forked from cerc-io/plugeth
ethstats: split read and write lock, otherwise they'll lock up
This commit is contained in:
parent
cbbc54c495
commit
9f45d6efae
@ -99,6 +99,7 @@ type Service struct {
|
|||||||
|
|
||||||
// connWrapper is a wrapper to prevent concurrent-write or concurrent-read on the
|
// connWrapper is a wrapper to prevent concurrent-write or concurrent-read on the
|
||||||
// websocket.
|
// websocket.
|
||||||
|
//
|
||||||
// From Gorilla websocket docs:
|
// From Gorilla websocket docs:
|
||||||
// Connections support one concurrent reader and one concurrent writer.
|
// Connections support one concurrent reader and one concurrent writer.
|
||||||
// Applications are responsible for ensuring that no more than one goroutine calls the write methods
|
// Applications are responsible for ensuring that no more than one goroutine calls the write methods
|
||||||
@ -107,11 +108,11 @@ type Service struct {
|
|||||||
// - NextReader, SetReadDeadline, ReadMessage, ReadJSON, SetPongHandler, SetPingHandler
|
// - NextReader, SetReadDeadline, ReadMessage, ReadJSON, SetPongHandler, SetPingHandler
|
||||||
// concurrently.
|
// concurrently.
|
||||||
// The Close and WriteControl methods can be called concurrently with all other methods.
|
// The Close and WriteControl methods can be called concurrently with all other methods.
|
||||||
//
|
|
||||||
// The connWrapper uses a single mutex for both reading and writing.
|
|
||||||
type connWrapper struct {
|
type connWrapper struct {
|
||||||
conn *websocket.Conn
|
conn *websocket.Conn
|
||||||
mu sync.Mutex
|
|
||||||
|
rlock sync.Mutex
|
||||||
|
wlock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func newConnectionWrapper(conn *websocket.Conn) *connWrapper {
|
func newConnectionWrapper(conn *websocket.Conn) *connWrapper {
|
||||||
@ -120,15 +121,17 @@ func newConnectionWrapper(conn *websocket.Conn) *connWrapper {
|
|||||||
|
|
||||||
// WriteJSON wraps corresponding method on the websocket but is safe for concurrent calling
|
// WriteJSON wraps corresponding method on the websocket but is safe for concurrent calling
|
||||||
func (w *connWrapper) WriteJSON(v interface{}) error {
|
func (w *connWrapper) WriteJSON(v interface{}) error {
|
||||||
w.mu.Lock()
|
w.wlock.Lock()
|
||||||
defer w.mu.Unlock()
|
defer w.wlock.Unlock()
|
||||||
|
|
||||||
return w.conn.WriteJSON(v)
|
return w.conn.WriteJSON(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadJSON wraps corresponding method on the websocket but is safe for concurrent calling
|
// ReadJSON wraps corresponding method on the websocket but is safe for concurrent calling
|
||||||
func (w *connWrapper) ReadJSON(v interface{}) error {
|
func (w *connWrapper) ReadJSON(v interface{}) error {
|
||||||
w.mu.Lock()
|
w.rlock.Lock()
|
||||||
defer w.mu.Unlock()
|
defer w.rlock.Unlock()
|
||||||
|
|
||||||
return w.conn.ReadJSON(v)
|
return w.conn.ReadJSON(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -275,6 +278,7 @@ func (s *Service) loop() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
go s.readLoop(conn)
|
go s.readLoop(conn)
|
||||||
|
|
||||||
// Send the initial stats so our node looks decent from the get go
|
// Send the initial stats so our node looks decent from the get go
|
||||||
if err = s.report(conn); err != nil {
|
if err = s.report(conn); err != nil {
|
||||||
log.Warn("Initial stats report failed", "err", err)
|
log.Warn("Initial stats report failed", "err", err)
|
||||||
|
Loading…
Reference in New Issue
Block a user