natsrpc

package module
v0.0.0-...-0e81397 Latest Latest
Warning

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

Go to latest
Published: May 31, 2023 License: Apache-2.0 Imports: 9 Imported by: 1

README

nats-rpc

Protocol agnostic Remote Procedure Call (RPC) framework over NATS.

WARNING: Work in progress! Expect API changes and things to break in unexpected ways.

Supported protocols

Protocol Project
JSON-RPC 2.0 nats-json-rpc

Concepts

Server

Server consumes request messages on NATS subjects and forwards them to registered Services, as well as produces reply messages.

Server can consume one or many subjects, as well as have one or many registered Services per subject.

All services must implement Service interface.

Service

Service encodes/decodes request/response messages according to the rules of its protocol, and calls appropriate RPC methods in bound Receivers.

Service can bind one or many Receivers, however each Receiver must have a unique name.

Receiver

Receiver provides implementation of RPC methods. It receives the decoded data from a Service as a parameter to method.

Receiver must be Service agnostic, meaning, that a Receiver must be compatible with any Service.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrReceiverNotFound = errors.New("receiver not found")
	ErrMethodNotFound   = errors.New("method not found")
)

Functions

This section is empty.

Types

type Mapper

type Mapper struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewMapper

func NewMapper() *Mapper

func (*Mapper) Call

func (m *Mapper) Call(receiver, method string, params reflect.Value) (reflect.Value, error)

func (*Mapper) IsDefined

func (m *Mapper) IsDefined(receiver, method string) bool

func (*Mapper) Params

func (m *Mapper) Params(receiver, method string) (reflect.Value, error)

func (*Mapper) Register

func (m *Mapper) Register(name string, receiver any)

func (*Mapper) Result

func (m *Mapper) Result(receiver, method string) (reflect.Value, error)

type Receiver

type Receiver struct {
	Name string
	R    any
}

type Server

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

func NewServer

func NewServer(nc *nats.Conn) *Server

func (*Server) Register

func (s *Server) Register(subject, group string, service Service)

Register registers a service with a NATS subject. Many services can be registered to the same subject, all services will receive their copy of the message. If group is specified, only of services subscribed to the same subject with the same group nam will receive a message.

func (*Server) StartWithContext

func (s *Server) StartWithContext(ctx context.Context) error

StartWithContext starts a new server and blocks until context is closed or an error occurs.

type Service

type Service interface {
	Start(ctx context.Context, reqCh <-chan *nats.Msg, respCh chan<- *nats.Msg) error
}

Jump to

Keyboard shortcuts

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