rpc

package
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: CC0-1.0 Imports: 31 Imported by: 2

Documentation

Index

Constants

View Source
const (
	DefaultContentType = "application/json"
)

Variables

This section is empty.

Functions

func PutNotifierKey

func PutNotifierKey(ctx context.Context, n *Notifier) context.Context

PutNotifierKey is a helper function to put Notifier in the context so that subscription handlers can use it

Types

type HttpConfig

type HttpConfig struct {
	HttpEndpoint       string
	HttpPathPrefix     string
	HttpCors           []string
	HttpCompress       bool
	HttpTimeout        time.Duration
	WsEndpoint         string
	WsPathPrefix       string
	WsHandshakeTimeout time.Duration
	WsOnly             bool
}

HttpConfig holds both http and websocket configuration elements

type HttpServer

type HttpServer struct {
	Logger *log.Logger
	Config HttpConfig
	// contains filtered or unexported fields
}

func (*HttpServer) Run

func (h *HttpServer) Run(ctx context.Context, resolver Resolver) error

Run enables CORS and Compression handlers if enabled on configuration and starts the fasthttp server

func (*HttpServer) Stop

func (h *HttpServer) Stop() error

Stop stops http server

type ID

type ID string

ID defines a pseudo random number that is used to identify RPC subscriptions.

func NewID

func NewID() ID

NewID returns a new, random ID.

type Middleware

type Middleware func(handler RpcHandler) RpcHandler

type Notifier

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

Notifier is tied to a RPC connection that supports subscriptions. Server callbacks use the notifier to send notifications.

func NotifierFromContext

func NotifierFromContext(ctx context.Context) (*Notifier, bool)

NotifierFromContext returns the Notifier value stored in ctx, if any.

func (*Notifier) CreateSubscription

func (n *Notifier) CreateSubscription() *Subscription

CreateSubscription returns a new subscription that is coupled to the RPC connection

func (*Notifier) Notify

func (n *Notifier) Notify(id ID, data interface{}) error

Notify sends a notification to the client with the given data as payload.

type Resolver

type Resolver interface {
	ResolveHttp(ctx *context.Context, rpcMessage []byte) []byte
	ResolveWs(ctx *context.Context, wsCtx *WebSocketContext, rpcMessage []byte) []byte
	CloseWsConn(wsCtx *WebSocketContext)
}

type RpcContext

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

func (*RpcContext) GetBody

func (ctx *RpcContext) GetBody() []byte

GetBody returns the body as a byte slice

func (*RpcContext) GetMethod

func (ctx *RpcContext) GetMethod() string

GetMethod returns the method name

func (*RpcContext) SetErrorObject

func (ctx *RpcContext) SetErrorObject(e errs.Error) *RpcContext

SetErrorObject sets response as error using the provided error object

func (*RpcContext) SetResponse

func (ctx *RpcContext) SetResponse(response []byte) *RpcContext

SetResponse sets whole response using the provided byte slice

type RpcHandler

type RpcHandler func(ctx *context.Context, rpcCtx *RpcContext) *RpcContext

type RpcServer

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

func New

func New(l *log.Logger, maxBatchReq uint, transports ...TransportOption) *RpcServer

func (*RpcServer) Close

func (r *RpcServer) Close()

Close gracefully stops RpcServer

func (*RpcServer) CloseWsConn

func (r *RpcServer) CloseWsConn(wsCtx *WebSocketContext)

func (*RpcServer) RegisterEndpoints

func (r *RpcServer) RegisterEndpoints(nameSpace string, sh any) error

RegisterEndpoints creates a map of service handlers for the given receiver by adding its exposed methods and their arguments

func (*RpcServer) RegisterEvents

func (r *RpcServer) RegisterEvents(nameSpace string, sh any) error

RegisterEvents creates a map of subscription handlers for the given receiver by adding its exposed methods and their arguments

func (*RpcServer) ResolveHttp

func (r *RpcServer) ResolveHttp(ctx *context.Context, rpcMessage []byte) []byte

func (*RpcServer) ResolveWs

func (r *RpcServer) ResolveWs(ctx *context.Context, wsCtx *WebSocketContext, rpcMessage []byte) []byte

func (*RpcServer) Run

func (r *RpcServer) Run(ctx context.Context) error

Run starts the configured transports (HTTP and WS) for the json rpc server

func (*RpcServer) WithMiddleware

func (r *RpcServer) WithMiddleware(m Middleware)

WithMiddleware places the given handler function to the middlewares chain.

type ServiceMap

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

ServiceMap holds all the services and subscriptions

type Subscription

type Subscription struct {
	ID ID
	// contains filtered or unexported fields
}

A Subscription is created by a notifier and tied to that notifier. The client can use this subscription to wait for an unsubscribe request for the client, see Err().

func (*Subscription) Err

func (s *Subscription) Err() <-chan error

Err returns a channel that is closed when the client send an unsubscribe request.

type Transport

type Transport interface {
	Run(ctx context.Context, resolver Resolver) error
	Stop() error
}

type TransportOption

type TransportOption func(s *RpcServer)

func WithTransport

func WithTransport(transport Transport) TransportOption

type WebSocketContext

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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