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
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})
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})
func Publish ¶
Publish an event
Example ¶
// Publish an event with asc data to a key
Publish(Event{"Interesting Feed", "Interesting Data"})
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)
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
}
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
Click to show internal directories.
Click to hide internal directories.