rmb-sdk-go – github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go Index | Files | Directories

package rmb

import "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go"

Index

Constants

const (
	DefaultSchema = "application/json"

	// DefaultAddress default redis address when no address is passed
	DefaultAddress = "tcp://127.0.0.1:6379"
)

Variables

var (
	// ErrFunctionNotFound is an err returned if the handler function is not found
	ErrFunctionNotFound = fmt.Errorf("function not found")
)

Functions

func GetTwinID

func GetTwinID(ctx context.Context) uint32

GetTwinID returns the twin id from context.

func LoggerMiddleware

func LoggerMiddleware(ctx context.Context, payload []byte) (context.Context, error)

LoggerMiddleware simple logger middleware.

func NewRedisPool

func NewRedisPool(address string, size ...uint32) (*redis.Pool, error)

Types

type Client

type Client interface {
	Call(ctx context.Context, twin uint32, fn string, data interface{}, result interface{}) error
}

Client is an rmb abstract client interface.

func Default

func Default() (Client, error)

Default return instance of to default (local) rmb shortcut for NewClient(DefaultAddress)

func NewRMBClient

func NewRMBClient(address string, poolSize ...uint32) (Client, error)

NewRMBClient creates a new rmb client that runs behind an rmb-peer. This client does not talk to the rmb relay directly, instead talk to an rmb-peer instance (like a gateway) that itself maintains a connection to the relay. the rmb-peer does all the heavy lifting, including signing, encryption, validation of the response, etc...

hence the address in this case, is an address to the local redis that must be the same one used with the rmb-peer process.

for more details about rmb-peer please check https://github.com/threefoldtech/rmb-rs Since the rmb protocol does not specify a "payload" format this Client and the DefaultRouter both uses json to encode and decode the rpc body. Hence this client should be always 100% compatible with services built with the DefaultRouter.

type DefaultRouter

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

DefaultRouter implements Router interface. It then can be used to register handlers to quickly implement servers that are callable over RMB.

func NewRouter

func NewRouter(address string) (*DefaultRouter, error)

NewRouter creates a new default router. with the local redis address. Normally you want to do NewRouter(DefaultAddress)

func (*DefaultRouter) Handlers

func (m *DefaultRouter) Handlers() []string

Handlers return full name of all registered handlers

func (*DefaultRouter) Run

func (m *DefaultRouter) Run(ctx context.Context) error

Run runs listeners to the configured handlers and will trigger the handlers in the case an event comes in

func (*DefaultRouter) Subroute

func (m *DefaultRouter) Subroute(prefix string) Router

func (*DefaultRouter) Use

func (m *DefaultRouter) Use(mw Middleware)

func (*DefaultRouter) WithHandler

func (m *DefaultRouter) WithHandler(topic string, handler Handler)

WithHandler adds a topic handler to the messagebus

type Error

type Error struct {
	Code    uint32 `json:"code"`
	Message string `json:"message"`
}

type Handler

type Handler func(ctx context.Context, payload []byte) (interface{}, error)

Handler is a handler function type

type Incoming

type Incoming struct {
	Version    int    `json:"ver"`
	Reference  string `json:"ref"`
	Command    string `json:"cmd"`
	Expiration int    `json:"exp"`
	Data       string `json:"dat"`
	TwinSrc    string `json:"src"`
	RetQueue   string `json:"ret"`
	Schema     string `json:"shm"`
	Epoch      int64  `json:"now"`
}

Incoming request that need to be handled by servers

func GetRequest

func GetRequest(ctx context.Context) Incoming

GetRequest gets a message from the context, panics if it's not there

func (*Incoming) GetPayload

func (m *Incoming) GetPayload() ([]byte, error)

GetPayload returns the payload for a message's data

type IncomingResponse

type IncomingResponse struct {
	Version   int    `json:"ver"`
	Reference string `json:"ref"`
	Data      string `json:"dat"`
	TwinSrc   string `json:"src"`
	Schema    string `json:"shm"`
	Epoch     int64  `json:"now"`
	Error     *Error `json:"err,omitempty"`
}

type Middleware

type Middleware func(ctx context.Context, payload []byte) (context.Context, error)

Middleware is middleware function type

type OutgoingResponse

type OutgoingResponse struct {
	Version   int    `json:"ver"`
	Reference string `json:"ref"`
	Data      string `json:"dat"`
	TwinDest  string `json:"dst"`
	Schema    string `json:"shm"`
	Epoch     int64  `json:"now"`
	Error     *Error `json:"err,omitempty"`
}

type RemoteError

type RemoteError struct {
	Code    uint32
	Message string
}

func (RemoteError) Error

func (e RemoteError) Error() string

type Request

type Request struct {
	Version    int      `json:"ver"`
	Reference  string   `json:"ref"`
	Command    string   `json:"cmd"`
	Expiration int      `json:"exp"`
	Data       string   `json:"dat"`
	TwinDest   []uint32 `json:"dst"`
	Session    *string  `json:"con"`
	RetQueue   string   `json:"ret"`
	Schema     string   `json:"shm"`
	Epoch      int64    `json:"now"`
}

Request is an outgoing request struct used to make rpc calls over rmb

func (*Request) GetPayload

func (m *Request) GetPayload() ([]byte, error)

GetPayload returns the payload for a message's data

type Router

type Router interface {
	WithHandler(route string, handler Handler)
	Subroute(route string) Router
	Use(Middleware)
}

Router is the router interface

Source Files

client.go interface.go mw.go redis.go rmb.go

Directories

PathSynopsis
examples
examples/client
examples/rpc_client
examples/server
peerPackage direct package provides the functionality to create a direct websocket connection to rmb relays without the need to rmb peers.
peer/encoder
peer/examples
peer/examples/peer
peer/examples/peer_pingmany
peer/examples/router_server
peer/examples/rpc
peer/types
Version
v0.16.6 (latest)
Published
Mar 17, 2025
Platform
linux/amd64
Imports
11 packages
Last checked
3 hours ago

Tools for package owners.