srpc

package module
v0.0.0-...-f563263 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

README

srpc

Simple RPC library for Go

Documentation

Overview

Package srpc implements a simple RPC-over-HTTP library.

Index

Constants

View Source
const QueryKey = "srpc"

QueryKey is the key for the query parameter that sRPC will use to issue state-preserving requests.

Variables

View Source
var ErrBadOrigin = errors.New("bad connector origin")

ErrBadOrigin is returned when a bad origin is given to construct a Connector.

Functions

This section is empty.

Types

type Codec

type Codec[T any] struct {
	// ContentType is the HTTP Content-Type header to use for responses.
	ContentType string
	// KeepOpen tells this library to not close streams after client calls return.
	KeepOpen bool
	// Co encodes the given value to the returned io.Reader.
	//
	// Implementers have the guarantee that the returned reader will be copied with
	// io.Copy to the response writer, or used as request body/query on the client side.
	Co func(ctx context.Context, t T) (io.Reader, error)
	// Dec decodes data from a stream.
	Dec func(ctx context.Context, r io.Reader) (T, error)
}

Codec implements functions to enCode and Decode requests and responses.

Ideally

var value T
wire, _ := myCodec.Co(ctx, value)
got := myCodec.Dec(ctx, wire)
got == value

Should be true for every possible value of T.

func NewCodecJSON

func NewCodecJSON[T any]() Codec[T]

NewCodecJSON creates a new Codec that uses JSON as wire format.

func NewCodecSeq

func NewCodecSeq[T any]() Codec[iter.Seq2[T, error]]

NewCodecSeq constructs an iter.Seq codec that supports Server-Sent-Events.

type Endpoint

type Endpoint[Response, Request any] struct {
	// contains filtered or unexported fields
}

Endpoint represents a single procedure.

Both the client side and the server side can be constructed from this type.

func NewEndpoint

func NewEndpoint[Response, Request any](method, path string, resc Codec[Response], reqc Codec[Request]) Endpoint[Response, Request]

NewEndpoint constructs a new endpoint with the given codecs.

func NewEndpointJSON

func NewEndpointJSON[Response, Request any](method, path string) Endpoint[Response, Request]

NewEndpointJSON constructs an endpoint with the JSON codec.

func NewEndpontSeq

func NewEndpontSeq[Response, Request any](method, path string) Endpoint[iter.Seq2[Response, error], Request]

NewEndpontSeq constructs and endpoint with JSON request and Seq response.

func (*Endpoint[Response, Request]) Register

func (e *Endpoint[Response, Request]) Register(m Mux, p Procedure[Response, Request])

Register registers the endpoint on the mux, implemented by the procedure.

func (*Endpoint[Response, Request]) Remote

func (e *Endpoint[Response, Request]) Remote(conn *Transport) Procedure[Response, Request]

Remote returns the remote procedure, ready to be called.

The endpoint needs to be registered and served on the remote server.

func (*Endpoint[Response, Request]) RemoteWithOrigin

func (e *Endpoint[Response, Request]) RemoteWithOrigin(origin string) Procedure[Response, Request]

RemoteWithOrigin is like Remote, but it creates a transport for the given origin.

If the origin is invalid, RemoteWithOrigin panics.

type EndpointR

type EndpointR[Response any] Endpoint[Response, struct{}]

EndpointR is like Endpoint for functions that take no inputs.

func (*EndpointR[Response]) Register

func (e *EndpointR[Response]) Register(m Mux, h ProcedureR[Response])

Register is like Endpoint.Register for EndpointR.

func (*EndpointR[Response]) Remote

func (e *EndpointR[Response]) Remote(conn *Transport) ProcedureR[Response]

Remote is like Endpoint.Remote for EndpointR.

func (*EndpointR[Response]) RemoteWithOrigin

func (e *EndpointR[Response]) RemoteWithOrigin(origin string) ProcedureR[Response]

RemoteWithOrigin is like Endpoint.RemoteWithOrigin for EndpointR.

type EndpointW

type EndpointW[Request any] Endpoint[struct{}, Request]

EndpointW is like Endpoint but for functions that return no content.

func (*EndpointW[Request]) Register

func (e *EndpointW[Request]) Register(m Mux, h ProcedureW[Request])

Register is like Endpoint.Register for EndpointW.

func (*EndpointW[Request]) Remote

func (e *EndpointW[Request]) Remote(conn *Transport) ProcedureW[Request]

Remote is like Endpoint.Remote for EndpointW.

func (*EndpointW[Request]) RemoteWithOrigin

func (e *EndpointW[Request]) RemoteWithOrigin(origin string) ProcedureW[Request]

RemoteWithOrigin is like Endpoint.RemoteWithOrigin for EndpointW.

type ErrorResponse

type ErrorResponse interface {
	Status() int
	Message() string
}

ErrorResponse can be returned by handlers to control the error code when returning an error.

type Mux

type Mux interface {
	HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
}

Mux is the subset of *http.ServeMux that srpc requires to work.

type Procedure

type Procedure[Response, Request any] = func(ctx context.Context, req Request) (Response, error)

Procedure is a function that can be called remotely.

type ProcedureR

type ProcedureR[Response any] func(ctx context.Context) (Response, error)

ProcedureR is like Procedure for functions that take no parameters.

type ProcedureW

type ProcedureW[Request any] func(ctx context.Context, req Request) error

ProcedureW is like Procedure, but for functions that return no value.

type Transport

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

Transport can be used to connect to a remote Endpoint.

func NewTransport

func NewTransport(origin string, client *http.Client, cookies []*http.Cookie) (*Transport, error)

NewTransport creates a new Connector.

The only mandatory parameter is origin, which must have a "http" or "https" scheme, a valid domain, and must not contain any path or query.

type Validable

type Validable interface {
	Validate() error
}

Validable represents all requests that can be validated.

All requests that implement this and that are not valid will be automatically rejected.

IMPORTANT NOTE: the validation error is returned to the client, so it should be telling enough for a user to understand what is wrong, but it should not contain internal or secret information.

type WireError

type WireError struct {
	Msg  string
	Code int
}

WireError is an error that can be sent over the wire.

It allows to set HTTP status and message.

func (*WireError) Error

func (w *WireError) Error() string

Error implements [error].

func (*WireError) Message

func (w *WireError) Message() string

Message implements ErrorResponse.

func (*WireError) Status

func (w *WireError) Status() int

Status implements ErrorResponse.

Jump to

Keyboard shortcuts

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