2019-07-03 17:39:07 +00:00
|
|
|
package jsonrpc
|
2019-06-28 13:49:34 +00:00
|
|
|
|
|
|
|
import (
|
2019-09-27 11:37:44 +00:00
|
|
|
"container/list"
|
2019-06-28 14:53:01 +00:00
|
|
|
"context"
|
2019-07-25 20:19:43 +00:00
|
|
|
"encoding/base64"
|
2019-06-28 13:49:34 +00:00
|
|
|
"encoding/json"
|
2019-06-28 14:05:10 +00:00
|
|
|
"fmt"
|
2019-07-23 18:49:09 +00:00
|
|
|
"net/http"
|
2019-06-28 13:49:34 +00:00
|
|
|
"reflect"
|
2019-09-27 11:37:44 +00:00
|
|
|
"sync"
|
2019-07-03 14:32:31 +00:00
|
|
|
"sync/atomic"
|
2019-07-08 19:07:16 +00:00
|
|
|
|
2019-07-12 15:29:41 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2019-07-08 19:07:16 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2019-07-25 20:19:43 +00:00
|
|
|
"go.opencensus.io/trace"
|
|
|
|
"go.opencensus.io/trace/propagation"
|
2019-07-18 10:46:32 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-06-28 13:49:34 +00:00
|
|
|
)
|
|
|
|
|
2019-07-08 19:07:16 +00:00
|
|
|
var log = logging.Logger("rpc")
|
|
|
|
|
2019-06-28 14:53:01 +00:00
|
|
|
var (
|
2019-07-01 20:00:22 +00:00
|
|
|
errorType = reflect.TypeOf(new(error)).Elem()
|
2019-06-28 14:53:01 +00:00
|
|
|
contextType = reflect.TypeOf(new(context.Context)).Elem()
|
|
|
|
)
|
|
|
|
|
2019-07-02 19:08:30 +00:00
|
|
|
// ErrClient is an error which occurred on the client side the library
|
2019-06-28 14:05:10 +00:00
|
|
|
type ErrClient struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ErrClient) Error() string {
|
|
|
|
return fmt.Sprintf("RPC client error: %s", e.err)
|
|
|
|
}
|
|
|
|
|
2019-07-02 17:45:03 +00:00
|
|
|
// Unwrap unwraps the actual error
|
2019-06-28 14:05:10 +00:00
|
|
|
func (e *ErrClient) Unwrap(err error) error {
|
|
|
|
return e.err
|
|
|
|
}
|
|
|
|
|
2019-06-28 13:49:34 +00:00
|
|
|
type clientResponse struct {
|
2019-07-22 20:28:40 +00:00
|
|
|
Jsonrpc string `json:"jsonrpc"`
|
|
|
|
Result json.RawMessage `json:"result"`
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
Error *respError `json:"error,omitempty"`
|
2019-06-28 13:49:34 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 12:20:57 +00:00
|
|
|
type makeChanSink func() (context.Context, func([]byte, bool))
|
|
|
|
|
2019-07-12 15:29:41 +00:00
|
|
|
type clientRequest struct {
|
|
|
|
req request
|
|
|
|
ready chan clientResponse
|
2019-07-22 18:13:41 +00:00
|
|
|
|
2019-07-22 23:58:43 +00:00
|
|
|
// retCh provides a context and sink for handling incoming channel messages
|
2019-07-24 12:20:57 +00:00
|
|
|
retCh makeChanSink
|
2019-07-12 15:29:41 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 12:30:21 +00:00
|
|
|
// ClientCloser is used to close Client from further use
|
|
|
|
type ClientCloser func()
|
|
|
|
|
2019-09-17 14:34:22 +00:00
|
|
|
// NewClient creates new jsonrpc 2.0 client
|
2019-07-02 17:45:03 +00:00
|
|
|
//
|
|
|
|
// handler must be pointer to a struct with function fields
|
2019-07-23 19:38:10 +00:00
|
|
|
// Returned value closes the client connection
|
2019-07-02 17:45:03 +00:00
|
|
|
// TODO: Example
|
2019-07-23 18:49:09 +00:00
|
|
|
func NewClient(addr string, namespace string, handler interface{}, requestHeader http.Header) (ClientCloser, error) {
|
2019-07-24 00:40:19 +00:00
|
|
|
return NewMergeClient(addr, namespace, []interface{}{handler}, requestHeader)
|
|
|
|
}
|
2019-06-28 13:49:34 +00:00
|
|
|
|
2019-07-24 12:20:57 +00:00
|
|
|
type client struct {
|
|
|
|
namespace string
|
|
|
|
|
|
|
|
requests chan clientRequest
|
2019-10-20 06:37:51 +00:00
|
|
|
exiting <-chan struct{}
|
2019-07-25 12:54:19 +00:00
|
|
|
idCtr int64
|
2019-07-24 12:20:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 00:40:19 +00:00
|
|
|
// NewMergeClient is like NewClient, but allows to specify multiple structs
|
|
|
|
// to be filled in the same namespace, using one connection
|
|
|
|
func NewMergeClient(addr string, namespace string, outs []interface{}, requestHeader http.Header) (ClientCloser, error) {
|
2019-07-23 18:49:09 +00:00
|
|
|
conn, _, err := websocket.DefaultDialer.Dial(addr, requestHeader)
|
2019-07-12 15:29:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-24 12:20:57 +00:00
|
|
|
c := client{
|
|
|
|
namespace: namespace,
|
|
|
|
}
|
|
|
|
|
2019-07-12 15:29:41 +00:00
|
|
|
stop := make(chan struct{})
|
2019-10-08 08:29:37 +00:00
|
|
|
exiting := make(chan struct{})
|
2019-07-24 12:20:57 +00:00
|
|
|
c.requests = make(chan clientRequest)
|
2019-10-20 06:37:51 +00:00
|
|
|
c.exiting = exiting
|
2019-07-12 15:29:41 +00:00
|
|
|
|
2019-07-12 17:12:38 +00:00
|
|
|
handlers := map[string]rpcHandler{}
|
2019-07-23 01:20:48 +00:00
|
|
|
go (&wsConn{
|
|
|
|
conn: conn,
|
|
|
|
handler: handlers,
|
2019-07-24 12:20:57 +00:00
|
|
|
requests: c.requests,
|
2019-07-23 01:20:48 +00:00
|
|
|
stop: stop,
|
2019-10-08 08:29:37 +00:00
|
|
|
exiting: exiting,
|
2019-07-23 01:20:48 +00:00
|
|
|
}).handleWsConn(context.TODO())
|
2019-07-12 15:29:41 +00:00
|
|
|
|
2019-07-24 00:40:19 +00:00
|
|
|
for _, handler := range outs {
|
|
|
|
htyp := reflect.TypeOf(handler)
|
|
|
|
if htyp.Kind() != reflect.Ptr {
|
|
|
|
return nil, xerrors.New("expected handler to be a pointer")
|
|
|
|
}
|
|
|
|
typ := htyp.Elem()
|
|
|
|
if typ.Kind() != reflect.Struct {
|
|
|
|
return nil, xerrors.New("handler should be a struct")
|
2019-06-28 13:49:34 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 00:40:19 +00:00
|
|
|
val := reflect.ValueOf(handler)
|
2019-06-28 13:49:34 +00:00
|
|
|
|
2019-07-24 00:40:19 +00:00
|
|
|
for i := 0; i < typ.NumField(); i++ {
|
2019-07-24 12:20:57 +00:00
|
|
|
fn, err := c.makeRpcFunc(typ.Field(i))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-06-28 13:49:34 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 12:20:57 +00:00
|
|
|
val.Elem().Field(i).Set(fn)
|
|
|
|
}
|
|
|
|
}
|
2019-06-28 14:05:10 +00:00
|
|
|
|
2019-07-24 12:20:57 +00:00
|
|
|
return func() {
|
|
|
|
close(stop)
|
2019-10-08 08:29:37 +00:00
|
|
|
<-exiting
|
2019-07-24 12:20:57 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2019-07-24 00:40:19 +00:00
|
|
|
|
2019-07-24 12:20:57 +00:00
|
|
|
func (c *client) makeOutChan(ctx context.Context, ftyp reflect.Type, valOut int) (func() reflect.Value, makeChanSink) {
|
|
|
|
retVal := reflect.Zero(ftyp.Out(valOut))
|
2019-06-28 14:05:10 +00:00
|
|
|
|
2019-07-24 12:20:57 +00:00
|
|
|
chCtor := func() (context.Context, func([]byte, bool)) {
|
|
|
|
// unpack chan type to make sure it's reflect.BothDir
|
|
|
|
ctyp := reflect.ChanOf(reflect.BothDir, ftyp.Out(valOut).Elem())
|
|
|
|
ch := reflect.MakeChan(ctyp, 0) // todo: buffer?
|
2019-10-30 10:23:13 +00:00
|
|
|
chCtx, chCancel := context.WithCancel(ctx)
|
2019-07-24 12:20:57 +00:00
|
|
|
retVal = ch.Convert(ftyp.Out(valOut))
|
2019-06-28 14:05:10 +00:00
|
|
|
|
2019-09-27 11:37:44 +00:00
|
|
|
buf := (&list.List{}).Init()
|
|
|
|
var bufLk sync.Mutex
|
|
|
|
|
2019-07-24 12:20:57 +00:00
|
|
|
return ctx, func(result []byte, ok bool) {
|
|
|
|
if !ok {
|
2019-10-30 10:23:13 +00:00
|
|
|
chCancel()
|
2019-07-24 12:20:57 +00:00
|
|
|
// remote channel closed, close ours too
|
|
|
|
ch.Close()
|
|
|
|
return
|
|
|
|
}
|
2019-07-24 00:40:19 +00:00
|
|
|
|
2019-07-24 12:20:57 +00:00
|
|
|
val := reflect.New(ftyp.Out(valOut).Elem())
|
|
|
|
if err := json.Unmarshal(result, val.Interface()); err != nil {
|
|
|
|
log.Errorf("error unmarshaling chan response: %s", err)
|
|
|
|
return
|
2019-06-28 13:49:34 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 11:37:44 +00:00
|
|
|
bufLk.Lock()
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
log.Errorf("got rpc message with cancelled context: %s", ctx.Err())
|
|
|
|
bufLk.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.PushBack(val)
|
|
|
|
|
|
|
|
if buf.Len() > 1 {
|
2019-10-11 16:02:21 +00:00
|
|
|
log.Warnw("rpc output message buffer", "n", buf.Len())
|
2019-09-27 11:37:44 +00:00
|
|
|
bufLk.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for buf.Len() > 0 {
|
2019-10-31 05:11:10 +00:00
|
|
|
front := buf.Front()
|
|
|
|
bufLk.Unlock()
|
|
|
|
|
2019-11-05 18:40:51 +00:00
|
|
|
cases := []reflect.SelectCase{
|
2019-10-31 05:11:10 +00:00
|
|
|
{
|
2019-11-05 18:40:51 +00:00
|
|
|
Dir: reflect.SelectRecv,
|
2019-10-31 05:11:10 +00:00
|
|
|
Chan: reflect.ValueOf(chCtx.Done()),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Dir: reflect.SelectSend,
|
|
|
|
Chan: ch,
|
|
|
|
Send: front.Value.(reflect.Value).Elem(),
|
|
|
|
},
|
|
|
|
}
|
2019-10-30 10:23:13 +00:00
|
|
|
|
2019-10-31 09:39:42 +00:00
|
|
|
chosen, _, _ := reflect.Select(cases)
|
|
|
|
bufLk.Lock()
|
|
|
|
|
|
|
|
switch chosen {
|
2019-10-31 05:11:10 +00:00
|
|
|
case 0:
|
|
|
|
buf.Init()
|
|
|
|
case 1:
|
2019-10-30 10:23:13 +00:00
|
|
|
buf.Remove(front)
|
|
|
|
}
|
2019-09-27 11:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bufLk.Unlock()
|
|
|
|
}()
|
|
|
|
|
2019-07-25 12:54:19 +00:00
|
|
|
}
|
2019-07-24 12:20:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return func() reflect.Value { return retVal }, chCtor
|
|
|
|
}
|
|
|
|
|
2019-10-20 06:37:51 +00:00
|
|
|
func (c *client) sendRequest(ctx context.Context, req request, chCtor makeChanSink) (clientResponse, error) {
|
2019-07-24 12:20:57 +00:00
|
|
|
rchan := make(chan clientResponse, 1)
|
2019-10-20 06:37:51 +00:00
|
|
|
creq := clientRequest{
|
2019-07-24 12:20:57 +00:00
|
|
|
req: req,
|
|
|
|
ready: rchan,
|
|
|
|
|
|
|
|
retCh: chCtor,
|
|
|
|
}
|
2019-10-20 06:37:51 +00:00
|
|
|
select {
|
|
|
|
case c.requests <- creq:
|
|
|
|
case <-c.exiting:
|
|
|
|
return clientResponse{}, fmt.Errorf("websocket routine exiting")
|
|
|
|
}
|
|
|
|
|
2019-07-24 12:20:57 +00:00
|
|
|
var ctxDone <-chan struct{}
|
|
|
|
var resp clientResponse
|
|
|
|
|
|
|
|
if ctx != nil {
|
|
|
|
ctxDone = ctx.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for response, handle context cancellation
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case resp = <-rchan:
|
|
|
|
break loop
|
|
|
|
case <-ctxDone: // send cancel request
|
|
|
|
ctxDone = nil
|
|
|
|
|
2019-10-20 06:37:51 +00:00
|
|
|
cancelReq := clientRequest{
|
2019-07-24 12:20:57 +00:00
|
|
|
req: request{
|
2019-07-24 00:40:19 +00:00
|
|
|
Jsonrpc: "2.0",
|
2019-07-24 12:20:57 +00:00
|
|
|
Method: wsCancel,
|
|
|
|
Params: []param{{v: reflect.ValueOf(*req.ID)}},
|
|
|
|
},
|
|
|
|
}
|
2019-10-20 06:37:51 +00:00
|
|
|
select {
|
|
|
|
case c.requests <- cancelReq:
|
|
|
|
case <-c.exiting:
|
|
|
|
log.Warn("failed to send request cancellation, websocket routing exited")
|
|
|
|
}
|
2019-07-24 12:20:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-28 13:49:34 +00:00
|
|
|
|
2019-10-20 06:37:51 +00:00
|
|
|
return resp, nil
|
2019-07-24 12:20:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type rpcFunc struct {
|
|
|
|
client *client
|
|
|
|
|
|
|
|
ftyp reflect.Type
|
|
|
|
name string
|
|
|
|
|
2019-07-25 12:54:19 +00:00
|
|
|
nout int
|
2019-07-24 12:20:57 +00:00
|
|
|
valOut int
|
|
|
|
errOut int
|
|
|
|
|
|
|
|
hasCtx int
|
2019-07-25 12:54:19 +00:00
|
|
|
retCh bool
|
2019-07-24 12:20:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fn *rpcFunc) processResponse(resp clientResponse, rval reflect.Value) []reflect.Value {
|
|
|
|
out := make([]reflect.Value, fn.nout)
|
|
|
|
|
|
|
|
if fn.valOut != -1 {
|
|
|
|
out[fn.valOut] = rval
|
|
|
|
}
|
|
|
|
if fn.errOut != -1 {
|
|
|
|
out[fn.errOut] = reflect.New(errorType).Elem()
|
|
|
|
if resp.Error != nil {
|
|
|
|
out[fn.errOut].Set(reflect.ValueOf(resp.Error))
|
2019-07-24 00:40:19 +00:00
|
|
|
}
|
2019-06-28 13:49:34 +00:00
|
|
|
}
|
2019-07-03 14:32:31 +00:00
|
|
|
|
2019-07-24 12:20:57 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fn *rpcFunc) processError(err error) []reflect.Value {
|
|
|
|
out := make([]reflect.Value, fn.nout)
|
|
|
|
|
|
|
|
if fn.valOut != -1 {
|
|
|
|
out[fn.valOut] = reflect.New(fn.ftyp.Out(fn.valOut)).Elem()
|
|
|
|
}
|
|
|
|
if fn.errOut != -1 {
|
|
|
|
out[fn.errOut] = reflect.New(errorType).Elem()
|
|
|
|
out[fn.errOut].Set(reflect.ValueOf(&ErrClient{err}))
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fn *rpcFunc) handleRpcCall(args []reflect.Value) (results []reflect.Value) {
|
|
|
|
id := atomic.AddInt64(&fn.client.idCtr, 1)
|
|
|
|
params := make([]param, len(args)-fn.hasCtx)
|
|
|
|
for i, arg := range args[fn.hasCtx:] {
|
|
|
|
params[i] = param{
|
|
|
|
v: arg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var ctx context.Context
|
2019-07-25 20:19:43 +00:00
|
|
|
var span *trace.Span
|
2019-07-24 12:20:57 +00:00
|
|
|
if fn.hasCtx == 1 {
|
|
|
|
ctx = args[0].Interface().(context.Context)
|
2019-07-25 20:19:43 +00:00
|
|
|
ctx, span = trace.StartSpan(ctx, "api.call")
|
|
|
|
defer span.End()
|
2019-07-24 12:20:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
retVal := func() reflect.Value { return reflect.Value{} }
|
|
|
|
|
|
|
|
// if the function returns a channel, we need to provide a sink for the
|
|
|
|
// messages
|
|
|
|
var chCtor makeChanSink
|
|
|
|
if fn.retCh {
|
|
|
|
retVal, chCtor = fn.client.makeOutChan(ctx, fn.ftyp, fn.valOut)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := request{
|
|
|
|
Jsonrpc: "2.0",
|
|
|
|
ID: &id,
|
|
|
|
Method: fn.client.namespace + "." + fn.name,
|
|
|
|
Params: params,
|
|
|
|
}
|
|
|
|
|
2019-07-25 20:19:43 +00:00
|
|
|
if span != nil {
|
|
|
|
span.AddAttributes(trace.StringAttribute("method", req.Method))
|
|
|
|
|
|
|
|
eSC := base64.StdEncoding.EncodeToString(
|
|
|
|
propagation.Binary(span.SpanContext()))
|
|
|
|
req.Meta = map[string]string{
|
|
|
|
"SpanContext": eSC,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 06:37:51 +00:00
|
|
|
resp, err := fn.client.sendRequest(ctx, req, chCtor)
|
|
|
|
if err != nil {
|
|
|
|
return fn.processError(fmt.Errorf("sendRequest failed: %w", err))
|
|
|
|
}
|
2019-07-24 12:20:57 +00:00
|
|
|
|
|
|
|
if resp.ID != *req.ID {
|
|
|
|
return fn.processError(xerrors.New("request and response id didn't match"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if fn.valOut != -1 && !fn.retCh {
|
|
|
|
val := reflect.New(fn.ftyp.Out(fn.valOut))
|
|
|
|
|
|
|
|
if resp.Result != nil {
|
|
|
|
log.Debugw("rpc result", "type", fn.ftyp.Out(fn.valOut))
|
|
|
|
if err := json.Unmarshal(resp.Result, val.Interface()); err != nil {
|
2019-08-26 10:04:57 +00:00
|
|
|
log.Warnw("unmarshaling failed", "message", string(resp.Result))
|
2019-07-24 12:20:57 +00:00
|
|
|
return fn.processError(xerrors.Errorf("unmarshaling result: %w", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
retVal = func() reflect.Value { return val.Elem() }
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn.processResponse(resp, retVal())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) makeRpcFunc(f reflect.StructField) (reflect.Value, error) {
|
|
|
|
ftyp := f.Type
|
|
|
|
if ftyp.Kind() != reflect.Func {
|
|
|
|
return reflect.Value{}, xerrors.New("handler field not a func")
|
|
|
|
}
|
|
|
|
|
|
|
|
fun := &rpcFunc{
|
|
|
|
client: c,
|
2019-07-25 12:54:19 +00:00
|
|
|
ftyp: ftyp,
|
|
|
|
name: f.Name,
|
2019-07-24 12:20:57 +00:00
|
|
|
}
|
|
|
|
fun.valOut, fun.errOut, fun.nout = processFuncOut(ftyp)
|
|
|
|
|
|
|
|
if ftyp.NumIn() > 0 && ftyp.In(0) == contextType {
|
|
|
|
fun.hasCtx = 1
|
|
|
|
}
|
|
|
|
fun.retCh = fun.valOut != -1 && ftyp.Out(fun.valOut).Kind() == reflect.Chan
|
|
|
|
|
|
|
|
return reflect.MakeFunc(ftyp, fun.handleRpcCall), nil
|
2019-06-28 13:49:34 +00:00
|
|
|
}
|