Skip to content

Commit ab0e0d9

Browse files
authored
fix: pubsub channel not closing during close if no value has been published (#4269)
1 parent 866393d commit ab0e0d9

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

utils/pubsub/pubsub.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,17 @@ type listener struct {
120120
// the last value waiting to be published to the channel
121121
lastValue *DataEvent
122122

123-
// used for initialization
124-
startedOnce sync.Once
125123
// channel for signaling the loop to read a new value
126124
ping chan struct{}
127125
}
128126

129127
func newListener(channel chan DataEvent) *listener {
130-
return &listener{
128+
l := &listener{
131129
ping: make(chan struct{}, 1),
132130
channel: channel,
133131
}
132+
go l.startLoop()
133+
return l
134134
}
135135

136136
// publish sets the publisher's lastValue and starts the
@@ -139,12 +139,6 @@ func (r *listener) publish(data *DataEvent) {
139139
r.lastValueLock.Lock()
140140
r.lastValue = data
141141
r.lastValueLock.Unlock()
142-
143-
r.startedOnce.Do(func() {
144-
r.ping = make(chan struct{}, 1)
145-
go r.startLoop()
146-
})
147-
148142
select {
149143
case r.ping <- struct{}{}: // signals the startLoop that it has to read the value
150144
default:
@@ -166,7 +160,5 @@ func (r *listener) startLoop() {
166160
}
167161

168162
func (r *listener) close() {
169-
if r.ping != nil {
170-
close(r.ping)
171-
}
163+
close(r.ping)
172164
}

0 commit comments

Comments
 (0)