-
Notifications
You must be signed in to change notification settings - Fork 56
/
msgio.go
213 lines (184 loc) · 3.35 KB
/
msgio.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright 2018 The go-zeromq Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zmq4
import (
"context"
"io"
"sync"
errgrp "github.com/go-zeromq/zmq4/internal/errgroup"
"golang.org/x/sync/errgroup"
)
// rpool is the interface that reads ZMQ messages from a pool of connections.
type rpool interface {
io.Closer
addConn(r *Conn)
rmConn(r *Conn)
read(ctx context.Context, msg *Msg) error
}
// wpool is the interface that writes ZMQ messages to a pool of connections.
type wpool interface {
io.Closer
addConn(w *Conn)
rmConn(r *Conn)
write(ctx context.Context, msg Msg) error
}
// qreader is a queued-message reader.
type qreader struct {
ctx context.Context
mu sync.RWMutex
rs []*Conn
c chan Msg
sem *semaphore // ready when a connection is live.
}
func newQReader(ctx context.Context) *qreader {
const qrsize = 10
return &qreader{
ctx: ctx,
c: make(chan Msg, qrsize),
sem: newSemaphore(),
}
}
func (q *qreader) Close() error {
q.mu.RLock()
var err error
var grp errgroup.Group
for i := range q.rs {
grp.Go(q.rs[i].Close)
}
err = grp.Wait()
q.rs = nil
q.mu.RUnlock()
return err
}
func (q *qreader) addConn(r *Conn) {
q.mu.Lock()
q.sem.enable()
q.rs = append(q.rs, r)
q.mu.Unlock()
go q.listen(q.ctx, r)
}
func (q *qreader) rmConn(r *Conn) {
q.mu.Lock()
defer q.mu.Unlock()
cur := -1
for i := range q.rs {
if q.rs[i] == r {
cur = i
break
}
}
if cur >= 0 {
q.rs = append(q.rs[:cur], q.rs[cur+1:]...)
}
}
func (q *qreader) read(ctx context.Context, msg *Msg) error {
q.sem.lock(ctx)
select {
case <-ctx.Done():
return ctx.Err()
case *msg = <-q.c:
}
return msg.err
}
func (q *qreader) listen(ctx context.Context, r *Conn) {
defer q.rmConn(r)
defer r.Close()
for {
msg := r.read()
select {
case <-ctx.Done():
return
default:
q.c <- msg
if msg.err != nil {
return
}
}
}
}
type mwriter struct {
ctx context.Context
mu sync.Mutex
ws []*Conn
sem *semaphore
}
func newMWriter(ctx context.Context) *mwriter {
return &mwriter{
ctx: ctx,
sem: newSemaphore(),
}
}
func (w *mwriter) Close() error {
w.mu.Lock()
var err error
for _, ww := range w.ws {
e := ww.Close()
if e != nil && err == nil {
err = e
}
}
w.ws = nil
w.mu.Unlock()
return err
}
func (mw *mwriter) addConn(w *Conn) {
mw.mu.Lock()
mw.sem.enable()
mw.ws = append(mw.ws, w)
mw.mu.Unlock()
}
func (mw *mwriter) rmConn(w *Conn) {
mw.mu.Lock()
defer mw.mu.Unlock()
cur := -1
for i := range mw.ws {
if mw.ws[i] == w {
cur = i
break
}
}
if cur >= 0 {
mw.ws = append(mw.ws[:cur], mw.ws[cur+1:]...)
}
}
func (w *mwriter) write(ctx context.Context, msg Msg) error {
w.sem.lock(ctx)
grp, _ := errgrp.WithContext(ctx)
w.mu.Lock()
for i := range w.ws {
ww := w.ws[i]
grp.Go(func() error {
return ww.SendMsg(msg)
})
}
err := grp.Wait()
w.mu.Unlock()
return err
}
type semaphore struct {
ready chan struct{}
}
func newSemaphore() *semaphore {
return &semaphore{ready: make(chan struct{})}
}
func (sem *semaphore) enable() {
select {
case _, ok := <-sem.ready:
if ok {
close(sem.ready)
}
default:
close(sem.ready)
}
}
func (sem *semaphore) lock(ctx context.Context) {
select {
case <-ctx.Done():
case <-sem.ready:
}
}
var (
_ rpool = (*qreader)(nil)
_ wpool = (*mwriter)(nil)
)