From 55d412efa7f5a734d2f926e0c7c948f0ab4def21 Mon Sep 17 00:00:00 2001 From: gammazero Date: Mon, 14 Jun 2021 14:03:52 -0700 Subject: [PATCH] Make close concurrent safe --- subscription.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/subscription.go b/subscription.go index cf13622..0745f8d 100644 --- a/subscription.go +++ b/subscription.go @@ -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 - } + }) }