forked from tw-bc-group/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demux.go
106 lines (95 loc) · 2.83 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package comm
import (
"sync"
"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. Can only be closed once and never open after a close.
type ChannelDeMultiplexer struct {
// lock protects everything below it.
lock sync.Mutex
closed bool // one way boolean from false -> true
stopCh chan struct{}
// deMuxInProgress keeps track of any calls to DeMultiplex
// that are still being handled. This is used to determine
// when it is safe to close all of the tracked channels
deMuxInProgress sync.WaitGroup
channels []*channel
}
// NewChannelDemultiplexer creates a new ChannelDeMultiplexer
func NewChannelDemultiplexer() *ChannelDeMultiplexer {
return &ChannelDeMultiplexer{stopCh: make(chan struct{})}
}
type channel struct {
pred common.MessageAcceptor
ch chan<- interface{}
}
// Close closes this channel, which makes all channels registered before
// to close as well.
func (m *ChannelDeMultiplexer) Close() {
m.lock.Lock()
if m.closed {
m.lock.Unlock()
return
}
m.closed = true
close(m.stopCh)
m.deMuxInProgress.Wait()
for _, ch := range m.channels {
close(ch.ch)
}
m.channels = nil
m.lock.Unlock()
}
// AddChannel registers a channel with a certain predicate. AddChannel
// returns a read-only channel that will produce values that are
// matched by the predicate function.
//
// If the DeMultiplexer is closed, the channel returned will be closed
// to prevent users of the channel from waiting on the channel.
func (m *ChannelDeMultiplexer) AddChannel(predicate common.MessageAcceptor) <-chan interface{} {
m.lock.Lock()
if m.closed { // closed once, can't put anything more in.
m.lock.Unlock()
ch := make(chan interface{})
close(ch)
return ch
}
bidirectionalCh := make(chan interface{}, 10)
// Assignment to channel converts bidirectionalCh to send-only.
// Return converts bidirectionalCh to a receive-only.
ch := &channel{ch: bidirectionalCh, pred: predicate}
m.channels = append(m.channels, ch)
m.lock.Unlock()
return bidirectionalCh
}
// DeMultiplex broadcasts the message to all channels that were returned
// by AddChannel calls and that hold the respected predicates.
//
// Blocks if any one channel that would receive msg has a full buffer.
func (m *ChannelDeMultiplexer) DeMultiplex(msg interface{}) {
m.lock.Lock()
if m.closed {
m.lock.Unlock()
return
}
channels := m.channels
m.deMuxInProgress.Add(1)
m.lock.Unlock()
for _, ch := range channels {
if ch.pred(msg) {
select {
case <-m.stopCh:
m.deMuxInProgress.Done()
return // stopping
case ch.ch <- msg:
}
}
}
m.deMuxInProgress.Done()
}