mirror of
https://github.com/fluencelabs/tendermint
synced 2025-05-17 00:51:20 +00:00
fix websocket handler
This commit is contained in:
parent
e702303e16
commit
8057fb7125
@ -225,6 +225,7 @@ type WSConnection struct {
|
|||||||
id string
|
id string
|
||||||
wsConn *websocket.Conn
|
wsConn *websocket.Conn
|
||||||
writeChan chan WSResponse
|
writeChan chan WSResponse
|
||||||
|
quitChan chan struct{}
|
||||||
failedSends uint
|
failedSends uint
|
||||||
started uint32
|
started uint32
|
||||||
stopped uint32
|
stopped uint32
|
||||||
@ -238,6 +239,7 @@ func NewWSConnection(wsConn *websocket.Conn) *WSConnection {
|
|||||||
id: wsConn.RemoteAddr().String(),
|
id: wsConn.RemoteAddr().String(),
|
||||||
wsConn: wsConn,
|
wsConn: wsConn,
|
||||||
writeChan: make(chan WSResponse, WriteChanBufferSize), // buffered. we keep track when its full
|
writeChan: make(chan WSResponse, WriteChanBufferSize), // buffered. we keep track when its full
|
||||||
|
quitChan: make(chan struct{}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -256,8 +258,10 @@ func (con *WSConnection) Start(evsw *events.EventSwitch) {
|
|||||||
// close the connection
|
// close the connection
|
||||||
func (con *WSConnection) Stop() {
|
func (con *WSConnection) Stop() {
|
||||||
if atomic.CompareAndSwapUint32(&con.stopped, 0, 1) {
|
if atomic.CompareAndSwapUint32(&con.stopped, 0, 1) {
|
||||||
con.wsConn.Close()
|
close(con.quitChan)
|
||||||
close(con.writeChan)
|
// the write loop closes the websocket connection
|
||||||
|
// when it exits its loop, and the read loop
|
||||||
|
// closes the writeChan
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -277,6 +281,7 @@ func (con *WSConnection) safeWrite(resp WSResponse) {
|
|||||||
|
|
||||||
// read from the socket and subscribe to or unsubscribe from events
|
// read from the socket and subscribe to or unsubscribe from events
|
||||||
func (con *WSConnection) read() {
|
func (con *WSConnection) read() {
|
||||||
|
defer close(con.writeChan)
|
||||||
reaper := time.Tick(time.Second * WSConnectionReaperSeconds)
|
reaper := time.Tick(time.Second * WSConnectionReaperSeconds)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@ -322,32 +327,30 @@ func (con *WSConnection) read() {
|
|||||||
default:
|
default:
|
||||||
con.safeWrite(WSResponse{Error: "Unknown request type: " + req.Type})
|
con.safeWrite(WSResponse{Error: "Unknown request type: " + req.Type})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// receives on a write channel and writes out on the socket
|
// receives on a write channel and writes out on the socket
|
||||||
func (con *WSConnection) write() {
|
func (con *WSConnection) write() {
|
||||||
|
defer con.wsConn.Close()
|
||||||
n, err := new(int64), new(error)
|
n, err := new(int64), new(error)
|
||||||
for {
|
for {
|
||||||
msg, more := <-con.writeChan
|
select {
|
||||||
if !more {
|
case msg := <-con.writeChan:
|
||||||
// the channel was closed, so ensure
|
buf := new(bytes.Buffer)
|
||||||
// connection is stopped and return
|
binary.WriteJSON(msg, buf, n, err)
|
||||||
con.Stop()
|
if *err != nil {
|
||||||
return
|
log.Error("Failed to marshal WSResponse to JSON", "error", err)
|
||||||
}
|
} else {
|
||||||
buf := new(bytes.Buffer)
|
if err := con.wsConn.WriteMessage(websocket.TextMessage, buf.Bytes()); err != nil {
|
||||||
binary.WriteJSON(msg, buf, n, err)
|
log.Error("Failed to write response on websocket", "error", err)
|
||||||
if *err != nil {
|
con.Stop()
|
||||||
log.Error("Failed to write JSON WSResponse", "error", err)
|
return
|
||||||
} else {
|
}
|
||||||
if err := con.wsConn.WriteMessage(websocket.TextMessage, buf.Bytes()); err != nil {
|
|
||||||
log.Error("Failed to write response on websocket", "error", err)
|
|
||||||
con.Stop()
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
case <-con.quitChan:
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user