jsonrpc2

package
v0.2.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2018 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrCodeParse          int = -32700
	ErrCodeInvalidRequest     = -32600
	ErrCodeMethodNotFound     = -32601
	ErrCodeInvalidParams      = -32602
	ErrCodeInternal           = -32603
	ErrCodeServer             = -32000
)
View Source
const Version = "2.0"

Variables

View Source
var ErrNoPublicMethods = errors.New("no public methods")

Functions

func DebugCodec

func DebugCodec(labelPrefix string, codec Codec) *debugCodec

DebugCodec logs each incoming and outgoing message with a given label prefix (use something like the IP address or user ID).

func IOCodec

func IOCodec(rwc io.ReadWriteCloser) *jsonCodec

IOCodec returns a Codec that wraps JSON encoding and decoding over IO.

func IsErrorCode added in v0.2.2

func IsErrorCode(err error, allowedCodes ...int) bool

IsErrorCode returns true iff the error has an ErrorCode. If allowedCodes is provided, then it also checks that it matches one of the allowedCodes.

func Methods

func Methods(receiver interface{}) (map[string]Method, error)

Methods returns a mapping of valid method names to Method definitions for a instance's receiver.

func ServePipe

func ServePipe() (*Remote, *Remote)

ServePipe sets up symmetric server/clients over a net.Pipe() and starts both in goroutines. Useful for testing. Services still need to be registered.

func SetLogger

func SetLogger(w io.Writer)

SetLogger overrides the logger output for this package.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is responsible for making request messages.

func (*Client) NextID

func (c *Client) NextID() int

func (*Client) Request

func (c *Client) Request(method string, params ...interface{}) (*Message, error)

type Codec

type Codec interface {
	ReadMessage() (*Message, error)
	WriteMessage(*Message) error
	Close() error
	RemoteAddr() string
}

Codec is an straction for receiving and sending JSONRPC messages.

type ContextMissingValueError added in v0.2.6

type ContextMissingValueError struct {
	Key serviceContext
}

ContextMissingValueError is returned when a context is missing an expected value.

func (ContextMissingValueError) Error added in v0.2.6

func (err ContextMissingValueError) Error() string

type ErrResponse

type ErrResponse struct {
	Code    int             `json:"code"`
	Message string          `json:"message"`
	Data    json.RawMessage `json:"data,omitempty"`
}

ErrResponse is returned as part of a Response message when there is an error.

func (*ErrResponse) Error

func (err *ErrResponse) Error() string

func (*ErrResponse) ErrorCode

func (err *ErrResponse) ErrorCode() int

type ErrorCode added in v0.2.2

type ErrorCode int

type HTTPRequestError added in v0.2.2

type HTTPRequestError struct {
	Response *http.Response
	Reason   string
}

HTTPRequestError is used when RPC over HTTP encounters an error during transport.

func (HTTPRequestError) Error added in v0.2.2

func (err HTTPRequestError) Error() string

type HTTPServer added in v0.2.2

type HTTPServer struct {
	Server

	// MaxContentLength is the request size limit (optional)
	MaxContentLength int64
}

HTTPServer provides a JSONRPC2 server over HTTP by implementing http.Handler.

func (*HTTPServer) ServeHTTP added in v0.2.2

func (h *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

type HTTPService added in v0.2.2

type HTTPService struct {
	Client
	HTTPClient http.Client

	// Endpoint is the HTTP URL to dial for RPC calls.
	Endpoint string
	// MaxContentLength is the response size limit (optional)
	MaxContentLength int64
}

func (*HTTPService) Call added in v0.2.2

func (service *HTTPService) Call(ctx context.Context, result interface{}, method string, params ...interface{}) error

type Handler

type Handler interface {
	// Handle takes a request message and returns a response message.
	Handle(ctx context.Context, request *Message) (response *Message)
	// FIXME: Register* really shouldn't be part of this signature, right?
	Register(prefix string, receiver interface{}) error
	RegisterMethod(rpcName string, receiver interface{}, methodName string) error
}

Handler is a server that executes an RPC request message, returning an RPC response message.

type Local

type Local struct {
	Client
	Server
}

Local is a Service implementation for a local Server. It's like Remote, but no Codec.

func (*Local) Call

func (loc *Local) Call(ctx context.Context, result interface{}, method string, params ...interface{}) error

type Message

type Message struct {
	*Request
	*Response
	ID      json.RawMessage `json:"id,omitempty"`
	Version string          `json:"jsonrpc"` // TODO: Replace this with a null-type that encodes to 2.0, like https://go-review.googlesource.com/c/tools/+/136675/1/internal/jsonrpc2/jsonrpc2.go#221
}

type Method

type Method struct {
	Receiver reflect.Value
	Method   reflect.Method
	ArgTypes []reflect.Type
	ErrPos   int
	HasCtx   bool
}

Method is the definition of a callable method.

func MethodByName

func MethodByName(receiver interface{}, name string) (Method, error)

MethodByName returns a single Method from receiver that matches the name.

func (*Method) Call

func (m *Method) Call(ctx context.Context, args []reflect.Value) (interface{}, error)

Call executes the method with the given arguments.

func (*Method) CallJSON

func (m *Method) CallJSON(ctx context.Context, rawArgs json.RawMessage) (interface{}, error)

CallJSON wraps Call but supports JSON-encoded args

type Remote

type Remote struct {
	Codec
	Client Requester
	Server Handler

	// PendingLimit is the number of messages to hold before oldest messages get discarded.
	PendingLimit int
	// PendingDiscard is the number of oldest messages that get discarded when PendingLimit is reached.
	PendingDiscard int
	// contains filtered or unexported fields
}

Remote is a wrapper around a connection that can be both a Client and a Server. It implements the Service interface, and manages async message routing.

func (*Remote) Call

func (r *Remote) Call(ctx context.Context, result interface{}, method string, params ...interface{}) error

Call handles sending an RPC and receiving the corresponding response synchronously.

func (*Remote) Serve

func (r *Remote) Serve() error

type Request

type Request struct {
	Method string          `json:"method"`
	Params json.RawMessage `json:"params,omitempty"`
}

type Requester

type Requester interface {
	// Request takes call inputs and creates a valid request Message.
	Request(method string, params ...interface{}) (*Message, error)
}

type Response

type Response struct {
	Result json.RawMessage `json:"result,omitempty"`
	Error  *ErrResponse    `json:"error,omitempty"`
}

func (*Response) UnmarshalResult added in v0.2.2

func (resp *Response) UnmarshalResult(result interface{}) error

UnmarshalResult attempts to convert the message into a successful result unmarshal. If the message is not a success type (or if unmarshal fails), then an appropriate error will be returned.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server contains the method registry.

func (*Server) Handle

func (s *Server) Handle(ctx context.Context, req *Message) *Message

Handle executes a request message against the server registry.

func (*Server) Register

func (s *Server) Register(prefix string, receiver interface{}) error

Register adds valid methods from the receiver to the registry with the given prefix. Method names are lowercased.

func (*Server) RegisterMethod

func (s *Server) RegisterMethod(rpcName string, receiver interface{}, methodName string) error

RegisterMethod registers a single methodName from receiver with the given rpcName.

type Service

type Service interface {
	Call(ctx context.Context, result interface{}, method string, params ...interface{}) error
}

Service represents a remote service that can be called.

func CtxService

func CtxService(ctx context.Context) (Service, error)

CtxService returns a Service associated with this request from a context used within a call. This is useful for initiating bidirectional calls.

Directories

Path Synopsis
ws
gorilla
Websocket implementation using Gorilla's Websocket library
Websocket implementation using Gorilla's Websocket library

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL