rpc

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2026 License: MIT Imports: 5 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 is JSON-RPC 2.0 over whole WebSocket messages: one object per frame, no batches. Both Perch daemons (perch-apd, perch-collector) and the controller speak it the same way.

Index

Constants

View Source
const (
	CodeParseError     = -32700
	CodeInvalidRequest = -32600
	CodeMethodNotFound = -32601
	CodeInvalidParams  = -32602
	CodeInternal       = -32603
	CodeCommandFailed  = -32000
	CodeUnsupported    = -32001
	CodeNotFound       = -32002
)

Error codes.

Variables

This section is empty.

Functions

func Decode

func Decode(frame []byte) (*Message, *Message)

Decode parses one frame. A parse failure returns a ready-to-send error response (id null).

func Notification

func Notification(method string, params any) ([]byte, error)

Notification builds a notification message.

func Params

func Params(raw json.RawMessage, v any) error

Params decodes params into v; empty or null params leave v unchanged.

func Request

func Request(id int64, method string, params any) ([]byte, error)

Request builds a request message with a numeric id.

Types

type Dispatcher

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

Dispatcher routes requests to handlers.

func NewDispatcher

func NewDispatcher() *Dispatcher

NewDispatcher returns an empty dispatcher.

func (*Dispatcher) Methods

func (d *Dispatcher) Methods() []string

Methods lists registered method names.

func (*Dispatcher) Register

func (d *Dispatcher) Register(method string, h Handler)

Register adds a method.

func (*Dispatcher) Serve

func (d *Dispatcher) Serve(ctx context.Context, m *Message) *Message

Serve runs a request and returns its response (nil for notifications).

type Error

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

Error is a JSON-RPC error object; it is also a Go error.

func Errorf

func Errorf(code int, format string, args ...any) *Error

Errorf builds an *Error.

func (*Error) Error

func (e *Error) Error() string

type Handler

type Handler func(ctx context.Context, params json.RawMessage) (any, error)

Handler serves one method. Returning an *Error sends it as is; any other error becomes -32000 "command failed".

type Message

type Message struct {
	JSONRPC string          `json:"jsonrpc"`
	ID      json.RawMessage `json:"id,omitempty"`
	Method  string          `json:"method,omitempty"`
	Params  json.RawMessage `json:"params,omitempty"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   *Error          `json:"error,omitempty"`
}

Message is any JSON-RPC 2.0 object: request, notification or response.

func (*Message) IsNotification

func (m *Message) IsNotification() bool

IsNotification: has a method, no id.

func (*Message) IsRequest

func (m *Message) IsRequest() bool

IsRequest: has a method and an id.

func (*Message) IsResponse

func (m *Message) IsResponse() bool

IsResponse: no method, an id.

Jump to

Keyboard shortcuts

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