@@ -20,7 +20,8 @@ import (
2020 "github.com/filecoin-project/go-jsonrpc/metrics"
2121)
2222
23- type rpcHandler struct {
23+ // methodHandler is a handler for a single method
24+ type methodHandler struct {
2425 paramReceivers []reflect.Type
2526 nParams int
2627
@@ -94,9 +95,34 @@ type response struct {
9495 Error * respError `json:"error,omitempty"`
9596}
9697
98+ type handler struct {
99+ methods map [string ]methodHandler
100+ errors * Errors
101+
102+ maxRequestSize int64
103+
104+ // aliasedMethods contains a map of alias:original method names.
105+ // These are used as fallbacks if a method is not found by the given method name.
106+ aliasedMethods map [string ]string
107+
108+ paramDecoders map [reflect.Type ]ParamDecoder
109+ }
110+
111+ func makeHandler (sc ServerConfig ) * handler {
112+ return & handler {
113+ methods : make (map [string ]methodHandler ),
114+ errors : sc .errors ,
115+
116+ aliasedMethods : map [string ]string {},
117+ paramDecoders : sc .paramDecoders ,
118+
119+ maxRequestSize : sc .maxRequestSize ,
120+ }
121+ }
122+
97123// Register
98124
99- func (s * RPCServer ) register (namespace string , r interface {}) {
125+ func (s * handler ) register (namespace string , r interface {}) {
100126 val := reflect .ValueOf (r )
101127 // TODO: expect ptr
102128
@@ -117,7 +143,7 @@ func (s *RPCServer) register(namespace string, r interface{}) {
117143
118144 valOut , errOut , _ := processFuncOut (funcType )
119145
120- s .methods [namespace + "." + method .Name ] = rpcHandler {
146+ s .methods [namespace + "." + method .Name ] = methodHandler {
121147 paramReceivers : recvs ,
122148 nParams : ins ,
123149
@@ -137,7 +163,7 @@ func (s *RPCServer) register(namespace string, r interface{}) {
137163type rpcErrFunc func (w func (func (io.Writer )), req * request , code ErrorCode , err error )
138164type chanOut func (reflect.Value , interface {}) error
139165
140- func (s * RPCServer ) handleReader (ctx context.Context , r io.Reader , w io.Writer , rpcError rpcErrFunc ) {
166+ func (s * handler ) handleReader (ctx context.Context , r io.Reader , w io.Writer , rpcError rpcErrFunc ) {
141167 wf := func (cb func (io.Writer )) {
142168 cb (w )
143169 }
@@ -194,7 +220,7 @@ func doCall(methodName string, f reflect.Value, params []reflect.Value) (out []r
194220 return out , nil
195221}
196222
197- func (s * RPCServer ) getSpan (ctx context.Context , req request ) (context.Context , * trace.Span ) {
223+ func (s * handler ) getSpan (ctx context.Context , req request ) (context.Context , * trace.Span ) {
198224 if req .Meta == nil {
199225 return ctx , nil
200226 }
@@ -221,7 +247,7 @@ func (s *RPCServer) getSpan(ctx context.Context, req request) (context.Context,
221247 return ctx , span
222248}
223249
224- func (s * RPCServer ) createError (err error ) * respError {
250+ func (s * handler ) createError (err error ) * respError {
225251 var code ErrorCode = 1
226252 if s .errors != nil {
227253 c , ok := s .errors .byType [reflect .TypeOf (err )]
@@ -245,7 +271,7 @@ func (s *RPCServer) createError(err error) *respError {
245271 return out
246272}
247273
248- func (s * RPCServer ) handle (ctx context.Context , req request , w func (func (io.Writer )), rpcError rpcErrFunc , done func (keepCtx bool ), chOut chanOut ) {
274+ func (s * handler ) handle (ctx context.Context , req request , w func (func (io.Writer )), rpcError rpcErrFunc , done func (keepCtx bool ), chOut chanOut ) {
249275 // Not sure if we need to sanitize the incoming req.Method or not.
250276 ctx , span := s .getSpan (ctx , req )
251277 ctx , _ = tag .New (ctx , tag .Insert (metrics .RPCMethod , req .Method ))
0 commit comments