Skip to content

Commit 3ec96e9

Browse files
committed
Allow multiple signal channels to be registered
1 parent 83071d2 commit 3ec96e9

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

conn.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type Conn struct {
5454
closed bool
5555
outLck sync.RWMutex
5656

57-
signals chan *Signal
57+
signals []chan *Signal
5858
signalsLck sync.Mutex
5959

6060
eavesdropped chan *Message
@@ -186,8 +186,8 @@ func (conn *Conn) Close() error {
186186
conn.closed = true
187187
conn.outLck.Unlock()
188188
conn.signalsLck.Lock()
189-
if conn.signals != nil {
190-
close(conn.signals)
189+
for _, ch := range conn.signals {
190+
close(ch)
191191
}
192192
conn.signalsLck.Unlock()
193193
conn.eavesdroppedLck.Lock()
@@ -304,11 +304,13 @@ func (conn *Conn) inWorker() {
304304
Name: iface + "." + member,
305305
Body: msg.Body,
306306
}
307-
// don't block trying to send a signal
308307
conn.signalsLck.Lock()
309-
select {
310-
case conn.signals <- signal:
311-
default:
308+
for _, ch := range conn.signals {
309+
// don't block trying to send a signal
310+
select {
311+
case ch <- signal:
312+
default:
313+
}
312314
}
313315
conn.signalsLck.Unlock()
314316
case TypeMethodCall:
@@ -476,18 +478,20 @@ func (conn *Conn) serials() {
476478
}
477479
}
478480

479-
// Signal sets the channel to which all received signal messages are forwarded.
480-
// The caller has to make sure that c is sufficiently buffered; if a message
481+
// Signal registers the given channel to be passed all received signal messages.
482+
// The caller has to make sure that ch is sufficiently buffered; if a message
481483
// arrives when a write to c is not possible, it is discarded.
482484
//
483-
// The channel can be reset by passing nil.
485+
// Multiple of these channels can be registered at the same time. Passing a
486+
// channel that already is registered will remove it from the list of the
487+
// registered channels.
484488
//
485-
// This channel is "overwritten" by Eavesdrop; i.e., if there currently is a
486-
// channel for eavesdropped messages, this channel receives all signals, and the
487-
// channel passed to Signal will not receive any signals.
488-
func (conn *Conn) Signal(c chan *Signal) {
489+
// Thess channels are "overwritten" by Eavesdrop; i.e., if there currently is a
490+
// channel for eavesdropped messages, this channel receives all signals, and
491+
// none of the channels passed to Signal will receive any signals.
492+
func (conn *Conn) Signal(ch chan *Signal) {
489493
conn.signalsLck.Lock()
490-
conn.signals = c
494+
conn.signals = append(conn.signals, ch)
491495
conn.signalsLck.Unlock()
492496
}
493497

0 commit comments

Comments
 (0)