pubsub

package module
v0.0.0-...-cfb3871 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2018 License: MIT Imports: 5 Imported by: 0

README

pubsub

Publication/Subscription library written in Go

Build Status GoDoc

Documentation

Overview

Package pubsub provides the core functionalities to handle publication/subscription pipelines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action func(interface{}) error

type CancelFunc

type CancelFunc func() error

type Command

type Command struct {
	// Topic is the subscription topic.
	Topic string

	// Run will be called each time that the pubsub receives
	// new data on Topic.
	Run Action

	// PostRun is triggered before terminating the subscription
	// pipeline.
	PostRun func(error)

	// Ref is filled with the subscription index reference.
	Ref int
}

Command contains fields used in a subscription pipeline.

type PubSub

type PubSub struct {
	MaxSubs int // maximum number of subscribers

	sync.Mutex
	// contains filtered or unexported fields
}

PubSub wraps the core pubsub functionalities.

func New

func New() *PubSub

New returns a new PubSub instance.

func (*PubSub) Close

func (ps *PubSub) Close(topic string) error

Close removes a topic and closes its related channels.

func (*PubSub) Pub

func (ps *PubSub) Pub(message interface{}, topic string)

Pub broadcasts the message to the listeners of topic. unsubscribes a channel if it is closed when sending to it. (i.e. causes a panic)

func (*PubSub) Sub

func (ps *PubSub) Sub(cmd *Command) (CancelFunc, error)

Sub subscribes cmd to cmd.Topic in the pubsub. If there is no such topic it creates it. Returns a cancel function that can be used to terminate the subscription. cmd.Run gets called for each data coming from the pubsub. PostRun gets as argument the returned value of Run. Returns an error if the command does not contain at least a topic and a Run function.

func (*PubSub) Unsub

func (ps *PubSub) Unsub(index int, topic string) error

Unsub removes c from the list of subscribed channels of topic. Returns an error if no such topic is present, or if the channel is already no longer in the subscription list.

Jump to

Keyboard shortcuts

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