rpc: implement full bi-directional communication (#18471)
New APIs added:
client.RegisterName(namespace, service) // makes service available to server
client.Notify(ctx, method, args...) // sends a notification
ClientFromContext(ctx) // to get a client in handler method
This is essentially a rewrite of the server-side code. JSON-RPC
processing code is now the same on both server and client side. Many
minor issues were fixed in the process and there is a new test suite for
JSON-RPC spec compliance (and non-compliance in some cases).
List of behavior changes:
- Method handlers are now called with a per-request context instead of a
per-connection context. The context is canceled right after the method
returns.
- Subscription error channels are always closed when the connection
ends. There is no need to also wait on the Notifier's Closed channel
to detect whether the subscription has ended.
- Client now omits "params" instead of sending "params": null when there
are no arguments to a call. The previous behavior was not compliant
with the spec. The server still accepts "params": null.
- Floating point numbers are allowed as "id". The spec doesn't allow
them, but we handle request "id" as json.RawMessage and guarantee that
the same number will be sent back.
- Logging is improved significantly. There is now a message at DEBUG
level for each RPC call served.
This commit is contained in:
82
rpc/types.go
82
rpc/types.go
@@ -17,13 +17,11 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
mapset "github.com/deckarep/golang-set"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
)
|
||||
|
||||
@@ -35,57 +33,6 @@ type API struct {
|
||||
Public bool // indication if the methods must be considered safe for public use
|
||||
}
|
||||
|
||||
// callback is a method callback which was registered in the server
|
||||
type callback struct {
|
||||
rcvr reflect.Value // receiver of method
|
||||
method reflect.Method // callback
|
||||
argTypes []reflect.Type // input argument types
|
||||
hasCtx bool // method's first argument is a context (not included in argTypes)
|
||||
errPos int // err return idx, of -1 when method cannot return error
|
||||
isSubscribe bool // indication if the callback is a subscription
|
||||
}
|
||||
|
||||
// service represents a registered object
|
||||
type service struct {
|
||||
name string // name for service
|
||||
typ reflect.Type // receiver type
|
||||
callbacks callbacks // registered handlers
|
||||
subscriptions subscriptions // available subscriptions/notifications
|
||||
}
|
||||
|
||||
// serverRequest is an incoming request
|
||||
type serverRequest struct {
|
||||
id interface{}
|
||||
svcname string
|
||||
callb *callback
|
||||
args []reflect.Value
|
||||
isUnsubscribe bool
|
||||
err Error
|
||||
}
|
||||
|
||||
type serviceRegistry map[string]*service // collection of services
|
||||
type callbacks map[string]*callback // collection of RPC callbacks
|
||||
type subscriptions map[string]*callback // collection of subscription callbacks
|
||||
|
||||
// Server represents a RPC server
|
||||
type Server struct {
|
||||
services serviceRegistry
|
||||
|
||||
run int32
|
||||
codecsMu sync.Mutex
|
||||
codecs mapset.Set
|
||||
}
|
||||
|
||||
// rpcRequest represents a raw incoming RPC request
|
||||
type rpcRequest struct {
|
||||
service string
|
||||
method string
|
||||
id interface{}
|
||||
isPubSub bool
|
||||
params interface{}
|
||||
err Error // invalid batch element
|
||||
}
|
||||
|
||||
// Error wraps RPC errors, which contain an error code in addition to the message.
|
||||
type Error interface {
|
||||
Error() string // returns the message
|
||||
@@ -96,24 +43,19 @@ type Error interface {
|
||||
// a RPC session. Implementations must be go-routine safe since the codec can be called in
|
||||
// multiple go-routines concurrently.
|
||||
type ServerCodec interface {
|
||||
// Read next request
|
||||
ReadRequestHeaders() ([]rpcRequest, bool, Error)
|
||||
// Parse request argument to the given types
|
||||
ParseRequestArguments(argTypes []reflect.Type, params interface{}) ([]reflect.Value, Error)
|
||||
// Assemble success response, expects response id and payload
|
||||
CreateResponse(id interface{}, reply interface{}) interface{}
|
||||
// Assemble error response, expects response id and error
|
||||
CreateErrorResponse(id interface{}, err Error) interface{}
|
||||
// Assemble error response with extra information about the error through info
|
||||
CreateErrorResponseWithInfo(id interface{}, err Error, info interface{}) interface{}
|
||||
// Create notification response
|
||||
CreateNotification(id, namespace string, event interface{}) interface{}
|
||||
// Write msg to client.
|
||||
Write(msg interface{}) error
|
||||
// Close underlying data stream
|
||||
Read() (msgs []*jsonrpcMessage, isBatch bool, err error)
|
||||
Close()
|
||||
// Closed when underlying connection is closed
|
||||
jsonWriter
|
||||
}
|
||||
|
||||
// jsonWriter can write JSON messages to its underlying connection.
|
||||
// Implementations must be safe for concurrent use.
|
||||
type jsonWriter interface {
|
||||
Write(context.Context, interface{}) error
|
||||
// Closed returns a channel which is closed when the connection is closed.
|
||||
Closed() <-chan interface{}
|
||||
// RemoteAddr returns the peer address of the connection.
|
||||
RemoteAddr() string
|
||||
}
|
||||
|
||||
type BlockNumber int64
|
||||
|
||||
Reference in New Issue
Block a user