Skip to content

Commit

Permalink
Make close concurrent safe
Browse files Browse the repository at this point in the history
gammazero authored and vyzo committed Jun 15, 2021
1 parent e25f98c commit 55d412e
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions subscription.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package pubsub

import (
"context"
"sync"
)

// Subscription handles the details of a particular Topic subscription.
@@ -10,9 +11,9 @@ type Subscription struct {
topic string
ch chan *Message
cancelCh chan<- *Subscription
closed bool
ctx context.Context
err error
once sync.Once
}

// Topic returns the topic string associated with the Subscription
@@ -44,8 +45,7 @@ func (sub *Subscription) Cancel() {
}

func (sub *Subscription) close() {
if !sub.closed {
sub.once.Do(func() {
close(sub.ch)
sub.closed = true
}
})
}

0 comments on commit 55d412e

Please sign in to comment.