Documentation ¶
Overview ¶
Package roletalk is asynchronous peer-to-peer communication framework for microservices with its own transport protocol. It was developed in honor of scalability, simplicity and efficiency.
For detailed overview and use case please refer to readme.md in project's repository.
Index ¶
- type ConnectOptions
- type Datatype
- type Destination
- func (dest *Destination) HasUnit(unit *Unit) bool
- func (dest *Destination) Name() string
- func (dest *Destination) NewReader(event string, opts EmitOptions) (res *MessageContext, r *Readable, err error)
- func (dest *Destination) NewWriter(event string, opts EmitOptions) (res *MessageContext, writable *Writable, err error)
- func (dest *Destination) OnClose(f func())
- func (dest *Destination) OnUnit(f func(unit *Unit))
- func (dest *Destination) Ready() bool
- func (dest *Destination) Request(event string, opts EmitOptions) (res *MessageContext, err error)
- func (dest *Destination) Send(event string, opts EmitOptions) error
- func (dest *Destination) Units() []*Unit
- type EmitOptions
- type MessageContext
- type MessageHandler
- type MetaInfo
- type OriginData
- type Peer
- func (peer *Peer) AddKey(id, key string)
- func (peer *Peer) Close()
- func (peer *Peer) Connect(urlStr string, opts ...ConnectOptions) (unit *Unit, err error)
- func (peer *Peer) Destination(name string) *Destination
- func (peer *Peer) ID() string
- func (peer *Peer) InvolveConn(c *websocket.Conn) (*Unit, error)
- func (peer *Peer) ListDestinations() []string
- func (peer *Peer) ListRoles() []string
- func (peer *Peer) Listen(address string) (net.Addr, error)
- func (peer *Peer) OnRole(f func(role *Role))
- func (peer *Peer) OnUnit(f func(unit *Unit))
- func (peer *Peer) Role(name string) *Role
- func (peer *Peer) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (peer *Peer) Unit(id string) *Unit
- func (peer *Peer) Units() []*Unit
- func (peer *Peer) WaitForClose()
- type PeerOptions
- type Readable
- type ReadableRequestHandler
- type ReaderRequestContext
- type RequestContext
- type RequestHandler
- type Role
- func (role *Role) Active() bool
- func (role *Role) Disable()
- func (role *Role) Enable()
- func (role *Role) Name() string
- func (role *Role) OnMessage(event string, handler func(im *MessageContext))
- func (role *Role) OnReader(event string, handler func(ctx *ReaderRequestContext))
- func (role *Role) OnRequest(event string, handler func(im *RequestContext))
- func (role *Role) OnStatusChange(fnc func())
- func (role *Role) OnWriter(event string, handler func(ctx *WriterRequestContext))
- type Unit
- func (unit *Unit) Close()
- func (unit *Unit) Connected() bool
- func (unit *Unit) Friendly() bool
- func (unit *Unit) GetRoles() []string
- func (unit *Unit) HasRole(name string) bool
- func (unit *Unit) ID() string
- func (unit *Unit) Meta() MetaInfo
- func (unit *Unit) Name() string
- func (unit *Unit) OnClose(f func(err error))
- type Writable
- type WritableRequestHandler
- type WriterRequestContext
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConnectOptions ¶
type ConnectOptions struct { DoNotReconnect bool //set true if connection is not supposed to reconnect after abort DoNotAcquaint bool //set true if connection is not supposed to be introduced to remote peers nor to be acquainted with remote peers InsecureTLS bool //set true if TLS errors are supposed to be ignoredt }
ConnectOptions specifies options for outgoing connection
type Datatype ¶
type Datatype byte
Datatype represents type of data defined by roletalk communication protocol. Can be checked with corresponding constants. Implements Stringer (https://golang.org/pkg/fmt/#Stringer) interface
const ( //DatatypeBinary represents []byte DatatypeBinary Datatype = 0 //DatatypeNull represents nil DatatypeNull Datatype = 1 //DatatypeBool represents bool DatatypeBool Datatype = 2 //DatatypeString represents string DatatypeString Datatype = 3 //DatatypeNumber represents float64 DatatypeNumber Datatype = 4 //DatatypeJSON represents []byte of JSON stringified object DatatypeJSON Datatype = 5 )
type Destination ¶
type Destination struct {
// contains filtered or unexported fields
}
Destination represents a role (a service name) of remote peers (units). Destination is used as a gateway for outgoing communication. It implements round-robin load balancing between units. To communicate with specific remote peer (unit) use EmitOptions
func (*Destination) HasUnit ¶
func (dest *Destination) HasUnit(unit *Unit) bool
HasUnit returns true if provided unit serves a role with Destination's name
func (*Destination) NewReader ¶
func (dest *Destination) NewReader(event string, opts EmitOptions) (res *MessageContext, r *Readable, err error)
NewReader requests for creating binary stream session and returns its readable end. Returns error if remote peer rejected the request or request timed out
func (*Destination) NewWriter ¶
func (dest *Destination) NewWriter(event string, opts EmitOptions) (res *MessageContext, writable *Writable, err error)
NewWriter requests for creating binary stream session and returns its writable end. Returns error if remote peer rejected the request or request timed out
func (*Destination) OnClose ¶
func (dest *Destination) OnClose(f func())
OnClose adds handler function f which runs synchronosly with other close handlers in FIFO order when last Unit gets disconnected
func (*Destination) OnUnit ¶
func (dest *Destination) OnUnit(f func(unit *Unit))
OnUnit adds handler function f which executes synchronosly with other unit handlers in FIFO order when it gets new unit
func (*Destination) Ready ¶
func (dest *Destination) Ready() bool
Ready indicates whether Destination has connected units
func (*Destination) Request ¶
func (dest *Destination) Request(event string, opts EmitOptions) (res *MessageContext, err error)
Request emits request message to remote peer (Unit). Returns error if remote peer rejected the request or request timed out, otherwise returns response context
func (*Destination) Send ¶
func (dest *Destination) Send(event string, opts EmitOptions) error
Send sends one-way message to remote peer (Unit). Returns error if message has not been written to underlying connection
func (*Destination) Units ¶
func (dest *Destination) Units() []*Unit
Units returns slice of all connected units serving corresponding role
type EmitOptions ¶
EmitOptions determines Data to send and additional transfer options. All fields are optional. Specify Unit to send data to; Timeout for callback (Timeout option is ignored for Send and Broadcast methods); If IgnoreUnitClose is true, request will not be rejected internally when communicated unit disconnects, but timeout will still has its place.
type MessageContext ¶
type MessageContext struct { Data interface{} //Payload of message. Feel free to be change it on your needs // contains filtered or unexported fields }
MessageContext is context for all types of incoming messages, including ones for readable and writable streams. Data option is allowed to be changed by middleware. To get original data call OriginData(). Note: MessageContext has no relation to type Context (https://golang.org/pkg/context/#Context)
func (*MessageContext) Conn ¶
func (ctx *MessageContext) Conn() *websocket.Conn
Conn returns underlying connections which was used to transfer the message
func (*MessageContext) Event ¶
func (ctx *MessageContext) Event() string
Event returns the message's event name
func (*MessageContext) OriginData ¶
func (ctx *MessageContext) OriginData() OriginData
OriginData returns unchanged context's data.
func (*MessageContext) Role ¶
func (ctx *MessageContext) Role() string
Role returns the role which message is addressed to
func (*MessageContext) String ¶
func (ctx *MessageContext) String() string
func (*MessageContext) Unit ¶
func (ctx *MessageContext) Unit() *Unit
Unit returns Unit who sent the message
type MessageHandler ¶
type MessageHandler func(im *MessageContext)
MessageHandler is function which handles incoming messages.
type MetaInfo ¶
type MetaInfo struct { Os string `json:"os"` Runtime string `json:"runtime"` Uptime int64 `json:"uptime"` Time int64 `json:"time"` Protocol string `json:"protocol"` }
MetaInfo represents meta info of remote peer
type OriginData ¶
OriginData represents unchanged received context's data.
type Peer ¶
type Peer struct { Name string Friendly bool //Friendly means that Peer will follow acquaint messages from remote peers (Units) and connect to them if it isn't connected yet // contains filtered or unexported fields }
Peer represents the local node in your peer-to-peer architecture. Create Peer with NewPeer() only. It is recommended to use Singleton() instance instead.
func NewPeer ¶
func NewPeer(opts PeerOptions) *Peer
NewPeer creates Peer and initializes its internal state
func Singleton ¶
func Singleton() *Peer
Singleton returns singleton Peer instance. It is used to share single Peer between multiple places of your code
func (*Peer) AddKey ¶
AddKey adds provided authentication key with corresponded id. Only peers with matched id's will proceed to futher authentication. Peer without keys will approve remote peer (Unit).
func (*Peer) Connect ¶
func (peer *Peer) Connect(urlStr string, opts ...ConnectOptions) (unit *Unit, err error)
Connect establishes connection to remote peer and creates Unit
func (*Peer) Destination ¶
func (peer *Peer) Destination(name string) *Destination
Destination returns Destination with provided name or first creates it if such does not exist. Destination prepresents corresponding remote peers' roles
func (*Peer) InvolveConn ¶
InvolveConn accepts websocket.Conn for authentication and further communication
func (*Peer) ListDestinations ¶
ListDestinations returns all registered destinations
func (*Peer) Listen ¶
Listen to incoming connections. Creates new http.Server and blocks till it listens
func (*Peer) OnRole ¶
OnRole adds role handler function f which executes synchronosly with other role handlers in FIFO order when Peer gets new Role
func (*Peer) OnUnit ¶
OnUnit adds unit handler function f which executes synchronosly with other unit handlers in FIFO order when Peer gets new Unit
func (*Peer) Unit ¶
Unit returns referrence to Unit with provided id or nil if such does not exist or is not connected
func (*Peer) WaitForClose ¶
func (peer *Peer) WaitForClose()
WaitForClose waits until all units (their underlying connections) and listeners will be closed. Could be used to prevent Main() from returning
type PeerOptions ¶
PeerOptions provide options to create Peer
type Readable ¶
type Readable struct {
// contains filtered or unexported fields
}
Readable implements Reader
type ReadableRequestHandler ¶
type ReadableRequestHandler func(im *ReaderRequestContext)
ReadableRequestHandler is function which handles incoming requests.
type ReaderRequestContext ¶
type ReaderRequestContext struct { *RequestContext // contains filtered or unexported fields }
ReaderRequestContext is context for incoming request to establish binary stream readable on this end
func (*ReaderRequestContext) Reply ¶
func (ctx *ReaderRequestContext) Reply(data interface{}) (*Readable, error)
Reply stops middleware flow and responds to the message. If data argument is provided, it overwrites im.Data
func (*ReaderRequestContext) Then ¶
func (ctx *ReaderRequestContext) Then(cb func(ctx *ReaderRequestContext))
Then binds middleware to message context. Middleware runs in LIFO order
type RequestContext ¶
type RequestContext struct { *MessageContext Res interface{} Err error // contains filtered or unexported fields }
RequestContext is context for incoming requests
func (*RequestContext) OriginData ¶
func (ctx *RequestContext) OriginData() OriginData
OriginData returns unchanged received context's data.
func (*RequestContext) Reject ¶
func (ctx *RequestContext) Reject(data interface{}) error
Reject responds to request with error; data can be error, string or nil. If Err argument is nil, Err option will be taken for rejection
func (*RequestContext) Reply ¶
func (ctx *RequestContext) Reply(data interface{}) error
Reply stops middleware flow and responds to the message. If data argument is provided, it overrides Data option
func (*RequestContext) Then ¶
func (ctx *RequestContext) Then(cb func(ctx *RequestContext))
Then binds middleware to message context. Middleware runs in LIFO order
type RequestHandler ¶
type RequestHandler func(im *RequestContext)
RequestHandler is function which handles incoming requests.
type Role ¶
type Role struct {
// contains filtered or unexported fields
}
Role represents a service on the local Peer. It should handle incoming messages, requests and stream requests for certain functionality
func (*Role) Active ¶
Active is used to check the role's state; returns true if Peer serves the role.
func (*Role) Disable ¶
func (role *Role) Disable()
Disable stops peer to serve the role; immediately hides the role for all connected units
func (*Role) Enable ¶
func (role *Role) Enable()
Enable starts peer to serve the role; immediately shows the role to all connected units
func (*Role) OnMessage ¶
func (role *Role) OnMessage(event string, handler func(im *MessageContext))
OnMessage registers message handler for provided event. It does not support wildcard or regexp matching. Providing empty string as event sets handler for all messages despite the event
func (*Role) OnReader ¶
func (role *Role) OnReader(event string, handler func(ctx *ReaderRequestContext))
OnReader registers readable stream handler for provided event. It does not support wildcard or regexp matching. Providing empty string as event sets handler for all requests despite the event
func (*Role) OnRequest ¶
func (role *Role) OnRequest(event string, handler func(im *RequestContext))
OnRequest registers request handler for provided event. It does not support wildcard or regexp matching. Providing empty string as event sets handler for all requests despite the event
func (*Role) OnStatusChange ¶
func (role *Role) OnStatusChange(fnc func())
OnStatusChange registers handler for role status change (when it gets activated or deactivated)
func (*Role) OnWriter ¶
func (role *Role) OnWriter(event string, handler func(ctx *WriterRequestContext))
OnWriter registers writable stream handler for provided event. It does not support wildcard or regexp matching. Providing empty string as event sets handler for all requests despite the event
type Unit ¶
type Unit struct {
// contains filtered or unexported fields
}
Unit represents remote peer
func (*Unit) Connected ¶
Connected returns true if unit's underlying conn's have not been closed. If all conns's are closed or there was at least a moment when all conn's were closed, Connected returns false
Explanation: when all connections of a unit are closed, Peer gets rid of the unit. But you could still keep referrence to it. When unit gets reconnected, a new instance of type Unit is created, possibly with different ID. So Connected() method can be used to check whether unit is still attached to Peer. That's the reason why Unit has no communication methods.
type Writable ¶
type Writable struct {
// contains filtered or unexported fields
}
Writable implement WriteCLoser
type WritableRequestHandler ¶
type WritableRequestHandler func(im *WriterRequestContext)
WritableRequestHandler is function which handles incoming requests.
type WriterRequestContext ¶
type WriterRequestContext struct { *RequestContext // contains filtered or unexported fields }
WriterRequestContext is context for incoming request to establish binary stream writable on this end
func (*WriterRequestContext) Reply ¶
func (ctx *WriterRequestContext) Reply(data interface{}) (*Writable, error)
Reply stops middleware flow and responds to the message. If data argument is provided, it overwrites im.Data
func (*WriterRequestContext) Then ¶
func (ctx *WriterRequestContext) Then(cb func(ctx *WriterRequestContext))
Then binds middleware to message context. Middleware runs in LIFO order