Documentation
¶
Index ¶
- Constants
- Variables
- type ConnectFunc
- type Connection
- type Discovery
- type DiscoveryFunc
- type DiscoveryOpts
- type FindFunc
- type Grav
- type Message
- type MsgBuffer
- type MsgChan
- type MsgFunc
- type MsgReceipt
- type Options
- type OptionsModifier
- type Pod
- func (p *Pod) Disconnect()
- func (mf Pod) FilterType(msgType string, allow bool)
- func (mf Pod) FilterUUID(uuid string, allow bool)
- func (p *Pod) On(onFunc MsgFunc)
- func (p *Pod) OnType(msgType string, onFunc MsgFunc)
- func (p *Pod) ReplyTo(inReplyTo Message, msg Message) *MsgReceipt
- func (p *Pod) Send(msg Message) *MsgReceipt
- func (p *Pod) WaitOn(onFunc MsgFunc) error
- func (p *Pod) WaitUntil(timeout TimeoutFunc, onFunc MsgFunc) error
- type ReceiveFunc
- type TimeoutFunc
- type Transport
- type TransportHandshake
- type TransportHandshakeAck
- type TransportOpts
Constants ¶
const ( TransportMsgTypeHandshake = 1 TransportMsgTypeUser = 2 )
TransportMsgTypeHandshake and others represent internal Transport message types used for handshakes and metadata transfer
const (
MsgTypeDefault string = "grav.default"
)
MsgTypeDefault and other represent message consts
Variables ¶
var ( ErrConnectionClosed = errors.New("connection was closed") ErrNodeUUIDMismatch = errors.New("handshake UUID did not match node UUID") )
ErrConnectionClosed and others are transport and connection related errors
var ErrMsgNotWanted = errors.New("message not wanted")
ErrMsgNotWanted is used by WaitOn to determine if the current message is what's being waited on
var ErrNoReceipt = errors.New("message receipt is nil")
ErrNoReceipt is returned when a method is called on a nil ticket
var (
ErrTransportNotConfigured = errors.New("transport plugin not configured")
)
ErrTransportNotConfigured represent package-level vars
var ErrWaitTimeout = errors.New("waited past timeout")
ErrWaitTimeout is returned if a timeout is exceeded
Functions ¶
This section is empty.
Types ¶
type ConnectFunc ¶ added in v0.0.4
type ConnectFunc func(Connection)
ConnectFunc is a function that provides a new Connection
type Connection ¶ added in v0.3.0
type Connection interface { // Called when the connection handshake is complete and the connection can actively start exchanging messages Start(recvFunc ReceiveFunc) // Send a message from the local instance to the connected node Send(msg Message) error // CanReplace returns true if the connection can be replaced (i.e. is not a persistent connection like a websocket) CanReplace() bool // Initiate a handshake for an outgoing connection and return the remote Ack DoOutgoingHandshake(handshake *TransportHandshake) (*TransportHandshakeAck, error) // Wait for an incoming handshake and return the provided Ack to the remote connection DoIncomingHandshake(handshakeAck *TransportHandshakeAck) (*TransportHandshake, error) // Close requests that the Connection close itself Close() }
Connection represents a connection to another node
type Discovery ¶ added in v0.1.0
type Discovery interface { // Start is called to start the Discovery plugin Start(*DiscoveryOpts, DiscoveryFunc) error }
Discovery represents a discovery plugin
type DiscoveryFunc ¶ added in v0.3.0
DiscoveryFunc is a function that allows a plugin to report a newly discovered node
type DiscoveryOpts ¶ added in v0.1.0
type DiscoveryOpts struct { NodeUUID string TransportPort string TransportURI string Logger *vlog.Logger Custom interface{} }
DiscoveryOpts is a set of options for transports
type FindFunc ¶ added in v0.3.0
type FindFunc func(uuid string) (Connection, bool)
FindFunc allows a Transport to query Grav for an active connection for the given UUID
type Grav ¶
type Grav struct { NodeUUID string // contains filtered or unexported fields }
Grav represents a Grav message bus instance
func (*Grav) ConnectEndpoint ¶ added in v0.0.4
ConnectEndpoint uses the configured transport to connect the bus to an external endpoint
func (*Grav) ConnectWithReplay ¶ added in v0.0.11
ConnectWithReplay creates a new connection (pod) to the bus and replays recent messages when the pod sets its onFunc
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 replied to, if any ReplyTo() string // Allow setting a message UUID that this message is a response to SetReplyTo(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
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
MsgFromRequest extracts an encoded Message from an HTTP request
func NewMsgReplyTo ¶ added in v0.1.0
func NewMsgReplyTo(ticket MsgReceipt, msgType string, data []byte) Message
NewMsgReplyTo creates a new message in response to a previous message
func NewMsgWithParentID ¶ added in v0.0.3
NewMsgWithParentID returns a new message with the provided parent ID
type MsgBuffer ¶ added in v0.3.3
type MsgBuffer struct {
// contains filtered or unexported fields
}
MsgBuffer is a buffer of messages with a particular size limit. Oldest messages are automatically evicted as new ones are added past said limit. Push() and Iter() are thread-safe.
func NewMsgBuffer ¶ added in v0.3.3
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
MsgFunc is a callback function that accepts a message and returns an error
type MsgReceipt ¶ added in v0.2.0
type MsgReceipt struct { UUID string // contains filtered or unexported fields }
MsgReceipt represents a "ticket" that references a message that was sent with the hopes of getting a response The embedded pod is a pointer to the pod that sent the original message, and therefore any ticket methods used will replace the OnFunc of the pod.
func (*MsgReceipt) OnReply ¶ added in v0.2.0
func (m *MsgReceipt) OnReply(mfn MsgFunc) error
OnReply will set the pod's OnFunc to the provided MsgFunc and set it to run asynchronously when a reply is received onFunc errors are discarded.
func (*MsgReceipt) WaitOn ¶ added in v0.2.0
func (m *MsgReceipt) WaitOn(onFunc MsgFunc) error
WaitOn will block until a response to the message is recieved and passes it to the provided onFunc. onFunc errors are propogated to the caller.
func (*MsgReceipt) WaitUntil ¶ added in v0.2.0
func (m *MsgReceipt) WaitUntil(timeout TimeoutFunc, onFunc MsgFunc) error
WaitUntil will block until a response to the message is recieved and passes it to the provided onFunc. ErrWaitTimeout is returned if the timeout elapses, onFunc errors are propogated to the caller.
type Options ¶ added in v0.1.0
type Options struct { Logger *vlog.Logger Transport Transport Discovery Discovery Port string URI string }
Options represent Grav options
type OptionsModifier ¶ added in v0.1.0
type OptionsModifier func(*Options)
OptionsModifier is function that modifies an option
func UseDiscovery ¶ added in v0.1.0
func UseDiscovery(discovery Discovery) OptionsModifier
UseDiscovery sets the discovery plugin to be used
func UseEndpoint ¶ added in v0.3.0
func UseEndpoint(port, uri string) OptionsModifier
UseEndpoint sets the endpoint settings for the instance to broadcast for discovery Pass empty strings for either if you would like to keep the defaults (8080 and /meta/message)
func UseLogger ¶ added in v0.1.0
func UseLogger(logger *vlog.Logger) OptionsModifier
UseLogger allows a custom logger to be used
func UseTransport ¶ added in v0.1.0
func UseTransport(transport Transport) OptionsModifier
UseTransport sets the transport plugin to be used.
type Pod ¶ added in v0.0.3
type Pod struct {
// 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) Disconnect ¶ added in v0.1.1
func (p *Pod) Disconnect()
Disconnect indicates to the bus that this pod is no longer needed and should be disconnected. Sending will immediately become unavailable, and the pod will soon stop recieving messages.
func (Pod) FilterType ¶ added in v0.0.3
func (Pod) FilterUUID ¶ added in v0.0.3
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
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. Errors returned from the onFunc are interpreted as problems handling messages. Too many errors will result in the pod being disconnected. Failed messages will be replayed when messages begin to succeed. Returning an error is inadvisable unless there is a real problem handling messages.
func (*Pod) OnType ¶ added in v0.0.3
OnType sets the function to be called whenever this pod recieves a message and sets the pod's filter to only receive certain message types. The same rules as `On` about error handling apply to OnType.
func (*Pod) ReplyTo ¶ added in v0.1.0
func (p *Pod) ReplyTo(inReplyTo Message, msg Message) *MsgReceipt
ReplyTo sends a response to a message. The reply message's ticket is returned.
func (*Pod) Send ¶ added in v0.0.3
func (p *Pod) Send(msg Message) *MsgReceipt
Send emits a message to be routed to the bus If the returned ticket is nil, it means the pod was unable to send It is safe to call methods on a nil ticket, they will error with ErrNoTicket This means error checking can be done on a chained call such as err := p.Send(msg).Wait(...)
func (*Pod) WaitOn ¶ added in v0.0.3
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. WaitOn will block forever if the desired message is never found. Use WaitUntil if a timeout is desired.
func (*Pod) WaitUntil ¶ added in v0.2.0
func (p *Pod) WaitUntil(timeout TimeoutFunc, onFunc MsgFunc) error
WaitUntil 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), WaitUntil 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. A timeout can be provided. If the timeout is non-nil and greater than 0, ErrWaitTimeout is returned if the time is exceeded.
type ReceiveFunc ¶ added in v0.3.0
type ReceiveFunc func(msg Message)
ReceiveFunc is a function that allows passing along a received message
type TimeoutFunc ¶ added in v0.2.0
TimeoutFunc is a function that takes a value (a number of seconds) and returns a channel that fires after that given amount of time
func Timeout ¶ added in v0.2.0
func Timeout(seconds int) TimeoutFunc
Timeout returns a function that returns a channel that fires after the provided number of seconds have elapsed if the value passed is less than or equal to 0, the timeout will never fire
type Transport ¶ added in v0.0.4
type Transport interface { // Setup is a transport-specific function that allows bootstrapping // Setup can block forever if needed; for example if a webserver is bring run Setup(opts *TransportOpts, connFunc ConnectFunc, findFunc FindFunc) error // CreateConnection connects to an endpoint and returns CreateConnection(endpoint string) (Connection, error) }
Transport represents a Grav transport plugin
type TransportHandshake ¶ added in v0.1.0
type TransportHandshake struct {
UUID string `json:"uuid"`
}
TransportHandshake represents a handshake sent to a node that you're trying to connect to
type TransportHandshakeAck ¶ added in v0.1.0
type TransportHandshakeAck struct {
UUID string `json:"uuid"`
}
TransportHandshakeAck represents a handshake response