rpc

package
v0.6.2-0...-fc8535a Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RemoteIDContextKey key = iota

	DefaultResponseBufferLen = 1024
)

Variables

View Source
var (
	ErrNotAFunction = errors.New("not a function")

	ErrInvalidArgsCount = errors.New("invalid argument count")
	ErrInvalidArg       = errors.New("invalid argument")

	ErrClosureDoesNotExist = errors.New("closure does not exist")
)
View Source
var (
	ErrInvalidReturn = errors.New("invalid return, can only return an error or a value and an error")
	ErrInvalidArgs   = errors.New("invalid arguments, first argument needs to be a context.Context")

	ErrCannotCallNonFunction = errors.New("can not call non function")
)

Functions

func GetRemoteID

func GetRemoteID(ctx context.Context) string

Types

type Message

type Message[T any] struct {
	Request  *T `json:"request"`
	Response *T `json:"response"`
}

type Options

type Options struct {
	OnClientConnect    func(remoteID string)
	OnClientDisconnect func(remoteID string)
}

type Registry

type Registry[R, T any] struct {
	// contains filtered or unexported fields
}

func NewRegistry

func NewRegistry[R, T any](
	local any,

	ctx context.Context,

	options *Options,
) *Registry[R, T]

func (Registry[R, T]) ForRemotes

func (r Registry[R, T]) ForRemotes(
	cb func(remoteID string, remote R) error,
) error

func (Registry[R, T]) LinkMessage

func (r Registry[R, T]) LinkMessage(
	writeRequest,
	writeResponse func(b T) error,

	readRequest,
	readResponse func() (T, error),

	marshal func(v any) (T, error),
	unmarshal func(data T, v any) error,
) error

func (Registry[R, T]) LinkStream

func (r Registry[R, T]) LinkStream(
	encode func(v Message[T]) error,
	decode func(v *Message[T]) error,

	marshal func(v any) (T, error),
	unmarshal func(data T, v any) error,
) error

type Request

type Request[T any] struct {
	Call     string `json:"call"`
	Function string `json:"function"`
	Args     []T    `json:"args"`
}

type Response

type Response[T any] struct {
	Call  string `json:"call"`
	Value T      `json:"value"`
	Err   string `json:"err"`
}

Jump to

Keyboard shortcuts

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