jsonrpc

package
v0.11.7 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package jsonrpc implements a JSONRPC2.0 compliant server as described in https://www.jsonrpc.org/specification

Index

Constants

View Source
const (
	InvalidJSON    = -32700 // Invalid JSON was received by the server.
	InvalidRequest = -32600 // The JSON sent is not a valid Request object.
	MethodNotFound = -32601 // The method does not exist / is not available.
	InvalidParams  = -32602 // Invalid method parameter(s).
	InternalError  = -32603 // Internal JSON-RPC error.
)
View Source
const MaxRequestBodySize = 10 * utils.Megabyte

Variables

View Source
var (
	ErrInvalidID = errors.New("id should be a string or an integer")
)

Functions

This section is empty.

Types

type Conn added in v0.7.1

type Conn interface {
	io.Writer
	Equal(Conn) bool
}

func ConnFromContext added in v0.7.0

func ConnFromContext(ctx context.Context) (Conn, bool)

ConnFromContext returns a writable connection. The connection should be written in a separate goroutine, since writes from handlers are blocked until the initial response is sent.

type ConnKey added in v0.7.0

type ConnKey struct{}

ConnKey the key used to retrieve the connection from the context passed to a handler. It is exported to allow transports to set it manually if they decide not to use HandleReadWriter, which sets it automatically. Manually setting the connection can be especially useful when testing handlers.

type Error

type Error struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    any    `json:"data,omitempty"`
}

func Err added in v0.4.0

func Err(code int, data any) *Error

func (*Error) CloneWithData added in v0.7.4

func (e *Error) CloneWithData(data any) *Error

CloneWithData copies the error and sets the data field on the copy

type EventListener added in v0.7.0

type EventListener interface {
	NewRequestListener
	OnRequestHandled(method string, took time.Duration)
	OnRequestFailed(method string, data any)
}

type HTTP added in v0.2.1

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

func NewHTTP added in v0.2.1

func NewHTTP(rpc *Server, log utils.SimpleLogger) *HTTP

func (*HTTP) ServeHTTP added in v0.2.1

func (h *HTTP) ServeHTTP(writer http.ResponseWriter, req *http.Request)

ServeHTTP processes an incoming HTTP request

func (*HTTP) WithListener added in v0.7.0

func (h *HTTP) WithListener(listener NewRequestListener) *HTTP

WithListener registers a NewRequestListener

type Method

type Method struct {
	Name    string
	Params  []Parameter
	Handler any
	// contains filtered or unexported fields
}

type NewRequestListener added in v0.7.0

type NewRequestListener interface {
	OnNewRequest(method string)
}

type Parameter

type Parameter struct {
	Name     string
	Optional bool
}

type Request added in v0.7.1

type Request struct {
	Version string `json:"jsonrpc"`
	Method  string `json:"method"`
	Params  any    `json:"params,omitempty"`
	ID      any    `json:"id,omitempty"`
}

type SelectiveListener added in v0.7.0

type SelectiveListener struct {
	OnNewRequestCb     func(method string)
	OnRequestHandledCb func(method string, took time.Duration)
	OnRequestFailedCb  func(method string, data any)
}

func (*SelectiveListener) OnNewRequest added in v0.7.0

func (l *SelectiveListener) OnNewRequest(method string)

func (*SelectiveListener) OnRequestFailed added in v0.7.0

func (l *SelectiveListener) OnRequestFailed(method string, data any)

func (*SelectiveListener) OnRequestHandled added in v0.7.0

func (l *SelectiveListener) OnRequestHandled(method string, took time.Duration)

type Server

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

func NewServer

func NewServer(poolMaxGoroutines int, log utils.SimpleLogger) *Server

NewServer instantiates a JSONRPC server

func (*Server) HandleReadWriter added in v0.7.1

func (s *Server) HandleReadWriter(ctx context.Context, rw io.ReadWriter) error

HandleReadWriter permits methods to send messages on the connection after the server sends the initial response. rw must permit concurrent writes. A non-nil error indicates the initial response could not be sent, and that no method will be able to write the connection.

func (*Server) HandleReader

func (s *Server) HandleReader(ctx context.Context, reader io.Reader) ([]byte, error)

HandleReader processes a request to the server It returns the response in a byte array, only returns an error if it can not create the response byte array

func (*Server) RegisterMethods added in v0.7.0

func (s *Server) RegisterMethods(methods ...Method) error

RegisterMethods verifies and creates an endpoint that the server recognises.

- name is the method name - handler is the function to be called when a request is received for the associated method. It should have (any, *jsonrpc.Error) as its return type - paramNames are the names of parameters in the order that they are expected by the handler

func (*Server) WithListener added in v0.7.0

func (s *Server) WithListener(listener EventListener) *Server

WithListener registers an EventListener

func (*Server) WithValidator added in v0.4.0

func (s *Server) WithValidator(validator Validator) *Server

WithValidator registers a validator to validate handler struct arguments

type Validator added in v0.4.0

type Validator interface {
	Struct(any) error
}

type Websocket added in v0.4.0

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

func NewWebsocket added in v0.4.0

func NewWebsocket(rpc *Server, log utils.SimpleLogger) *Websocket

func (*Websocket) ServeHTTP added in v0.6.0

func (ws *Websocket) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP processes an HTTP request and upgrades it to a websocket connection. The connection's entire "lifetime" is spent in this function.

func (*Websocket) WithConnParams added in v0.4.0

func (ws *Websocket) WithConnParams(p *WebsocketConnParams) *Websocket

WithConnParams sanity checks and applies the provided params.

func (*Websocket) WithListener added in v0.7.0

func (ws *Websocket) WithListener(listener NewRequestListener) *Websocket

WithListener registers a NewRequestListener

type WebsocketConnParams added in v0.4.0

type WebsocketConnParams struct {
	// Maximum message size allowed.
	ReadLimit int64
	// Maximum time to write a message.
	WriteDuration time.Duration
}

func DefaultWebsocketConnParams added in v0.4.0

func DefaultWebsocketConnParams() *WebsocketConnParams

Jump to

Keyboard shortcuts

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