broker

package
v0.0.0-...-525d4e8 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2019 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package broker contains the broker implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Broker

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

The Broker type represents a node in the cluster, it contains the list of all other members as well as a map of connected client channels.

func New

func New(ml Memberlist, cl *http.Client) *Broker

New creates a new instance of the Broker type using the given member list and node.

func (*Broker) Close

func (b *Broker) Close()

Close blocks the goroutine until all asynchronous operations of the broker have stopped.

func (*Broker) NewClient

func (b *Broker) NewClient(channelID, clientID string) (*Client, error)

NewClient creates a new client for a given channel. If the channel does not exist, it is created.

func (*Broker) Publish

func (b *Broker) Publish(channelID, clientID string, msg Message) error

Publish writes a given message to a client. If no client identifier is specified, the message is written to the entire channel. If running in a cluster, the event is forwarded asynchronously via HTTP to the next node whose id does not exist in the message's BeenTo field.

func (*Broker) RemoveClient

func (b *Broker) RemoveClient(channelID, clientID string)

RemoveClient removes a client from a channel. If the channel has no connected clients, it is also removed.

func (*Broker) Status

func (b *Broker) Status() *Status

Status returns information on the broker. It contains the number of running goroutines, the gossip members and total member count, as well as client information for this broker.

type Channel

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

The Channel type represents a channel within the broker. Each channel has a unique identifier and can have one or more clients. When events are published to a channel, a client is chosen at random

func NewChannel

func NewChannel(id string) *Channel

NewChannel creates a new instance of the Channel type using the given identifier

func (*Channel) ClientIDs

func (c *Channel) ClientIDs() []string

ClientIDs returns an array of all client identifiers in this channel.

func (*Channel) NewClient

func (c *Channel) NewClient(id string) (*Client, error)

NewClient adds a new client to the channel

func (*Channel) NumClients

func (c *Channel) NumClients() int

NumClients returns the total number of clients for a channel.

func (*Channel) RemoveClient

func (c *Channel) RemoveClient(id string)

RemoveClient removes a client from the channel

func (*Channel) Write

func (c *Channel) Write(msg Message)

Write writes a given message to all clients in the channel

func (*Channel) WriteTo

func (c *Channel) WriteTo(clientID string, msg Message)

WriteTo writes a message directly to a given client

type Client

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

The Client type represents a single client connected to the broker

func NewClient

func NewClient(id string) *Client

NewClient creates a new instance of the Client type with the given identifier.

func (*Client) ID

func (c *Client) ID() string

ID returns this client's identifier.

func (*Client) Messages

func (c *Client) Messages() <-chan Message

Messages returns a read-only channel for this client's messages.

func (*Client) Write

func (c *Client) Write(msg Message)

Write writes a given array of bytes to a client

type Memberlist

type Memberlist interface {
	NumMembers() int
	LocalNode() *memberlist.Node
	Members() []*memberlist.Node
}

The Memberlist type represents the gossip implementation used by the broker for service discovery.

type Message

type Message struct {
	// The event ID to set the EventSource object's last event ID value.
	ID string `json:"id"`

	// A string identifying the type of event described. If this is specified, an event will
	// be dispatched on the browser to the listener for the specified event name;
	// the website source code should use addEventListener() to listen for named events.
	// The onmessage handler is called if no event name is specified for a message.
	Event string `json:"event"`

	// The data field for the message. When the EventSource receives multiple consecutive lines that begin with data:,
	// it will concatenate them, inserting a newline character between each one.
	// Trailing newlines are removed.
	Data json.RawMessage `json:"data"`

	// The reconnection time to use when attempting to send the event. This must be an integer,
	// specifying the reconnection time in milliseconds.
	// If a non-integer value is specified, the field is ignored.
	Retry int `json:"retry"`

	// Contains identifiers of previous nodes this event has been through
	BeenTo []string `json:"been_to"`
}

The Message type represents a server-sent event.

func (*Message) Bytes

func (m *Message) Bytes() []byte

Bytes returns the Message instance in its textual form

func (*Message) JSON

func (m *Message) JSON() []byte

JSON returns the Message instance in JSON encoding

type Status

type Status struct {
	Goroutines int `json:"num_goroutines"`
	Gossip     struct {
		MemberCount int            `json:"member_count"`
		Members     map[string]int `json:"members"`
	} `json:"gossip"`
	Channels map[string][]string `json:"channels"`
}

The Status type represents the status of a node/cluster. It contains sections for the gossip memberlist and the node's channels

Jump to

Keyboard shortcuts

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