This repository has been archived by the owner on Dec 21, 2019. It is now read-only.
/
pub.go
185 lines (156 loc) · 3.27 KB
/
pub.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2018 The Mangos Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package pub implements the PUB protocol. This protocol publishes messages
// to subscribers (SUB peers). The subscribers will filter incoming messages
// from the publisher based on their subscription.
package pub
import (
"sync"
"time"
"nanomsg.org/go-mangos"
)
type pubEp struct {
ep mangos.Endpoint
q chan *mangos.Message
p *pub
w mangos.Waiter
}
type pub struct {
sock mangos.ProtocolSocket
eps map[uint32]*pubEp
raw bool
w mangos.Waiter
sync.Mutex
}
func (p *pub) Init(sock mangos.ProtocolSocket) {
p.sock = sock
p.eps = make(map[uint32]*pubEp)
p.sock.SetRecvError(mangos.ErrProtoOp)
p.w.Init()
p.w.Add()
go p.sender()
}
func (p *pub) Shutdown(expire time.Time) {
p.w.WaitAbsTimeout(expire)
p.Lock()
peers := p.eps
p.eps = make(map[uint32]*pubEp)
p.Unlock()
for id, peer := range peers {
mangos.DrainChannel(peer.q, expire)
close(peer.q)
delete(peers, id)
}
}
// Bottom sender.
func (pe *pubEp) peerSender() {
for {
m := <-pe.q
if m == nil {
break
}
if pe.ep.SendMsg(m) != nil {
m.Free()
break
}
}
}
// Top sender.
func (p *pub) sender() {
defer p.w.Done()
cq := p.sock.CloseChannel()
sq := p.sock.SendChannel()
for {
select {
case <-cq:
return
case m := <-sq:
if m == nil {
sq = p.sock.SendChannel()
continue
}
p.Lock()
for _, peer := range p.eps {
m := m.Dup()
select {
case peer.q <- m:
default:
m.Free()
}
}
p.Unlock()
m.Free()
}
}
}
func (p *pub) AddEndpoint(ep mangos.Endpoint) {
depth := 16
if i, err := p.sock.GetOption(mangos.OptionWriteQLen); err == nil {
depth = i.(int)
}
pe := &pubEp{ep: ep, p: p, q: make(chan *mangos.Message, depth)}
pe.w.Init()
p.Lock()
p.eps[ep.GetID()] = pe
p.Unlock()
pe.w.Add()
go pe.peerSender()
go mangos.NullRecv(ep)
}
func (p *pub) RemoveEndpoint(ep mangos.Endpoint) {
id := ep.GetID()
p.Lock()
pe := p.eps[id]
delete(p.eps, id)
p.Unlock()
if pe != nil {
close(pe.q)
}
}
func (*pub) Number() uint16 {
return mangos.ProtoPub
}
func (*pub) PeerNumber() uint16 {
return mangos.ProtoSub
}
func (*pub) Name() string {
return "pub"
}
func (*pub) PeerName() string {
return "sub"
}
func (p *pub) SetOption(name string, v interface{}) error {
var ok bool
switch name {
case mangos.OptionRaw:
if p.raw, ok = v.(bool); !ok {
return mangos.ErrBadValue
}
return nil
default:
return mangos.ErrBadOption
}
}
func (p *pub) GetOption(name string) (interface{}, error) {
switch name {
case mangos.OptionRaw:
return p.raw, nil
default:
return nil, mangos.ErrBadOption
}
}
// NewSocket allocates a new Socket using the PUB protocol.
func NewSocket() (mangos.Socket, error) {
return mangos.MakeSocket(&pub{}), nil
}