middleman

package module
v0.0.0-...-60493dc Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2019 License: MIT Imports: 9 Imported by: 0

README

middleman

A PubSub & Request/Response WebSocket Server, in Go.

MiddleMan distributes messages between connected services and clients. It allows stateless and stateful services from diverse platforms to be quickly integrated into a running cluster of services.

This is the middleman protocol.

  1. When a client connects to middleman, the the first message is the API key.

A servce API key enables all messages, a client API key only enables messages that a service has explicitly enabled.

Eg:

MyServerKey

The middleman will then respond with:

MM OK

if the key was accepted. If the key is not accepted, the connection is closed.

  1. Each message then follows this structure:
COMMAND TARGET
Header:Value

Body 
.

Note that the message body is terminated with a single dot and newline, just like SMTP.

  1. Commands are:

PUB: Publish a message with a name (target). Message is received by all subscribers.

SUB: Request that all published messages of (target) are delivered.

REQ: Request something from a service. Must have ReqID header. This is delivered to only one service, even if multiple services have subscribed to the target.

RES: Respond to a request. Must have a ReqID header.

EPUB: Allow clients to publish to this target.

EREQ: Allow clients to request from this target.

ESUB: Allow clients to subscribe to this target.

A PUB command distributes to all services and clients. A REQ command is effectively load balanced between any waiting services.

 
                             +------------------+
                             |                  |          +--------------+
  +-------------+      +-----+-------+  +-------------+----+  Service #1  |
  |  Client #1  +------+  Client Key |  | Server Key  |    +--------------+
  +-------------+      |             |  |             |
                       |             |  |             |
                       ++-+----------+  +----------+-++
                        | |  |                  |  | |     +--------------+
                        | |  |                  |  | +-----+  Service #2  |
  +-------------+       | |  |     MiddleMan    |  |       +--------------+
  |  Client #2  +-------+ |  |                  |  |
  +-------------+         |  |                  |  |
                          |  +------------------+  |
                          |                        |       +--------------+
                          |                        +-------+  Service #N  |
                          |                                +--------------+
  +-------------+         |
  |  Client #N  +---------+
  +-------------+
 

     PUB #Name                                           EPUB #Name
     #Body
                                                         ESUB #Name
     SUB #Name
                                                         EREQ #Name
     REQ #Name
     #Body

     RES #Name
     #Body


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(message *Message) []byte

Marshal turns a *Message into a []byte

Types

type Client

type Client struct {
	Conn      *websocket.Conn
	Outbox    chan []byte
	IsTrusted bool
	Quit      chan bool
	GUID      string
	UID       string
	Header    textproto.MIMEHeader
}

Client is used by the MM server to hold information about client connections from trusted service providers and untrusted clients. +gen * set

type ClientAtomicMap

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

ClientAtomicMap is a copy-on-write thread-safe map of pointers to Client

func NewClientAtomicMap

func NewClientAtomicMap() *ClientAtomicMap

NewClientAtomicMap returns a new initialized ClientAtomicMap

func (*ClientAtomicMap) Delete

func (am *ClientAtomicMap) Delete(key string)

Delete removes the pointer to Client under key from the map

func (*ClientAtomicMap) Get

func (am *ClientAtomicMap) Get(key string) (value *Client, ok bool)

Get returns a pointer to Client for a given key

func (*ClientAtomicMap) GetAll

func (am *ClientAtomicMap) GetAll() map[string]*Client

GetAll returns the underlying map of pointers to Client this map must NOT be modified, to change the map safely use the Set and Delete functions and Get the value again

func (*ClientAtomicMap) Len

func (am *ClientAtomicMap) Len() int

Len returns the number of elements in the map

func (*ClientAtomicMap) Set

func (am *ClientAtomicMap) Set(key string, value *Client)

Set inserts in the map a pointer to Client under a given key

type ClientSet

type ClientSet map[*Client]struct{}

ClientSet is the primary type that represents a set

func NewClientSet

func NewClientSet(a ...*Client) ClientSet

NewClientSet creates and returns a reference to an empty set.

func (ClientSet) Add

func (set ClientSet) Add(i *Client) bool

Add adds an item to the current set if it doesn't already exist in the set.

func (ClientSet) Cardinality

func (set ClientSet) Cardinality() int

Cardinality returns how many items are currently in the set.

func (*ClientSet) Clear

func (set *ClientSet) Clear()

Clear clears the entire set to be the empty set.

func (ClientSet) Clone

func (set ClientSet) Clone() ClientSet

Clone returns a clone of the set. Does NOT clone the underlying elements.

func (ClientSet) Contains

func (set ClientSet) Contains(i *Client) bool

Contains determines if a given item is already in the set.

func (ClientSet) ContainsAll

func (set ClientSet) ContainsAll(i ...*Client) bool

ContainsAll determines if the given items are all in the set

func (ClientSet) Difference

func (set ClientSet) Difference(other ClientSet) ClientSet

Difference returns a new set with items in the current set but not in the other set

func (ClientSet) Equal

func (set ClientSet) Equal(other ClientSet) bool

