-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.go
216 lines (194 loc) · 4.83 KB
/
server.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
214
215
216
package chanserv
import (
"errors"
"fmt"
"net"
"time"
)
type server struct {
mpx Multiplexer
maxErrMass int
onMaxErrMass func(mass int, err error)
onMpxError func(err error)
onError func(err error)
onChanError func(err error)
timeouts serverTimeouts
useCompression bool
}
type serverTimeouts struct {
servingTimeout time.Duration
sourcingTimeout time.Duration
chanAcceptTimeout time.Duration
masterReadTimeout time.Duration
masterWriteTimeout time.Duration
frameWriteTimeout time.Duration
}
// NewServer initializes a new server using the provided multiplexer for
// the network capabilities. Refer to the server options if you want to specify
// timeouts and error callbacks.
func NewServer(mpx Multiplexer, opts ...ServerOption) Server {
srv := server{
mpx: mpx,
onError: func(err error) {}, // no report
onChanError: func(err error) {}, // no report
onMaxErrMass: func(mass int, err error) {
// TODO: graceful fallback based on the mass value
time.Sleep(30 * time.Second)
},
maxErrMass: 10,
useCompression: false,
timeouts: serverTimeouts{
// chanAcceptTimeout triggers when a channel has been announced
// but nobody has discovered it in time.
chanAcceptTimeout: 30 * time.Second,
},
}
for _, o := range opts {
o(&srv)
}
return srv
}
func (s server) ListenAndServe(vAddr string, srcFn SourceFunc) error {
if s.mpx == nil {
return errors.New("chanserv: mpx not set")
}
l, err := s.mpx.Bind("", vAddr)
if err != nil {
err = fmt.Errorf("chanserv mpx.Bind: %v", err)
return err
}
// warn: serve will close listener
go s.serve(l, srcFn)
return nil
}
func (s server) serve(listener net.Listener, srcFn SourceFunc) {
defer listener.Close()
var errMass int
for {
masterConn, err := listener.Accept()
if err != nil {
err = fmt.Errorf("master listener.Accept: %v", err)
s.onError(err)
errMass++
if s.maxErrMass > 0 && errMass >= s.maxErrMass {
s.onMaxErrMass(errMass, err)
}
continue
}
errMass = 0
go s.serveMaster(masterConn, srcFn)
}
}
func (s server) serveMaster(masterConn net.Conn, srcFn SourceFunc) {
if s.timeouts.servingTimeout > 0 {
masterConn.SetDeadline(time.Now().Add(s.timeouts.servingTimeout))
}
defer masterConn.Close()
if d := s.timeouts.masterReadTimeout; d > 0 {
masterConn.SetReadDeadline(time.Now().Add(d))
}
reqBody, err := readFrame(masterConn, false)
if err != nil {
err = fmt.Errorf("master readFrame: %v", err)
s.onError(err)
return
}
timeout := timerPool.Get().(*time.Timer)
defer func() {
timeout.Stop()
timerPool.Put(timeout)
}()
if d := s.timeouts.sourcingTimeout; d > 0 {
timeout.Reset(d)
}
sourceChan := srcFn(reqBody)
for {
select {
case <-timeout.C:
return
case out, ok := <-sourceChan:
if !ok {
// sourcing is over
return
}
if s.timeouts.sourcingTimeout > 0 {
timeout.Reset(s.timeouts.sourcingTimeout)
}
chanAddr, err := s.bindChannel(out.Out())
if err != nil {
err = fmt.Errorf("master bindChannel: %v", err)
s.onError(err)
continue
}
if d := s.timeouts.masterWriteTimeout; d > 0 {
masterConn.SetWriteDeadline(time.Now().Add(d))
}
if err := writeFrame(masterConn, out.Header()); err == nil {
if err = writeFrame(masterConn, []byte(chanAddr)); err != nil {
err = fmt.Errorf("master writeFrame: %v", err)
s.onError(err)
}
} else {
err = fmt.Errorf("master writeFrame: %v", err)
s.onError(err)
}
}
}
}
func (s server) bindChannel(out <-chan Frame) (string, error) {
l, err := s.mpx.Bind("", ":0") // random port
if err != nil {
err = fmt.Errorf("bindChannel mpx.Bind: %v", err)
s.onError(err)
return "", err
}
c := channel{
Listener: l,
outChan: out,
onError: s.onChanError,
wTimeout: s.timeouts.frameWriteTimeout,
aTimeout: s.timeouts.chanAcceptTimeout,
useCompression: s.useCompression,
}
vAddr := l.Addr().String()
go c.serve(s.timeouts.servingTimeout)
return vAddr, nil
}
type channel struct {
net.Listener
outChan <-chan Frame
wTimeout time.Duration
aTimeout time.Duration
onError func(err error)
useCompression bool
}
func (c channel) serve(timeout time.Duration) {
defer c.Close()
conn, err := acceptTimeout(c, c.aTimeout)
if err != nil {
err = fmt.Errorf("channel acceptTimeout: %v", err)
c.onError(err)
return
}
if timeout > 0 {
conn.SetDeadline(time.Now().Add(timeout))
}
defer conn.Close()
for frame := range c.outChan {
if c.wTimeout > 0 {
conn.SetWriteDeadline(time.Now().Add(c.wTimeout))
}
data := frame.Bytes()
if c.useCompression && needCompression(data) {
if err := writeCompressedFrame(conn, data); err != nil {
err = fmt.Errorf("channel writeCompressedFrame: %v", err)
c.onError(err)
}
continue
}
if err := writeFrame(conn, data); err != nil {
err = fmt.Errorf("channel writeFrame: %v", err)
c.onError(err)
}
}
}