2018-08-28 03:01:08 +00:00
|
|
|
package pubsub
|
2016-10-19 23:01:06 +00:00
|
|
|
|
2016-11-17 10:27:57 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
2016-10-19 23:01:06 +00:00
|
|
|
type Subscription struct {
|
|
|
|
topic string
|
|
|
|
ch chan *Message
|
|
|
|
cancelCh chan<- *Subscription
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sub *Subscription) Topic() string {
|
|
|
|
return sub.topic
|
|
|
|
}
|
|
|
|
|
2016-11-17 10:27:57 +00:00
|
|
|
func (sub *Subscription) Next(ctx context.Context) (*Message, error) {
|
|
|
|
select {
|
|
|
|
case msg, ok := <-sub.ch:
|
|
|
|
if !ok {
|
|
|
|
return msg, sub.err
|
|
|
|
}
|
2016-10-19 23:01:06 +00:00
|
|
|
|
2016-11-17 10:27:57 +00:00
|
|
|
return msg, nil
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
2016-10-19 23:01:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sub *Subscription) Cancel() {
|
|
|
|
sub.cancelCh <- sub
|
|
|
|
}
|