-
Notifications
You must be signed in to change notification settings - Fork 0
/
pub.go
185 lines (155 loc) · 3.43 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 2015 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"
"github.com/gdamore/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.w.Init()
p.w.Add()
go p.sender()
}
func (p *pub) Shutdown(linger time.Duration) {
when := time.Now().Add(linger)
tq := time.After(linger)
p.w.WaitAbsTimeout(when)
p.Lock()
for _, pe := range p.eps {
select {
case pe.q <- nil:
case <-tq:
tq = time.After(0)
}
pe.w.WaitAbsTimeout(when)
}
p.Unlock()
}
// Bottom sender.
func (pe *pubEp) sender() {
for {
m := <-pe.q
if m == nil {
break
}
if pe.ep.SendMsg(m) != nil {
m.Free()
break
}
}
pe.w.Done()
}
// Top sender.
func (p *pub) sender() {
sq := p.sock.SendChannel()
for {
m := <-sq
if m == nil {
break
}
p.Lock()
for _, pe := range p.eps {
m := m.Dup()
select {
case pe.q <- m:
default:
m.Free()
}
}
p.Unlock()
}
p.w.Done()
}
func (pe *pubEp) receiver() {
// In order for us to detect a dropped connection, we need to poll
// on the socket. We don't care about the results and discard them,
// but this allows the disconnect to be noticed. Note that we will
// be blocked in this call forever, until the connection is dropped.
pe.ep.RecvMsg()
}
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.sender()
go pe.receiver()
}
func (p *pub) RemoveEndpoint(ep mangos.Endpoint) {
p.Lock()
delete(p.eps, ep.GetID())
p.Unlock()
}
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 {
switch name {
case mangos.OptionRaw:
p.raw = v.(bool)
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
}
}
// NewProtocol returns a new PUB protocol object.
func NewProtocol() mangos.Protocol {
return &pub{}
}
// NewSocket allocates a new Socket using the PUB protocol.
func NewSocket() (mangos.Socket, error) {
return mangos.MakeSocket(&pub{}), nil
}