lotus/lib/jsonrpc/websocket.go

195 lines
3.6 KiB
Go
Raw Normal View History

2019-07-12 17:12:38 +00:00
package jsonrpc
import (
"context"
"encoding/json"
"errors"
"io"
2019-07-15 16:21:48 +00:00
"io/ioutil"
"sync"
2019-07-12 17:12:38 +00:00
"github.com/gorilla/websocket"
)
2019-07-15 16:21:48 +00:00
const wsCancel = "xrpc.cancel"
2019-07-12 17:12:38 +00:00
type frame struct {
// common
2019-07-15 16:32:43 +00:00
Jsonrpc string `json:"jsonrpc"`
ID *int64 `json:"id,omitempty"`
2019-07-12 17:12:38 +00:00
// request
2019-07-15 16:32:43 +00:00
Method string `json:"method,omitempty"`
Params []param `json:"params,omitempty"`
2019-07-12 17:12:38 +00:00
// response
2019-07-15 16:32:43 +00:00
Result result `json:"result,omitempty"`
Error *respError `json:"error,omitempty"`
2019-07-12 17:12:38 +00:00
}
func handleWsConn(ctx context.Context, conn *websocket.Conn, handler handlers, requests <-chan clientRequest, stop <-chan struct{}) {
incoming := make(chan io.Reader)
2019-07-13 12:44:20 +00:00
var incErr error
2019-07-12 17:12:38 +00:00
nextMessage := func() {
mtype, r, err := conn.NextReader()
if err != nil {
2019-07-13 12:44:20 +00:00
incErr = err
close(incoming)
2019-07-12 17:12:38 +00:00
return
}
if mtype != websocket.BinaryMessage && mtype != websocket.TextMessage {
2019-07-13 12:44:20 +00:00
incErr = errors.New("unsupported message type")
close(incoming)
2019-07-12 17:12:38 +00:00
return
}
incoming <- r
}
2019-07-15 16:21:48 +00:00
var writeLk sync.Mutex
nextWriter := func(cb func(io.Writer)) {
writeLk.Lock()
defer writeLk.Unlock()
wcl, err := conn.NextWriter(websocket.TextMessage)
if err != nil {
log.Error("handle me:", err)
return
}
cb(wcl)
if err := wcl.Close(); err != nil {
log.Error("handle me:", err)
return
}
}
2019-07-12 17:12:38 +00:00
go nextMessage()
inflight := map[int64]clientRequest{}
2019-07-15 16:21:48 +00:00
handling := map[int64]context.CancelFunc{}
var handlingLk sync.Mutex
defer func() {
for id, req := range inflight {
req.ready <- clientResponse{
Jsonrpc: "2.0",
ID: id,
Error: &respError{
Message: "handler: websocket connection closed",
},
}
handlingLk.Lock()
for _, cancel := range handling {
cancel()
}
handlingLk.Unlock()
}
}()
2019-07-15 16:21:48 +00:00
cancelCtx := func(req frame) {
if req.ID != nil {
log.Warnf("%s call with ID set, won't respond", wsCancel)
}
var id int64
if err := json.Unmarshal(req.Params[0].data, &id); err != nil {
log.Error("handle me:", err)
return
}
handlingLk.Lock()
defer handlingLk.Unlock()
cf, ok := handling[id]
if ok {
cf()
}
}
2019-07-12 17:12:38 +00:00
for {
select {
2019-07-13 12:44:20 +00:00
case r, ok := <-incoming:
if !ok {
if incErr != nil {
log.Debugf("websocket error", "error", incErr)
}
return // remote closed
}
2019-07-12 17:12:38 +00:00
var frame frame
if err := json.NewDecoder(r).Decode(&frame); err != nil {
log.Error("handle me:", err)
return
}
2019-07-15 16:21:48 +00:00
switch frame.Method {
case "": // Response to our call
2019-07-12 17:12:38 +00:00
req, ok := inflight[*frame.ID]
if !ok {
log.Error("client got unknown ID in response")
continue
}
req.ready <- clientResponse{
Jsonrpc: frame.Jsonrpc,
Result: frame.Result,
ID: *frame.ID,
Error: frame.Error,
}
delete(inflight, *frame.ID)
2019-07-15 16:21:48 +00:00
case wsCancel:
cancelCtx(frame)
default: // Remote call
req := request{
Jsonrpc: frame.Jsonrpc,
ID: frame.ID,
Method: frame.Method,
Params: frame.Params,
}
ctx, cf := context.WithCancel(ctx)
nw := func(cb func(io.Writer)) {
cb(ioutil.Discard)
}
2019-07-15 16:32:43 +00:00
done := cf
2019-07-15 16:21:48 +00:00
if frame.ID != nil {
nw = nextWriter
handlingLk.Lock()
handling[*frame.ID] = cf
handlingLk.Unlock()
done = func() {
handlingLk.Lock()
defer handlingLk.Unlock()
cf()
delete(handling, *frame.ID)
}
}
go handler.handle(ctx, req, nw, rpcError, done)
2019-07-12 17:12:38 +00:00
}
go nextMessage()
case req := <-requests:
2019-07-15 16:21:48 +00:00
if req.req.ID != nil {
inflight[*req.req.ID] = req
}
2019-07-12 17:12:38 +00:00
if err := conn.WriteJSON(req.req); err != nil {
log.Error("handle me:", err)
return
}
case <-stop:
2019-07-13 12:44:20 +00:00
if err := conn.Close(); err != nil {
log.Debugf("websocket close error", "error", err)
}
2019-07-12 17:12:38 +00:00
return
}
}
}