pubsub

package
v2.5.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2016 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package pubsub implements helpers to connect to and read events from Redis in a reliable way.

Subscriptions are handled on a Redis connection; multiple listeners can be attached to single events, and subscription and unsubscription on the connection are handled automatically. If the connection goes down, a new connection will be established and events will be automatically re-subscribed to. All methods are thread-safe.

Sometimes you wish to subscribe to a simple, single event in Redis, but more often than not you want to subscribe to an event on a resource. Pubsub gives gives you an easy way to handle these kinds of subscriptions, without the need for `fmt` on your part or any kind or reflection-based formatting on ours. You create Event structures with typed fields and can later inspect those fields when you receive an event from Redis, in a reasonably fluid and very type-safe manner.

Simple Event

pub := pubsub.New(cnx)
pub.Subscribe(pubsub.NewEvent("foo"), function (e pubsub.Event, b []byte) {
	// You're passed the subscribed event and any byte payload
	fmt.Printf("foo happened with payload %#v\n", b)
})

Patterns and Inspections

pub := pubsub.New(cnx)
event := pubsub.NewPattern().
	String("foo:").
	Start().As("id").
	String(":bar")

pub.Subscribe(event, function (e pubsub.Event, b []byte) {
	// You can alias fields and look them up by their names. Pattern
	// events will have their "stars" filled in.
	id, _ := e.Find("id").Int()
	fmt.Printf("just got foo:%d:bar!\n", id)
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Emitter

type Emitter interface {
	// Subscribe registers that the provided lister wants to be notified
	// of the given Event. If the Listener is already subscribed to the
	// event, it will be added again and the listener will be invoked
	// multiple times when that event occurs.
	Subscribe(e EventBuilder, l Listener)

	// Unsubscribe unregisters a listener from an event. If the listener
	// is not subscribed to the event, this will be a noop. Note that this
	// only unsubscribes *one* listener from the event; if it's subscribed
	// multiple times, it will need to unsubscribe multiple times.
	Unsubscribe(e EventBuilder, l Listener)

	// Errs returns a channel of errors that occur asynchronously on
	// the Redis connection.
	Errs() <-chan error

	// Close frees resources associated with the Emitter.
	Close()
}

Emitter is the primary interface to interact with pubsub.

type Event

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

An Event is passed down to a Listener's Handle function.

func (Event) Channel

func (e Event) Channel() string

Channel returns a the channel that the event was sent down. For plain events, this is equivalent to event names. For pattern events, this is the fulfilled name of the event.

func (Event) Find

func (e Event) Find(alias string) Field

Find looks up a field value by its alias. This is most useful in pattern subscriptions where might use Find to look up a parameterized property. If the alias does not exist, an empty struct will be returned.

func (Event) Get

func (e Event) Get(i int) Field

Get returns the value of a field at index `i` within the event. If the field does not exist, an empty struct will be returned.

func (Event) Len

func (e Event) Len() int

Len returns the number of fields contained in the event.

func (Event) Pattern

func (e Event) Pattern() string

Pattern returns the pattern which was originally used to subscribe to this event. For plain events, this will simply be the event name.

func (Event) Type

func (e Event) Type() EventType

Type returns the type of the event (either a PlainEvent or a PatternEvent).

type EventBuilder

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

An Event is passed to an Emitter to manage which events a Listener is subscribed to.

func NewEvent

func NewEvent(fields ...interface{}) EventBuilder

NewEvent creates and returns a new event based off the series of fields. This translates to a Redis SUBSCRIBE call.

func NewPattern

func NewPattern(fields ...interface{}) EventBuilder

NewPattern creates and returns a new event pattern off the series of fields. This translates to a Redis PSUBSCRIBE call.

func (EventBuilder) Alternatives

func (e EventBuilder) Alternatives(alts string) EventBuilder

Alternatives creates a field with the alts wrapped in brackets, to match one of them in a Redis pattern, and chains it on to the event.

func (EventBuilder) As

func (e EventBuilder) As(alias string) EventBuilder

As sets the alias of the last field in the event list. You may then call Event.Find(alias) to look up the value of the field in the event.

func (EventBuilder) Int

func (e EventBuilder) Int(x int) EventBuilder

Int creates a Field containing an integer.

func (EventBuilder) Name

func (e EventBuilder) Name() string

Name returns name of the event, formed by a concatenation of all the event fields.

func (EventBuilder) Placeholder

func (e EventBuilder) Placeholder() EventBuilder

Placeholder creates a field containing a `?` for a placeholder in Redis patterns, and chains it on to the event.

func (EventBuilder) Star

func (e EventBuilder) Star() EventBuilder

Star creates a Field containing the Kleene star `*` for pattern subscription, and chains it on to the EventBuilder.

func (EventBuilder) String

func (e EventBuilder) String(str string) EventBuilder

String creates a Field containing a string.

func (EventBuilder) ToEvent

func (e EventBuilder) ToEvent(channel, pattern string) Event

ToEvent converts an EventBuilder into an immutable event which appears to have been send down the provided channel and pattern. This is primarily used internally but may also be useful for unit testing.

type EventType

type EventType int

EventType is used to distinguish between pattern and plain text events.

const (
	// PlainEvent is the event type for events that are simple subscribed
	// to in Redis without pattern matching.
	PlainEvent EventType = iota
	// PatternEvent is the event type for events in Redis which will be
	// subscribed to using patterns.
	PatternEvent
)

func (EventType) SubCommand

func (e EventType) SubCommand() string

SubCommand returns the command issued to subscribe to the event in Redis.

func (EventType) UnsubCommand

func (e EventType) UnsubCommand() string

UnsubCommand returns the command issued o unsubscribe from the event in Redis.

type Field

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

Field is a type which is are concatenated into events, listed to over Redis.

func (Field) Bytes

func (f Field) Bytes() []byte

Bytes returns the field value as a byte slice.

func (Field) Int

func (f Field) Int() (int, error)

Int attempts to parse and return the field value as an integer.

func (Field) Int64

func (f Field) Int64() (int64, error)

Int64 attempts to parse and return the field value as a int64.

func (Field) IsZero

func (f Field) IsZero() bool

IsZero returns true if the field is empty. A call to Event.Find() or Event.Get() with a non-existent alias or index will return such a struct.

func (Field) String

func (f Field) String() string

String returns the field value as a string.

func (Field) Uint64

func (f Field) Uint64() (uint64, error)

Uint64 attempts to parse and return the field value as a uint64.

type Listener

type Listener interface {
	// Handle is invoked when the event occurs, with
	// the byte payload it contains.
	Handle(e Event, b []byte)
}

The Listener contains a function which, when passed to an Emitter, is invoked when an event occurs. It's invoked with the corresponding subscribed Event and the event's payload.

Note that if a listener is subscribed to multiple overlapping events such as `foo:bar` and `foo:*`, the listener will be called multiple times.

type Pubsub

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

Pubsub is an implementation of the Emitter interface using Redis pupsub.

func NewPubsub

func NewPubsub(pool *redis.Pool) *Pubsub

NewPubsub creates a new Emitter based on pubsub on the provided Redis pool.

func (*Pubsub) Close

func (p *Pubsub) Close()

Close implements Emitter.Close

func (*Pubsub) Errs

func (p *Pubsub) Errs() <-chan error

Errs implements Emitter.Errs

func (*Pubsub) Subscribe

func (p *Pubsub) Subscribe(ev EventBuilder, l Listener)

Subscribe implements Emitter.Subscribe

func (*Pubsub) Unsubscribe

func (p *Pubsub) Unsubscribe(ev EventBuilder, l Listener)

Unsubscribe implements Emitter.Unsubscribe

Jump to

Keyboard shortcuts

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