1 Star 0 Fork 0

peter/fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
demux.go 1.71 KB
一键复制 编辑 原始数据 按行查看 历史
Inbar Badian 提交于 2018-07-24 15:07 . [FAB-11278] Removing DATA RACES
/*
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
type ChannelDeMultiplexer struct {
channels []*channel
lock *sync.RWMutex
closed bool
}
// NewChannelDemultiplexer creates a new ChannelDeMultiplexer
func NewChannelDemultiplexer() *ChannelDeMultiplexer {
return &ChannelDeMultiplexer{
channels: make([]*channel, 0),
lock: &sync.RWMutex{},
}
}
type channel struct {
pred common.MessageAcceptor
ch chan interface{}
}
func (m *ChannelDeMultiplexer) isClosed() bool {
return m.closed
}
// Close closes this channel, which makes all channels registered before
// to close as well.
func (m *ChannelDeMultiplexer) Close() {
m.lock.Lock()
defer m.lock.Unlock()
m.closed = true
for _, ch := range m.channels {
close(ch.ch)
}
m.channels = nil
}
// 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{}) {
m.lock.RLock()
defer m.lock.RUnlock()
if m.isClosed() {
return
}
for _, ch := range m.channels {
if ch.pred(msg) {
ch.ch <- msg
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/peter_code_git/fabric.git
git@gitee.com:peter_code_git/fabric.git
peter_code_git
fabric
fabric
v1.4.5

搜索帮助