rpc

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2026 License: GPL-3.0 Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package rpc implements bounded bidirectional JSON-RPC 2.0 over a byte stream.

Index

Constants

View Source
const (
	Version         = "2.0"
	MaxMessageBytes = 1 << 20
)

Variables

View Source
var (
	ErrClosed           = errors.New("json-rpc connection closed")
	ErrInboundSaturated = errors.New("json-rpc inbound method capacity exhausted")
	ErrOutcomeUnknown   = errors.New("json-rpc call outcome unknown")
	ErrProtocol         = errors.New("json-rpc protocol violation")
)

Functions

This section is empty.

Types

type Error

type Error struct {
	Code    int             `json:"code"`
	Message string          `json:"message"`
	Data    json.RawMessage `json:"data,omitempty"`
}

Error is a JSON-RPC error object.

func (*Error) Error

func (e *Error) Error() string

type Handler

type Handler func(context.Context, json.RawMessage) (any, *Error)

Handler processes one inbound request.

type Peer

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

Peer owns one bidirectional JSON-RPC connection. Serve must run while calls are made.

func NewPeer

func NewPeer(conn net.Conn) *Peer

NewPeer takes ownership of an established connection and closes it on shutdown. The connection must honor net.Conn's deadline and Close semantics so blocked I/O can be interrupted. Callers must not read, write, or set deadlines on conn after handing it to the peer.

func (*Peer) Call

func (p *Peer) Call(ctx context.Context, method string, params any, result any) error

Call sends a request and waits for its correlated response.

func (*Peer) CallEmpty

func (p *Peer) CallEmpty(ctx context.Context, method string, params any) error

CallEmpty sends a request whose successful response is exactly an empty JSON object. It rejects null, scalars, arrays, and objects with fields.

func (*Peer) Close

func (p *Peer) Close() error

Close terminates the connection and unblocks all calls. It is safe to repeat. It joins the transport workers without waiting for active handlers to return.

func (*Peer) Done

func (p *Peer) Done() <-chan struct{}

Done closes when the peer terminates.

func (*Peer) Handle

func (p *Peer) Handle(method string, handler Handler) error

Handle registers a method before Serve begins.

func (*Peer) HandleLossyNotification

func (p *Peer) HandleLossyNotification(method string, handler Handler) error

HandleLossyNotification registers a method whose notifications may be dropped, and counted, when the inbound method queue is saturated.

func (*Peer) Notify

func (p *Peer) Notify(ctx context.Context, method string, params any) error

Notify sends a critical notification and waits for its write to complete.

func (*Peer) Serve

func (p *Peer) Serve(ctx context.Context) error

Serve reads and dispatches messages until the connection, context, or protocol fails.

func (*Peer) TerminateProtocol

func (p *Peer) TerminateProtocol(cause error)

TerminateProtocol marks the peer unusable without waiting for active handlers to join. It is safe to call from a reentrant handler.

func (*Peer) TryNotifyLossy

func (p *Peer) TryNotifyLossy(method string, params any) (bool, error)

TryNotifyLossy attempts to enqueue a noncritical notification without blocking. A false result with a nil error means the bounded outbound queue was full.

Jump to

Keyboard shortcuts

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