mirror of
https://github.com/logos-messaging/go-libp2p-pubsub.git
synced 2026-01-04 05:43:06 +00:00
Make close concurrent safe
This commit is contained in:
parent
e25f98c402
commit
55d412efa7
@ -2,6 +2,7 @@ package pubsub
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Subscription handles the details of a particular Topic subscription.
|
// Subscription handles the details of a particular Topic subscription.
|
||||||
@ -10,9 +11,9 @@ type Subscription struct {
|
|||||||
topic string
|
topic string
|
||||||
ch chan *Message
|
ch chan *Message
|
||||||
cancelCh chan<- *Subscription
|
cancelCh chan<- *Subscription
|
||||||
closed bool
|
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
err error
|
err error
|
||||||
|
once sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
// Topic returns the topic string associated with the Subscription
|
// Topic returns the topic string associated with the Subscription
|
||||||
@ -44,8 +45,7 @@ func (sub *Subscription) Cancel() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (sub *Subscription) close() {
|
func (sub *Subscription) close() {
|
||||||
if !sub.closed {
|
sub.once.Do(func() {
|
||||||
close(sub.ch)
|
close(sub.ch)
|
||||||
sub.closed = true
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user