2018-03-19 17:14:50 +00:00
|
|
|
// Package mrpc contains types and functionality to facilitate creating RPC
|
2018-04-14 10:45:11 +00:00
|
|
|
// interfaces and for making calls against those same interfaces.
|
2018-03-19 17:14:50 +00:00
|
|
|
//
|
|
|
|
// This package contains a few fundamental types: Handler, Call, and
|
|
|
|
// Client. Together these form the components needed to implement nearly any RPC
|
|
|
|
// system.
|
2018-04-27 08:03:39 +00:00
|
|
|
//
|
|
|
|
// TODO document examples
|
|
|
|
// TODO document Debug?
|
2018-03-19 17:14:50 +00:00
|
|
|
package mrpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2018-05-16 12:19:31 +00:00
|
|
|
// Request describes an RPC request being processed by a Handler
|
2018-05-10 03:29:32 +00:00
|
|
|
type Request struct {
|
|
|
|
Context context.Context
|
|
|
|
|
|
|
|
// The name of the RPC method being called.
|
|
|
|
Method string
|
|
|
|
|
|
|
|
// Unmarshal takes in a pointer and unmarshals the RPC request's arguments
|
|
|
|
// into it. The properties of the unmarshaling are dependent on the
|
|
|
|
// underlying implementation of the protocol.
|
|
|
|
Unmarshal func(interface{}) error
|
2018-05-16 12:19:31 +00:00
|
|
|
|
|
|
|
// Debugging information being carried with the Request. See Debug's docs
|
2018-05-16 12:56:06 +00:00
|
|
|
// for more on how it is intended to be used.
|
2018-05-16 12:19:31 +00:00
|
|
|
Debug Debug
|
2018-05-10 03:29:32 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 12:19:31 +00:00
|
|
|
// ResponseWriter is used to capture the response of an RPC request being
|
|
|
|
// processed by a Handler
|
2018-05-10 03:29:32 +00:00
|
|
|
type ResponseWriter struct {
|
2018-05-16 12:19:31 +00:00
|
|
|
// Response should be overwritten with whatever response to the call should
|
|
|
|
// be. The exact nature and behavior of how the response value is treated is
|
|
|
|
// dependent on the RPC implementation.
|
|
|
|
Response interface{}
|
|
|
|
|
|
|
|
// Debug may be overwritten to provide debugging information back to the
|
|
|
|
// Client with the Response. See Debug's docs for more on how it is intended
|
|
|
|
// to be used.
|
|
|
|
Debug Debug
|
2018-05-10 03:29:32 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 12:19:31 +00:00
|
|
|
// Reponse describes the response from the RPC call being returned to the
|
|
|
|
// Client.
|
2018-05-10 03:29:32 +00:00
|
|
|
type Response struct {
|
2018-05-16 12:19:31 +00:00
|
|
|
// Unmarshal takes in a pointer value into which the Client will unmarshal
|
|
|
|
// the response value. The exact nature and behavior of how the pointer
|
|
|
|
// value is treated is dependend on the RPC implementation.
|
2018-05-10 03:29:32 +00:00
|
|
|
Unmarshal func(interface{}) error
|
2018-05-16 12:19:31 +00:00
|
|
|
|
|
|
|
// Debug will be whatever debug information was set by the server when
|
|
|
|
// responding to the call.
|
|
|
|
Debug Debug
|
2018-05-10 03:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handler is a type which serves RPC calls. For each incoming Requests the
|
|
|
|
// ServeRPC method is called with a ResponseWriter which will write the call's
|
|
|
|
// response back to the client.
|
2018-05-16 12:56:06 +00:00
|
|
|
//
|
|
|
|
// Any go-routines spawned by ServeRPC should expect to terminate if the
|
|
|
|
// Request's Context is canceled
|
2018-03-19 17:14:50 +00:00
|
|
|
type Handler interface {
|
2018-05-10 03:29:32 +00:00
|
|
|
ServeRPC(Request, *ResponseWriter)
|
2018-03-19 17:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandlerFunc can be used to wrap an individual function which fits the
|
|
|
|
// ServeRPC signature, and use that function as a Handler
|
2018-05-10 03:29:32 +00:00
|
|
|
type HandlerFunc func(Request, *ResponseWriter)
|
2018-03-19 17:14:50 +00:00
|
|
|
|
|
|
|
// ServeRPC implements the method for the Handler interface by calling the
|
|
|
|
// underlying function
|
2018-05-10 03:29:32 +00:00
|
|
|
func (hf HandlerFunc) ServeRPC(r Request, rw *ResponseWriter) {
|
|
|
|
hf(r, rw)
|
2018-03-19 17:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Client is an entity which can perform RPC calls against a remote endpoint.
|
|
|
|
type Client interface {
|
2018-05-16 12:56:06 +00:00
|
|
|
CallRPC(ctx context.Context, method string, args interface{}, debug Debug) Response
|
2018-03-19 17:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ClientFunc can be used to wrap an individual function which fits the CallRPC
|
|
|
|
// signature, and use that function as a Client
|
2018-05-16 12:56:06 +00:00
|
|
|
type ClientFunc func(context.Context, string, interface{}, Debug) Response
|
2018-03-19 17:14:50 +00:00
|
|
|
|
|
|
|
// CallRPC implements the method for the Client interface by calling the
|
|
|
|
// underlying function
|
2018-05-16 12:56:06 +00:00
|
|
|
func (cf ClientFunc) CallRPC(
|
|
|
|
ctx context.Context,
|
|
|
|
method string,
|
|
|
|
args interface{},
|
|
|
|
debug Debug,
|
|
|
|
) Response {
|
|
|
|
return cf(ctx, method, args, debug)
|
|
|
|
}
|
2018-03-19 17:14:50 +00:00
|
|
|
|
|
|
|
// ReflectClient returns a Client whose CallRPC method will use reflection to
|
|
|
|
// call the given Handler's ServeRPC method directly, using reflect.Value's Set
|
2018-05-10 03:29:32 +00:00
|
|
|
// method to copy CallRPC's args parameter into the Request's Unmarshal method's
|
|
|
|
// receiver parameter, and similarly to copy the result from ServeRPC into
|
|
|
|
// the Response's Unmarshal method's receiver parameter.
|
2018-03-19 17:14:50 +00:00
|
|
|
func ReflectClient(h Handler) Client {
|
|
|
|
into := func(dst, src interface{}) error {
|
|
|
|
dstV, srcV := reflect.ValueOf(dst), reflect.ValueOf(src)
|
|
|
|
dstVi, srcVi := reflect.Indirect(dstV), reflect.Indirect(srcV)
|
|
|
|
if !dstVi.CanSet() || dstVi.Type() != srcVi.Type() {
|
|
|
|
return fmt.Errorf("can't set value of type %v into type %v", srcV.Type(), dstV.Type())
|
|
|
|
}
|
|
|
|
dstVi.Set(srcVi)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ClientFunc(func(
|
|
|
|
ctx context.Context,
|
|
|
|
method string,
|
|
|
|
args interface{},
|
2018-05-16 12:56:06 +00:00
|
|
|
debug Debug,
|
2018-05-10 03:29:32 +00:00
|
|
|
) Response {
|
|
|
|
req := Request{
|
|
|
|
Context: ctx,
|
|
|
|
Method: method,
|
|
|
|
Unmarshal: func(i interface{}) error { return into(i, args) },
|
2018-05-16 12:56:06 +00:00
|
|
|
Debug: debug,
|
2018-03-19 17:14:50 +00:00
|
|
|
}
|
2018-05-16 12:19:31 +00:00
|
|
|
rw := ResponseWriter{}
|
2018-05-10 03:29:32 +00:00
|
|
|
h.ServeRPC(req, &rw)
|
|
|
|
|
|
|
|
return Response{
|
|
|
|
Unmarshal: func(i interface{}) error {
|
2018-05-16 12:19:31 +00:00
|
|
|
return into(i, rw.Response)
|
2018-05-10 03:29:32 +00:00
|
|
|
},
|
2018-05-16 12:19:31 +00:00
|
|
|
Debug: rw.Debug,
|
2018-05-10 03:29:32 +00:00
|
|
|
}
|
2018-03-19 17:14:50 +00:00
|
|
|
})
|
|
|
|
}
|