1
0
Fork 0
burlesque/hub/subscription.go

57 lines
831 B
Go
Raw Normal View History

2014-09-10 12:52:03 +00:00
package hub
type (
Subscription struct {
2014-09-11 10:27:48 +00:00
Queues []string
result chan<- Result
2014-09-10 12:52:03 +00:00
done chan struct{}
}
2014-09-11 10:27:48 +00:00
Result struct {
Queue string
Message []byte
}
2014-09-10 12:52:03 +00:00
)
2014-09-11 10:27:48 +00:00
func NewSubscription(queues []string, result chan<- Result) *Subscription {
2014-09-10 12:52:03 +00:00
return &Subscription{
2014-09-11 10:27:48 +00:00
Queues: queues,
2014-09-10 12:52:03 +00:00
result: result,
done: make(chan struct{}),
}
}
2014-09-10 13:34:39 +00:00
func (s *Subscription) Need(queue string) bool {
2014-09-11 10:27:48 +00:00
for _, q := range s.Queues {
2014-09-10 13:34:39 +00:00
if q == queue {
return true
}
}
return false
}
2014-09-11 10:27:48 +00:00
func (s *Subscription) Send(res Result) bool {
2014-09-10 12:52:03 +00:00
success := make(chan bool)
go func() {
defer func() {
if err := recover(); err != nil {
success <- false
}
}()
2014-09-11 10:27:48 +00:00
s.result <- res
2014-09-10 12:52:03 +00:00
success <- true
}()
return <-success
}
func (s *Subscription) Done() <-chan struct{} {
return s.done
}
func (s *Subscription) Close() {
close(s.done)
}