forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demux.go
87 lines (73 loc) · 1.96 KB
/
demux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package comm
import (
"sync"
"sync/atomic"
"github.com/hyperledger/fabric/gossip/common"
)
// ChannelDeMultiplexer is a struct that can receive channel registrations (AddChannel)
// and publications (DeMultiplex) and it broadcasts the publications to registrations
// according to their predicate
type ChannelDeMultiplexer struct {
channels []*channel
lock *sync.RWMutex
closed int32
}
// NewChannelDemultiplexer creates a new ChannelDeMultiplexer
func NewChannelDemultiplexer() *ChannelDeMultiplexer {
return &ChannelDeMultiplexer{
channels: make([]*channel, 0),
lock: &sync.RWMutex{},
closed: int32(0),
}
}
type channel struct {
pred common.MessageAcceptor
ch chan interface{}
}
func (m *ChannelDeMultiplexer) isClosed() bool {
return atomic.LoadInt32(&m.closed) == int32(1)
}
// Close closes this channel, which makes all channels registered before
// to close as well.
func (m *ChannelDeMultiplexer) Close() {
defer func() {
// recover closing an already closed channel
recover()
}()
atomic.StoreInt32(&m.closed, int32(1))
m.lock.Lock()
defer m.lock.Unlock()
for _, ch := range m.channels {
close(ch.ch)
}
}
// AddChannel registers a channel with a certain predicate
func (m *ChannelDeMultiplexer) AddChannel(predicate common.MessageAcceptor) chan interface{} {
m.lock.Lock()
defer m.lock.Unlock()
ch := &channel{ch: make(chan interface{}, 10), pred: predicate}
m.channels = append(m.channels, ch)
return ch.ch
}
// DeMultiplex broadcasts the message to all channels that were returned
// by AddChannel calls and that hold the respected predicates.
func (m *ChannelDeMultiplexer) DeMultiplex(msg interface{}) {
defer func() {
recover()
}() // recover from sending on a closed channel
if m.isClosed() {
return
}
m.lock.RLock()
channels := m.channels
m.lock.RUnlock()
for _, ch := range channels {
if ch.pred(msg) {
ch.ch <- msg
}
}
}