Documentation
¶
Index ¶
- Constants
- Variables
- func ContextFrom(opts json.Options) context.Context
- func ExtractReverseClient[C any](ctx context.Context) (C, bool)
- func HasReqCtx(ctx context.Context) bool
- func LimitReader(r io.Reader, n int64) io.Reader
- func LoadAssert[T any](loadf func(any) (any, bool), key any) (t T, ok bool)
- func NewTeeLogValue(r io.Reader) teeValue
- func OptionsLoad[T any](opts json.Options) (T, bool)
- func OptionsStore[T any](opts json.Options, value T) json.Options
- func RegisterError[T error](errs Errors, code ErrorCode)
- func WithClientHandler(ns string, hnd any) func(c *Config)
- func WithClientHandlerAlias(alias, original string) func(c *Config)
- func WithClientHandlerFormatter(namer MethodNameFormatter) func(c *Config)
- func WithContext(opts json.Options, ctx context.Context) json.Options
- func WithErrors(es Errors) func(c *Config)
- func WithHTTPClient(h *http.Client) func(c *Config)
- func WithJsonOptions(opts ...json.Options) func(c *Config)
- func WithLogger(logger *slog.Logger) func(c *Config)
- func WithMethodNameFormatter(namer MethodNameFormatter) func(c *Config)
- func WithNoReconnect() func(c *Config)
- func WithParamMarshaler[T any](fn MarshalerFunc[T]) func(c *Config)
- func WithParamUnmarshaler[T any](fn UnmarshalerFunc[*T]) func(c *ServerConfig)
- func WithPingInterval(d time.Duration) func(c *Config)
- func WithReconnectBackoff(minDelay, maxDelay time.Duration) func(c *Config)
- func WithResultMarshaler[T any](fn MarshalerFunc[T]) func(c *ServerConfig)
- func WithResultUnmarshaler[T any](fn UnmarshalerFunc[*T]) func(c *Config)
- func WithTimeout(d time.Duration) func(c *Config)
- func WithWebsocketDialer(d *websocket.Dialer) func(c *Config)
- type CaseStyle
- type ClientCloser
- func NewClient(ctx context.Context, addr string, namespace string, handler any, ...) (ClientCloser, error)
- func NewCustomClient(namespace string, outs []any, ...) (ClientCloser, error)
- func NewCustomReaderClient(ctx context.Context, namespace string, outs []any, ...) (ClientCloser, error)
- func NewMergeClient(ctx context.Context, addr string, namespace string, outs []any, ...) (ClientCloser, error)
- type Config
- type ConnectionType
- type ErrClient
- type ErrorCode
- type Errors
- type JSONRPCError
- type LimitedReader
- type MarshalerFunc
- type MethodNameFormatter
- type Object
- type Option
- type Optional
- type RPCConnectionError
- type RPCErrorCodec
- type RPCServer
- func (s *RPCServer) AliasMethod(alias, original string)
- func (h RPCServer) GetJsonOptions() json.Options
- func (s *RPCServer) GetLogger() *slog.Logger
- func (s *RPCServer) HandleRequest(ctx context.Context, r io.Reader, w io.Writer)
- func (s *RPCServer) Register(namespace string, handler any)
- func (s *RPCServer) ServeHTTP(w http.ResponseWriter, r *http.Request)
- type ServerConfig
- type ServerOption
- func WithMaxRequestSize(max int64) ServerOption
- func WithReverseClient[RP any](namespace string) ServerOption
- func WithServerErrors(es Errors) ServerOption
- func WithServerJsonOptions(opts ...json.Options) ServerOption
- func WithServerLogger(logger *slog.Logger) ServerOption
- func WithServerMethodNameFormatter(formatter MethodNameFormatter) ServerOption
- func WithServerPingInterval(d time.Duration) ServerOption
- func WithServerTimeout(d time.Duration) ServerOption
- func WithTracer(l Tracer) ServerOption
- type Tracer
- type TracerBytesFunc
- type TracerParamsFunc
- type TracerRequestFunc
- type UnmarshalerFunc
Constants ¶
const ( ProxyTagRetry = "retry" ProxyTagNotify = "notify" ProxyTagRPCMethod = "rpc_method" )
const ( ErrNoParam paramError = "no params when params is required" ErrShortParams paramError = "not enough params" ErrExtraParams paramError = "extra params" ErrNotAnArray paramError = "param value is not an array" )
const ( ErrExtraOptionalParams = paramError("non-nil optional params found after a nil one") ErrExtraVariadicParams = paramError("variadic params found after nil optional params") )
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.
const ErrWsExiting = cerrors.String("websocket routine exiting")
const FirstUserCode = 2
Variables ¶
var DefaultMethodNameFormatter = NewMethodNameFormatter(true, OriginalCase)
DefaultMethodNameFormatter is a pass-through formatter with default options.
var ErrDataTooLarge = errors.New("request bigger than maximum allowed")
Functions ¶
func ExtractReverseClient ¶
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 LimitReader ¶
LimitReader returns a Reader that reads from r but stops with ErrDataTooLarge after n bytes. The underlying implementation is a *LimitedReader.
func NewTeeLogValue ¶
func RegisterError ¶
func WithClientHandler ¶
func WithClientHandlerAlias ¶
WithClientHandlerAlias creates an alias for a client HANDLER method - for handlers created with WithClientHandler
func WithClientHandlerFormatter ¶
func WithClientHandlerFormatter(namer MethodNameFormatter) func(c *Config)
Just like WithMethodNameFormatter, but for client handlers.
func WithContext ¶
this is sort of black magic, but it allows us to pass context into json unmarshaling
func WithErrors ¶
func WithHTTPClient ¶
func WithJsonOptions ¶
func WithLogger ¶
func WithMethodNameFormatter ¶
func WithMethodNameFormatter(namer MethodNameFormatter) func(c *Config)
func WithNoReconnect ¶
func WithNoReconnect() func(c *Config)
func WithParamMarshaler ¶
func WithParamUnmarshaler ¶
func WithParamUnmarshaler[T any](fn UnmarshalerFunc[*T]) func(c *ServerConfig)
func WithReconnectBackoff ¶
func WithResultMarshaler ¶
func WithResultMarshaler[T any](fn MarshalerFunc[T]) func(c *ServerConfig)
func WithResultUnmarshaler ¶
func WithResultUnmarshaler[T any](fn UnmarshalerFunc[*T]) func(c *Config)
func WithTimeout ¶
func WithWebsocketDialer ¶
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 any, requestHeader http.Header, opts ...Option) (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 NewCustomClient ¶
func NewCustomClient(namespace string, outs []any, doRequest func(ctx context.Context, body []byte) (io.ReadCloser, error), opts ...Option) (ClientCloser, error)
NewCustomClient is like NewMergeClient in single-request (http) mode, except it allows for a custom doRequest function
func NewCustomReaderClient ¶
func NewCustomReaderClient(ctx context.Context, namespace string, outs []any, doRequest func(ctx context.Context, body io.ReadCloser) (io.ReadCloser, error), opts ...Option) (ClientCloser, error)
func NewMergeClient ¶
func NewMergeClient(ctx context.Context, addr string, namespace string, outs []any, 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
}
func (*Config) GetHTTPClient ¶
func (*Config) GetJsonOptions ¶
func (*Config) GetTimeout ¶
type ConnectionType ¶
type ConnectionType string
ConnectionType indicates the type of connection, this is set in the context and can be retrieved with GetConnectionType.
const ( // ConnectionTypeUnknown indicates that the connection type cannot be determined, likely because // it hasn't passed through an RPCServer. ConnectionTypeUnknown ConnectionType = "unknown" // ConnectionTypeHTTP indicates that the connection is an HTTP connection. ConnectionTypeHTTP ConnectionType = "http" // ConnectionTypeWS indicates that the connection is a WebSockets connection. ConnectionTypeWS ConnectionType = "websockets" )
func GetConnectionType ¶
func GetConnectionType(ctx context.Context) ConnectionType
GetConnectionType returns the connection type of the request if it was set by an RPCServer. A connection type of ConnectionTypeUnknown means that the connection type was not set.
type ErrClient ¶
type ErrClient struct {
// contains filtered or unexported fields
}
ErrClient is an error which occurred on the client side the library
type Errors ¶
type Errors struct {
// contains filtered or unexported fields
}
func (*Errors) Register
deprecated
Deprecated: Use RegisterError instead for type-safe operations
type JSONRPCError ¶
type JSONRPCError struct {
Code ErrorCode `json:"code"`
Message string `json:"message"`
Meta jsontext.Value `json:"meta,omitzero"`
Data any `json:"data,omitzero"`
}
func (*JSONRPCError) Error ¶
func (e *JSONRPCError) Error() string
func (*JSONRPCError) Unwrap ¶
func (e *JSONRPCError) Unwrap() error
type LimitedReader ¶
type MethodNameFormatter ¶
MethodNameFormatter is a function that takes a namespace and a method name and returns the full method name, sent via JSON-RPC. This is useful if you want to customize the default behaviour, e.g. send without the namespace or make it lowercase.
func NewMethodNameFormatter ¶
func NewMethodNameFormatter(includeNamespace bool, nameCase CaseStyle) MethodNameFormatter
NewMethodNameFormatter creates a new method name formatter based on the provided options.
type Object ¶
type Object[T any] struct { Value T `json:",embed"` }
Object is my solution for named params in golang. Use an Object[T] type as the only param (except for context) in method, then its corresponded json object will be for "params" field in rpc call. T must be a Go struct, jsontext.Value, map[~string]T, or an unnamed pointer to such types. You can use Object[jsontext.Value] to get the usage similar to the old RawParams type.
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 RPCErrorCodec ¶
type RPCErrorCodec interface {
FromJSONRPCError(JSONRPCError) error
ToJSONRPCError() (JSONRPCError, 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 (RPCServer) GetJsonOptions ¶
func (*RPCServer) HandleRequest ¶
type ServerConfig ¶
type ServerConfig struct {
// contains filtered or unexported fields
}
func (*ServerConfig) GetJsonOptions ¶
func (c *ServerConfig) GetJsonOptions() json.Options
func (*ServerConfig) GetLogger ¶
func (c *ServerConfig) GetLogger() *slog.Logger
type ServerOption ¶
type ServerOption func(c *ServerConfig)
func WithMaxRequestSize ¶
func WithMaxRequestSize(max int64) 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 WithServerJsonOptions ¶
func WithServerJsonOptions(opts ...json.Options) ServerOption
func WithServerLogger ¶
func WithServerLogger(logger *slog.Logger) ServerOption
func WithServerMethodNameFormatter ¶
func WithServerMethodNameFormatter(formatter MethodNameFormatter) ServerOption
func WithServerPingInterval ¶
func WithServerPingInterval(d time.Duration) ServerOption
func WithServerTimeout ¶
func WithServerTimeout(d time.Duration) ServerOption
func WithTracer ¶
func WithTracer(l Tracer) ServerOption
WithTracer allows the instantiator to trace the method calls and results. This is useful for debugging a client-server interaction.
type Tracer ¶
type Tracer struct {
OnParseError TracerBytesFunc
OnInvalidRequest TracerBytesFunc
OnInvalidMethod TracerRequestFunc
OnInvalidParams TracerRequestFunc
OnRequestError TracerParamsFunc
OnResponseError TracerParamsFunc
OnNotification TracerParamsFunc
OnSuccess TracerParamsFunc
}