rpc

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 6 Imported by: 0 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 runs request/response/notification exchanges over a wire.Codec: a Client for the calling side and Serve for the serving side.

Index

Constants

This section is empty.

Variables

View Source
var Async = &struct{ async bool }{true}

Async, returned by a Handler as its result, means the handler will answer the request itself later via Conn.Reply / Conn.ReplyError (typically from a goroutine, so a slow request does not block the connection).

View Source
var ErrClosed = errors.New("rpc: connection closed")

ErrClosed is returned by calls on a closed client.

Functions

func Decode

func Decode(m *wire.Msg, v any) error

Decode unmarshals request params into v, mapping failures to bad_request.

func Serve

func Serve(ctx context.Context, rw io.ReadWriteCloser, h Handler) error

Serve reads requests from rw and dispatches them to h sequentially, in arrival order, until the connection ends or ctx is cancelled. It closes rw before returning.

Types

type Client

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

Client issues requests and dispatches incoming notifications.

func NewClient

func NewClient(rw io.ReadWriteCloser, onNotify func(*wire.Msg)) *Client

NewClient starts reading rw. onNotify (may be nil) receives every notification, sequentially, on the read goroutine — it must not block for long. Incoming requests are answered with unknown_method.

func (*Client) Call

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

Call sends a request and decodes the result into result (may be nil). A protocol error is returned as *wire.Error.

func (*Client) CallRaw

func (c *Client) CallRaw(ctx context.Context, method string, params json.RawMessage) (*wire.Msg, error)

CallRaw sends a request with raw params and returns the raw response message (Result or Error set). Used by the bridge to forward verbatim.

func (*Client) Close

func (c *Client) Close() error

Close closes the underlying connection.

func (*Client) Codec

func (c *Client) Codec() *wire.Codec

Codec exposes the codec (e.g. to write pre-encoded messages).

func (*Client) Done

func (c *Client) Done() <-chan struct{}

Done is closed when the connection ends.

func (*Client) Err

func (c *Client) Err() error

Err is the reason the connection ended (after Done).

func (*Client) Go

func (c *Client) Go(method string, params json.RawMessage, onResponse func(*wire.Msg))

Go sends a request without waiting for it. onResponse is called exactly once: with the response on the read goroutine — before any later message of the connection is dispatched, so a forwarded response stays ordered with the notifications around it — or with nil when the request could not be sent or the connection ended first. It must not block for long.

func (*Client) Notify

func (c *Client) Notify(method string, params any) error

Notify sends a notification.

type Conn

type Conn struct {
	*wire.Codec
	// contains filtered or unexported fields
}

Conn is the serving side of one connection.

func (*Conn) Close

func (c *Conn) Close() error

Close ends the connection.

func (*Conn) Context

func (c *Conn) Context() context.Context

Context is cancelled when the connection ends.

func (*Conn) ReplyResult

func (c *Conn) ReplyResult(id *int64, result any, err error) error

ReplyResult answers id with result or err (same rules as Handler).

type Handler

type Handler func(ctx context.Context, c *Conn, m *wire.Msg) (any, error)

Handler handles one request or notification. For notifications (m.ID == nil) the return values are ignored. Returning a *wire.Error sends that error; any other error is sent as "internal".

Jump to

Keyboard shortcuts

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