Equal determines if two sets are equal to each other. If they both are the same size and have the same items they are considered equal. Order of items is not relevent for sets to be equal.

func (ClientSet) Intersect

func (set ClientSet) Intersect(other ClientSet) ClientSet

Intersect returns a new set with items that exist only in both sets.

func (ClientSet) IsSubset

func (set ClientSet) IsSubset(other ClientSet) bool

IsSubset determines if every item in the other set is in this set.

func (ClientSet) IsSuperset

func (set ClientSet) IsSuperset(other ClientSet) bool

IsSuperset determines if every item of this set is in the other set.

func (ClientSet) Iter

func (set ClientSet) Iter() <-chan *Client

Iter returns a channel of type *Client that you can range over.

func (ClientSet) Remove

func (set ClientSet) Remove(i *Client)

Remove allows the removal of a single item in the set.

func (ClientSet) SymmetricDifference

func (set ClientSet) SymmetricDifference(other ClientSet) ClientSet

SymmetricDifference returns a new set with items in the current set or the other set but not in both.

func (ClientSet) ToSlice

func (set ClientSet) ToSlice() []*Client

ToSlice returns the elements of the current set as a slice

func (ClientSet) Union

func (set ClientSet) Union(other ClientSet) ClientSet

Union returns a new set with all items in both sets.

type ClientSetAtomicMap

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

ClientSetAtomicMap is a copy-on-write thread-safe map of ClientSet

func NewClientSetAtomicMap

func NewClientSetAtomicMap() *ClientSetAtomicMap

NewClientSetAtomicMap returns a new initialized ClientSetAtomicMap

func (*ClientSetAtomicMap) Delete

func (am *ClientSetAtomicMap) Delete(key string)

Delete removes the ClientSet under key from the map

func (*ClientSetAtomicMap) Get

func (am *ClientSetAtomicMap) Get(key string) (value ClientSet, ok bool)

Get returns a ClientSet for a given key

func (*ClientSetAtomicMap) GetAll

func (am *ClientSetAtomicMap) GetAll() map[string]ClientSet

GetAll returns the underlying map of ClientSet this map must NOT be modified, to change the map safely use the Set and Delete functions and Get the value again

func (*ClientSetAtomicMap) Len

func (am *ClientSetAtomicMap) Len() int

Len returns the number of elements in the map

func (*ClientSetAtomicMap) Set

func (am *ClientSetAtomicMap) Set(key string, value ClientSet)

Set inserts in the map a ClientSet under a given key

type HandlerFunc

type HandlerFunc func(*Message)

+gen atomicmap

type HandlerFuncAtomicMap

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

HandlerFuncAtomicMap is a copy-on-write thread-safe map of HandlerFunc

func NewHandlerFuncAtomicMap

func NewHandlerFuncAtomicMap() *HandlerFuncAtomicMap

NewHandlerFuncAtomicMap returns a new initialized HandlerFuncAtomicMap

func (*HandlerFuncAtomicMap) Delete

func (am *HandlerFuncAtomicMap) Delete(key string)

Delete removes the HandlerFunc under key from the map

func (*HandlerFuncAtomicMap) Get

func (am *HandlerFuncAtomicMap) Get(key string) (value HandlerFunc, ok bool)

Get returns a HandlerFunc for a given key

func (*HandlerFuncAtomicMap) GetAll

func (am *HandlerFuncAtomicMap) GetAll() map[string]HandlerFunc

GetAll returns the underlying map of HandlerFunc this map must NOT be modified, to change the map safely use the Set and Delete functions and Get the value again

func (*HandlerFuncAtomicMap) Len

func (am *HandlerFuncAtomicMap) Len() int

Len returns the number of elements in the map

func (*HandlerFuncAtomicMap) Set

func (am *HandlerFuncAtomicMap) Set(key string, value HandlerFunc)

Set inserts in the map a HandlerFunc under a given key

type Message

type Message struct {
	Cmd    string
	Key    string
	Header textproto.MIMEHeader
	Body   []byte
	Client *Client
}

Message represents the protocol used for MM communication over the websocket.

func Unmarshal

func Unmarshal(payload []byte) (*Message, error)

Unmarshal turns a []byte into a *Message.

type Service

type Service struct {
	Closed chan bool
	// contains filtered or unexported fields
}

Service is the connection from a service provider to the MM server.

func NewService

func NewService(u string, key string) (Service, error)

NewService creates a connection to an MM server using a websocket URL, eg ws://localhost:8765/

func (Service) Connect

func (mmc Service) Connect() error

func (Service) RegisterPubHandler

func (mmc Service) RegisterPubHandler(key string, fn HandlerFunc)

Register a HandlerFunc to be called when PUB is received.

func (Service) RegisterReqHandler

func (mmc Service) RegisterReqHandler(key string, fn HandlerFunc)

Register a HandlerFunc to be called when REQ is received.

func (Service) Send

func (mmc Service) Send(msg *Message)

Send a message from the service to the MM server.

func (Service) Stop

func (mmc Service) Stop()

Stop the service and disconnect,

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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