jsonrpc

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT, Apache-2.0 Imports: 32 Imported by: 1

README

go-jsonrpc

Documentation

Index

Constants

View Source
const (
	ProxyTagRetry     = "retry"
	ProxyTagNotify    = "notify"
	ProxyTagRPCMethod = "rpc_method"
)
View Source
const DEFAULT_MAX_REQUEST_SIZE = 100 << 20 // 100 MiB

Limit request size. Ideally this limit should be specific for each field in the JSON request but as a simple defensive measure we just limit the entire HTTP body. Configured by WithMaxRequestSize.

View Source
const FirstUserCode = 2

Variables

This section is empty.

Functions

func DecodeParams

func DecodeParams[T any](p RawParams) (T, error)

todo is there a better way to tell 'struct with any number of fields'?

func ExtractReverseClient

func ExtractReverseClient[C any](ctx context.Context) (C, bool)

ExtractReverseClient will extract reverse client from context. Reverse client for the type will only be present if the server was constructed with a matching WithReverseClient option and the connection was a websocket connection. If there is no reverse client, the call will return a zero value and `false`. Otherwise a reverse client and `true` will be returned.

func WithClientHandler

func WithClientHandler(ns string, hnd interface{}) func(c *Config)

func WithClientHandlerAlias

func WithClientHandlerAlias(alias, original string) func(c *Config)

WithClientHandlerAlias creates an alias for a client HANDLER method - for handlers created with WithClientHandler

func WithErrors

func WithErrors(es Errors) func(c *Config)

func WithHTTPClient

func WithHTTPClient(h *http.Client) func(c *Config)

func WithNoReconnect

func WithNoReconnect() func(c *Config)

func WithParamEncoder

func WithParamEncoder(t interface{}, encoder ParamEncoder) func(c *Config)

func WithPingInterval

func WithPingInterval(d time.Duration) func(c *Config)

Must be < Timeout/2

func WithReconnectBackoff

func WithReconnectBackoff(minDelay, maxDelay time.Duration) func(c *Config)

func WithTimeout

func WithTimeout(d time.Duration) func(c *Config)

Types

type ClientCloser

type ClientCloser func()

ClientCloser is used to close Client from further use

func NewClient

func NewClient(ctx context.Context, addr string, namespace string, handler interface{}, requestHeader http.Header) (ClientCloser, error)

NewClient creates new jsonrpc 2.0 client

handler must be pointer to a struct with function fields Returned value closes the client connection TODO: Example

func NewMergeClient

func NewMergeClient(ctx context.Context, addr string, namespace string, outs []interface{}, requestHeader http.Header, opts ...Option) (ClientCloser, error)

NewMergeClient is like NewClient, but allows to specify multiple structs to be filled in the same namespace, using one connection

type Config

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

type ErrClient

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

ErrClient is an error which occurred on the client side the library

func (*ErrClient) Error

func (e *ErrClient) Error() string

func (*ErrClient) Unwrap

func (e *ErrClient) Unwrap() error

Unwrap unwraps the actual error

type ErrorCode

type ErrorCode int

type Errors

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

func NewErrors

func NewErrors() Errors

func (*Errors) Register

func (e *Errors) Register(c ErrorCode, typ interface{})

type Option

type Option func(c *Config)

type ParamDecoder

type ParamDecoder func(ctx context.Context, json []byte) (reflect.Value, error)

type ParamEncoder

type ParamEncoder func(reflect.Value) (reflect.Value, error)

type RPCConnectionError

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

func (*RPCConnectionError) Error

func (e *RPCConnectionError) Error() string

func (*RPCConnectionError) Unwrap

func (e *RPCConnectionError) Unwrap() error

type RPCServer

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

RPCServer provides a jsonrpc 2.0 http server handler

func NewServer

func NewServer(opts ...ServerOption) *RPCServer

NewServer creates new RPCServer instance

func (*RPCServer) AliasMethod

func (s *RPCServer) AliasMethod(alias, original string)

func (*RPCServer) Register

func (s *RPCServer) Register(namespace string, handler interface{})

Register registers new RPC handler

Handler is any value with methods defined

func (*RPCServer) ServeHTTP

func (s *RPCServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

TODO: return errors to clients per spec

type RawParams

type RawParams json.RawMessage

type ServerConfig

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

type ServerOption

type ServerOption func(c *ServerConfig)

func WithMaxRequestSize

func WithMaxRequestSize(max int64) ServerOption

func WithParamDecoder

func WithParamDecoder(t interface{}, decoder ParamDecoder) ServerOption

func WithReverseClient

func WithReverseClient[RP any](namespace string) ServerOption

WithReverseClient will allow extracting reverse client on **WEBSOCKET** calls. RP is a proxy-struct type, much like the one passed to NewClient.

func WithServerErrors

func WithServerErrors(es Errors) ServerOption

func WithServerPingInterval

func WithServerPingInterval(d time.Duration) ServerOption

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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