2014-06-22 00:22:38 +03:00
|
|
|
package pubsub
|
|
|
|
|
|
|
|
import (
|
2014-07-12 05:10:18 +03:00
|
|
|
"log"
|
2014-06-22 00:22:38 +03:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Channel struct {
|
|
|
|
record bool
|
|
|
|
history []interface{}
|
|
|
|
timeout time.Duration
|
|
|
|
closed chan bool
|
|
|
|
broadcast chan interface{}
|
|
|
|
subscribe chan *Subscription
|
|
|
|
unsubscribe chan *Subscription
|
|
|
|
subscriptions map[*Subscription]bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewChannel(opts *Opts) *Channel {
|
|
|
|
return &Channel{
|
|
|
|
timeout: opts.Timeout,
|
|
|
|
record: opts.Record,
|
|
|
|
history: make([]interface{}, 0),
|
|
|
|
closed: make(chan bool),
|
|
|
|
broadcast: make(chan interface{}),
|
|
|
|
subscribe: make(chan *Subscription),
|
|
|
|
unsubscribe: make(chan *Subscription),
|
|
|
|
subscriptions: make(map[*Subscription]bool),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Channel) Publish(data interface{}) {
|
2014-07-12 02:52:22 +03:00
|
|
|
go func() { c.broadcast <- data }()
|
2014-06-22 00:22:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Channel) Subscribe() *Subscription {
|
|
|
|
s := NewSubscription(c)
|
|
|
|
c.subscribe <- s
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Channel) Close() {
|
|
|
|
go func() { c.closed <- true }()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Channel) start() {
|
|
|
|
// make sure we don't bring down the application
|
|
|
|
// if somehow we encounter a nil pointer or some
|
|
|
|
// other unexpected behavior.
|
|
|
|
defer func() {
|
2014-07-12 05:10:18 +03:00
|
|
|
if r := recover(); r != nil {
|
|
|
|
log.Println("recoved from panic", r)
|
|
|
|
}
|
2014-06-22 00:22:38 +03:00
|
|
|
}()
|
|
|
|
|
|
|
|
// timeout the channel after N duration
|
|
|
|
// ignore the timeout if set to 0
|
|
|
|
var timeout <-chan time.Time
|
|
|
|
if c.timeout > 0 {
|
|
|
|
timeout = time.After(c.timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
|
|
|
|
case sub := <-c.unsubscribe:
|
|
|
|
delete(c.subscriptions, sub)
|
|
|
|
close(sub.send)
|
|
|
|
|
|
|
|
case sub := <-c.subscribe:
|
|
|
|
c.subscriptions[sub] = true
|
|
|
|
|
|
|
|
// if we are recording the output
|
|
|
|
// we should send it to the subscriber
|
|
|
|
// upon first connecting.
|
|
|
|
if c.record && len(c.history) > 0 {
|
|
|
|
history := make([]interface{}, len(c.history))
|
|
|
|
copy(history, c.history)
|
|
|
|
go replay(sub, history)
|
|
|
|
}
|
|
|
|
|
|
|
|
case msg := <-c.broadcast:
|
|
|
|
// if we are recording the output, append
|
|
|
|
// the message to the history
|
|
|
|
if c.record {
|
|
|
|
c.history = append(c.history, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop through each subscription and
|
|
|
|
// send the message.
|
|
|
|
for sub := range c.subscriptions {
|
|
|
|
select {
|
|
|
|
case sub.send <- msg:
|
|
|
|
// do nothing
|
2014-07-12 05:10:18 +03:00
|
|
|
//default:
|
|
|
|
// log.Println("subscription closed in inner select")
|
|
|
|
// sub.Close()
|
2014-06-22 00:22:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-timeout:
|
2014-07-12 05:10:18 +03:00
|
|
|
log.Println("subscription's timedout channel received message")
|
2014-06-22 00:22:38 +03:00
|
|
|
c.Close()
|
|
|
|
|
|
|
|
case <-c.closed:
|
2014-07-12 05:10:18 +03:00
|
|
|
log.Println("subscription's close channel received message")
|
2014-06-22 00:22:38 +03:00
|
|
|
c.stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func replay(s *Subscription, history []interface{}) {
|
2014-11-11 07:13:18 +02:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
log.Println("recoved from panic", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-06-22 00:22:38 +03:00
|
|
|
for _, msg := range history {
|
|
|
|
s.send <- msg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Channel) stop() {
|
|
|
|
for sub := range c.subscriptions {
|
|
|
|
sub.Close()
|
|
|
|
}
|
|
|
|
}
|