bullhorn

package
v0.0.1-tiedot Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2014 License: GPL-3.0, MIT Imports: 1 Imported by: 0

README

bullhorn

golang package to provide lightweight internal pub/sub for goroutines

Documentation

Overview

The bullhorn package provides a lightweight internal pub/sub mechanism for goroutines

Example
type A struct {
	AField int
}

type B struct {
	BField string
}

inbound := make(chan interface{}, 16)
Start(4, 16)

// start the consumer
go func() {

	for data := range inbound {

		fmt.Printf("data:%+v ", data)

		switch data := data.(type) {
		case A:
			fmt.Printf("A:%+v\n", data.AField)
		case B:
			fmt.Printf("B:%+v\n", data.BField)
		}

	}

}()

// start the producer
go func() {
	i := 0
	for {
		time.Sleep(1e8)

		Publish(Event{"A feed", A{i}})
		Publish(Event{"B feed", B{fmt.Sprintf("%v", i)}})
		i++
	}
}()

// subscribe to A feed
Add(Subscription{"A feed", inbound})

time.Sleep(2e9)

// subscribe to B feed
Add(Subscription{"B feed", inbound})

time.Sleep(2e9)

// delete A feed subscription
Delete(Subscription{"A feed", inbound})

time.Sleep(2e9)

// Start over
Start(4, 16)

time.Sleep(2e9)

// subscribe to B feed again
Add(Subscription{"B feed", inbound})

time.Sleep(2e9)

// enough
return
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(s Subscription) error

Add a new subscription

Example
// Register my inbound channel against an interesting feed
interestingFeed := make(chan interface{}, 1024)
Add(Subscription{"Interesting Feed", interestingFeed})
Output:

func Delete

func Delete(s Subscription) error

Delete an existing subscription

Example
interestingFeed := make(chan interface{}, 1)

// Remove an existingsubscription
Delete(Subscription{"Interesting Feed", interestingFeed})
Output:

func Publish

func Publish(e Event) error

Publish an event

Example
// Publish an event with asc data to a key
Publish(Event{"Interesting Feed", "Interesting Data"})
Output:

func Start

func Start(subscriptionBuffer, eventBuffer int64)

Start the publish and subscribe service

Example
// Start the service with buffered subscription and event channels
Start(1024, 2048)
Output:

func Subscribed

func Subscribed(key interface{}, receiver chan interface{}) bool

Make a blocking query returning true if a channel is subscribed to a key

Example
ch := make(chan interface{}, 1)

Add(Subscription{"order book prices", ch})
if Subscribed("order book prices", ch) {
	// all set, off we go
}
Output:

Types

type Event

type Event struct {
	Key  interface{}
	Data interface{}
}

The Event struct holds the key and data to be published

type Subscription

type Subscription struct {
	Key      interface{}
	Receiver chan interface{}
}

The Subscription struct holds the key to subscribe to and the channel the subscriber will be listening on

Jump to

Keyboard shortcuts

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