grav

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2020 License: Apache-2.0 Imports: 7 Imported by: 6

Documentation

Index

Constants

View Source
const (
	MsgTypeDefault string = "grav.default"
)

MsgTypeDefault and other represent message consts

Variables

View Source
var ErrMsgNotWanted = errors.New("message not wanted")

ErrMsgNotWanted is used by WaitOn to determine if the current message is what's being waited on

View Source
var (
	ErrTransportNotConfigured = errors.New("transport plugin not configured")
)

ErrTransportNotConfigured represent package-level vars

Functions

This section is empty.

Types

type ConnectFunc added in v0.0.4

type ConnectFunc func() *Pod

ConnectFunc represents a function that returns a pod conntected to Grav

type Grav

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

Grav represents a Grav message bus instance

func New

func New() *Grav

New creates a new Grav instance

func NewWithTransport added in v0.0.4

func NewWithTransport(tspt Transport) *Grav

NewWithTransport creates a new Grav with a transport plugin configured

func (*Grav) Connect

func (g *Grav) Connect() *Pod

Connect creates a new connection (pod) to the bus

func (*Grav) ConnectEndpoint added in v0.0.4

func (g *Grav) ConnectEndpoint(endpoint string) error

ConnectEndpoint uses the configured transport to connect the bus to an external endpoint

func (*Grav) Serve added in v0.0.4

func (g *Grav) Serve(opts *TransportServeOpts) error

Serve directs the configured transport plugin to serve an endpoint that other nodes can connect to Calling Serve will block, so if the Grav endpoint is meant to run in the background, it should be called on a goroutine. Calling Serve is optional if incoming connections are not needed (or if no transport is being used)

type Message added in v0.0.3

type Message interface {
	// Unique ID for this message
	UUID() string
	// ID of the parent event or request, such as HTTP request
	ParentID() string
	// The UUID of the message being responded to, if any
	ResponseTo() string
	// Type of message (application-specific)
	Type() string
	// Time the message was sent
	Timestamp() time.Time
	// Raw data of message
	Data() []byte
	// Unmarshal the message's data into a struct
	UnmarshalData(interface{}) error
	// Marshal the message itself to encoded bytes (JSON or otherwise)
	Marshal() ([]byte, error)
	// Unmarshal encoded Message into object
	Unmarshal([]byte) error
}

Message represents a message

func MsgFromBytes added in v0.0.3

func MsgFromBytes(bytes []byte) (Message, error)

MsgFromBytes returns a default _message that has been unmarshalled from bytes. Should only be used if the default _message type is being used.

func MsgFromRequest added in v0.0.3

func MsgFromRequest(r *http.Request) (Message, error)

MsgFromRequest extracts an encoded Message from an HTTP request

func NewMsg added in v0.0.3

func NewMsg(msgType string, data []byte) Message

NewMsg creates a new Message with the built-in `_message` type

func NewMsgResponseTo added in v0.0.3

func NewMsgResponseTo(msgType, responseTo string, data []byte) Message

NewMsgResponseTo creates a new message in response to a previous message

func NewMsgWithParentID added in v0.0.3

func NewMsgWithParentID(msgType, parentID string, data []byte) Message

NewMsgWithParentID returns a new message with the provided parent ID

type MsgChan added in v0.0.3

type MsgChan chan Message

MsgChan is a channel that accepts a message

type MsgFunc added in v0.0.3

type MsgFunc func(Message) error

MsgFunc is a callback function that accepts a message and returns an error

type Pod added in v0.0.3

type Pod struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Pod is a connection to Grav Pods are bi-directional. Messages can be sent to them from the bus, and they can be used to send messages to the bus. Pods are meant to be extremely lightweight with no persistence they are meant to quickly and immediately route a message between its owner and the Bus. The Bus is responsible for any "smarts". Messages coming from the bus are filtered using the pod's messageFilter, which is configurable by the caller.

func (Pod) FilterType added in v0.0.3

func (mf Pod) FilterType(msgType string, allow bool)

func (Pod) FilterUUID added in v0.0.3

func (mf Pod) FilterUUID(uuid string, allow bool)

FilterUUID likely should not be used in normal cases, it adds a message UUID to the pod's filter.

func (*Pod) On added in v0.0.3

func (p *Pod) On(onFunc MsgFunc)

On sets the function to be called whenever this pod recieves a message from the bus. If nil is passed, the pod will ignore all messages. Calling On multiple times causes the function to be overwritten. To recieve using two different functions, create two pods.

func (*Pod) OnType added in v0.0.3

func (p *Pod) OnType(onFunc MsgFunc, msgTypes ...string)

OnType sets the function to be called whenever this pod recieves a message and sets the pod's filter to only include certain message types

func (*Pod) Send added in v0.0.3

func (p *Pod) Send(msg Message)

Send emits a message to be routed to the bus

func (*Pod) WaitOn added in v0.0.3

func (p *Pod) WaitOn(onFunc MsgFunc) error

WaitOn takes a function to be called whenever this pod recieves a message and blocks until that function returns something other than ErrMsgNotWanted. WaitOn should be used if there is a need to wait for a particular message. When the onFunc returns something other than ErrMsgNotWanted (such as nil or a different error), WaitOn will return and set the onFunc to nil. If an error other than ErrMsgNotWanted is returned from the onFunc, it will be propogated to the caller.

type Transport added in v0.0.4

type Transport interface {
	// Serve is a transport-specific function that exposes a connection point
	Serve(*TransportServeOpts) error
	// ConnectEndpoint indicates to the Transport that a connection to a remote endpoint is needed
	ConnectEndpoint(string, ConnectFunc) error
}

Transport represents a Grav transport plugin

type TransportServeOpts added in v0.0.7

type TransportServeOpts struct {
	Port   int
	Custom interface{}
}

TransportServeOpts is a set of options for transports

func DefaultTransportServeOpts added in v0.0.8

func DefaultTransportServeOpts() *TransportServeOpts

DefaultTransportServeOpts returns the default Grav Transport options

Jump to

Keyboard shortcuts

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