router

package module
v0.0.0-...-0700c64 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2018 License: MIT Imports: 3 Imported by: 0

README

Go Pub Sub Channels

Build Status

Easily send messages between multiple go routines with error response handling.

Setup

rt := router.NewRouter()
defer rt.Close()

Subscribing

subChan := rt.Subscribe("channel id")
for {
    select {
    case msg, ok := <-subChan:
        if ok == false {
            // Either was closed from Unsubscribe call or
            // rt.Close()
            break
        }
    }
}

Publishing

err := rt.Publish(context.Background(), "channel_id", "Hello World!")
if err == router.ErrNotDelivered {
    // The message didn't get delivered to any subscribers.
} else if err == router.ErrTimedOut {
    // The message may have been delivered to some subscribers,
    // but the context timed out.
} else err != nil {
    // Errors passed back by one or more subscribers.
}

Unsubscribing

doneChan := rt.Unsubscribe("channel_id", subChan)
// Either wait for the doneChan to close
<-doneChan
// Or wait for the subChan to close

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTimedOut     = errors.New("timed out")
	ErrNotDelivered = errors.New("not delivered")
)

Functions

This section is empty.

Types

type Message

type Message struct {
	Obj    interface{}
	Result chan<- error
}

type Router

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

func NewRouter

func NewRouter() *Router

func (*Router) Close

func (router *Router) Close()

func (*Router) NonBlockingPublish

func (router *Router) NonBlockingPublish(ctx context.Context, channelID string, obj interface{}) <-chan error

func (*Router) Publish

func (router *Router) Publish(ctx context.Context, channelID string, obj interface{}) error

func (*Router) Subscribe

func (router *Router) Subscribe(channelID string) chan Message

func (*Router) Unsubscribe

func (router *Router) Unsubscribe(channelID string, ch chan Message) chan struct{}

Jump to

Keyboard shortcuts

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