2021-11-01 14:42:55 +00:00
|
|
|
package relay
|
2021-04-15 02:17:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2021-04-22 00:09:37 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol"
|
2021-04-15 02:17:12 +00:00
|
|
|
)
|
|
|
|
|
2021-10-09 18:18:53 +00:00
|
|
|
// Subscription handles the subscrition to a particular pubsub topic
|
2021-04-15 02:17:12 +00:00
|
|
|
type Subscription struct {
|
2022-02-23 15:08:27 +00:00
|
|
|
sync.RWMutex
|
|
|
|
|
2021-10-09 18:18:53 +00:00
|
|
|
// C is channel used for receiving envelopes
|
2021-04-22 18:49:52 +00:00
|
|
|
C chan *protocol.Envelope
|
|
|
|
|
2021-04-15 02:17:12 +00:00
|
|
|
closed bool
|
2021-11-01 14:42:55 +00:00
|
|
|
once sync.Once
|
2021-04-15 02:17:12 +00:00
|
|
|
quit chan struct{}
|
|
|
|
}
|
|
|
|
|
2021-10-09 18:18:53 +00:00
|
|
|
// Unsubscribe will close a subscription from a pubsub topic. Will close the message channel
|
2021-04-15 02:17:12 +00:00
|
|
|
func (subs *Subscription) Unsubscribe() {
|
2021-11-01 14:42:55 +00:00
|
|
|
subs.once.Do(func() {
|
|
|
|
close(subs.quit)
|
|
|
|
})
|
2022-02-23 15:08:27 +00:00
|
|
|
|
2021-04-15 02:17:12 +00:00
|
|
|
}
|
|
|
|
|
2021-10-09 18:18:53 +00:00
|
|
|
// IsClosed determine whether a Subscription is still open for receiving messages
|
2021-04-15 02:17:12 +00:00
|
|
|
func (subs *Subscription) IsClosed() bool {
|
2022-02-23 15:08:27 +00:00
|
|
|
subs.RLock()
|
|
|
|
defer subs.RUnlock()
|
2021-04-15 02:17:12 +00:00
|
|
|
return subs.closed
|
|
|
|
}
|