-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.go
More file actions
291 lines (260 loc) · 10 KB
/
session.go
File metadata and controls
291 lines (260 loc) · 10 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package netchan
/* TODO:
- more tests
- documentation
- tune desired batch size
- adjust Quit timeout
- think about bufio.Writer api
- false sharing?
*/
import (
"io"
"net"
"reflect"
"sync/atomic"
"time"
)
// NewContext returns a Context that is canceled either when parent is canceled or when
// ssn ends.
/*func NewContext(parent context.Context, ssn *Session) context.Context {
ctx, cancel := context.WithCancel(parent)
go func() {
select {
case <-ssn.Done():
cancel()
case <-ctx.Done():
}
}()
return ctx
}*/
// once is an implementation of sync.Once that uses a channel.
type once struct {
done chan struct{}
state int32
}
// once state
const (
onceNotDone int32 = iota
onceDoing
onceDone
)
func (o *once) Do(f func()) {
if atomic.LoadInt32(&o.state) == onceDone {
return
}
// Slow path.
won := atomic.CompareAndSwapInt32(&o.state, onceNotDone, onceDoing)
if !won {
<-o.done
return
}
f()
atomic.StoreInt32(&o.state, onceDone)
close(o.done)
}
// A Session handles the message traffic of its connection, implementing the netchan
// protocol.
type Session struct {
id int64
conn io.ReadWriteCloser
recvMn *recvManager
sendMn *sendManager
errOnce, closeOnce once
err, closeErr error
}
/*
This graph shows how the goroutines and channels of a session are organized:
+-----------+ +---------+ +---------+ +-------------+
====> | sender | =====> | encoder | ====> | decoder | ====> | recvManager |
+-----------+ +---------+ +---------+ +-------------+
^ ||
| \/
+-------------+ +---------+ +---------+ +-----------+
| sendManager | <---- | decoder | <==== | encoder | <----- | receiver | ---->
+-------------+ +---------+ +---------+ +-----------+
The sender has a table that contains an entry for each channel that has been opened for
sending. The user values flow through a pipeline from the sender to the receiver on the
other side of the connection.
Credits flow in the opposite direction. There is no cycle, as, for example, the sender
shares the table with the credit receiver and they do not communicate through channels.
The former graph is a simplification, because each session has actually both a sender and
a receiver:
----> +----------+ +----------+ ---->
----> | sender | ---\ /--> | receiver | ---->
----> +----------+ \ / +----------+ ---->
[send table] \--> +---------+ +---------+ ---/ [recv table]
+----------+ | encoder | ====> | decoder | +----------+
| credRecv | <-\ /-> +---------+ +---------+ --\ /-- | credSend |
+----------+ \ / \ / +----------+
X X
+----------+ / \ / \ +----------+
| credSend | --/ \-- +---------+ +---------+ <-/ \-> | credRecv |
+----------+ | decoder | <==== | encoder | +----------+
[recv table] /--- +---------+ +---------+ <--\ [send table]
<---- +----------+ / \ +----------+ <----
<---- | receiver | <--/ \--- | sender | <----
<---- +----------+ +----------+ <----
To avoid deadlocked/leaking goroutines, termination must happen in pipeline order. For
example, the sender and the credit sender both check periodically if an error occurred
with Err(). If so, they close the channels to the encoder. The encoder does not check
Err and keeps draining the channels until they are empty. Then it sends the error to
the peer and closes the connection.
*/
const (
minMsgSizeLimit = 512
defMsgSizeLimit = 16 * 1024
maxNameLen = 500
)
// NewSession starts a new session for the specified connection and returns it. The
// connection can be any full-duplex io.ReadWriteCloser that provides in-order delivery
// of data with best-effort reliability. On each end, a connection must have only one
// session.
//
// There is a default limit imposed on the size of incoming gob messages. To change it,
// use NewSessionLimit.
//
// NewSessionLimit is like NewSession, but also allows to specify the maximum size of the gob
// messages that will be accepted from the connection. If msgSizeLimit is 0 or negative,
// the default will be used. When a too big message is received, an error is signaled on
// this session and the session shuts down.
func NewSession(conn io.ReadWriteCloser) *Session {
return NewSessionLimit(conn, defMsgSizeLimit)
}
var newSessionId int64
const internalChCap int = 8
func NewSessionLimit(conn io.ReadWriteCloser, msgSizeLimit int) *Session {
if msgSizeLimit < minMsgSizeLimit {
msgSizeLimit = minMsgSizeLimit
}
// create all the components, connect them with channels and fire up the goroutines.
ssn := &Session{id: atomic.AddInt64(&newSessionId, 1), conn: conn}
ssn.errOnce.done = make(chan struct{})
ssn.closeOnce.done = make(chan struct{})
encDataCh := make(chan data, internalChCap)
encCredCh := make(chan credit, internalChCap)
decDataCh := make(chan data, internalChCap)
decCredCh := make(chan credit, internalChCap)
enc := newEncoder(ssn, encDataCh, encCredCh, conn)
dec := newDecoder(ssn, decDataCh, decCredCh, conn, msgSizeLimit)
recvMn := &recvManager{ssn: ssn, dataCh: decDataCh, toEncoder: encCredCh,
types: &dec.types}
recvMn.table.buffer = make(map[int]*buffer)
recvMn.table.chInfo = make(map[string]rChanInfo)
ssn.recvMn = recvMn
sendMn := &sendManager{ssn: ssn, creditCh: decCredCh, toEncoder: encDataCh}
sendMn.table.chans = make(map[int]sChans)
sendMn.table.chInfo = make(map[string]sChanInfo)
ssn.sendMn = sendMn
go enc.run()
go dec.run()
go recvMn.run()
go sendMn.run()
netConn, ok := conn.(net.Conn)
if ok {
logDebug("netchan session %d started on connection (local %s, remote %s)",
ssn.id, netConn.LocalAddr(), netConn.RemoteAddr())
} else {
logDebug("netchan session %d started", ssn.id)
}
go func() {
<-ssn.Done()
logDebug("netchan session %d shut down with error: %s", ssn.id, ssn.Err())
}()
return ssn
}
// Open method opens a net-chan with the given name and direction on the connection
// handled by the session. The channel argument must be a channel and will be used for
// receiving or sending data on this net-chan.
//
// If the direction is Recv, the following rules apply: channel must be buffered and its
// buffer must be empty (cap(channel) > 0 && len(channel) == 0); the channel must be used
// exclusively for receiving values from a single net-chan.
//
// Opening a net-chan twice, i.e. with the same name and direction on the same session,
// will return an error. It is possible to have, on a single session/connection, two
// net-chans with the same name and opposite directions.
//
// An eventual error returned by Open does not compromise the netchan session, that is,
// the error will not be caught by Done and Err methods, will not be
// communicated to the peer and the session will not shut down.
//
// To close a net-chan, close the channel used for sending; the receiving channel on the
// other peer will be closed too. Messages that are already in the buffers or in flight
// will not be lost.
func (m *Session) OpenSend(name string, channel interface{}) error {
if len(name) > maxNameLen {
return fmtErr("OpenSend: name too long")
}
ch := reflect.ValueOf(channel)
if ch.Kind() != reflect.Chan {
return fmtErr("OpenSend: channel arg is not a channel")
}
if ch.Type().ChanDir()&reflect.RecvDir == 0 {
return fmtErr("OpenSend requires a <-chan")
}
return m.sendMn.open(name, ch)
}
func (m *Session) OpenRecv(name string, channel interface{}, bufferCap int) error {
if len(name) > maxNameLen {
return fmtErr("OpenRecv: name too long")
}
ch := reflect.ValueOf(channel)
if ch.Kind() != reflect.Chan {
return fmtErr("OpenRecv channel is not a channel")
}
if ch.Type().ChanDir()&reflect.SendDir == 0 {
return fmtErr("OpenRecv requires a chan<-")
}
if bufferCap <= 0 {
return fmtErr("OpenRecv bufferCap must be at least 1")
}
return m.recvMn.open(name, ch, bufferCap)
}
// Err returns the first error that occurred on this session. If no error
// occurred, it returns nil. When an error occurs, the session tries to communicate it to
// the peer and then shuts down.
func (m *Session) Err() error {
if atomic.LoadInt32(&m.errOnce.state) == onceDone {
return m.err
}
return nil
}
// Done returns a channel that never receives any message and is closed when an
// error occurs on this session.
func (m *Session) Done() <-chan struct{} {
return m.errOnce.done
}
// Quit tries to send a termination message to the peer and then shuts down the
// session and closes the connection. The Done channel is closed and Err will
// return EndOfSession. The remote peer will also shut down and get EndOfSession, if the termination
// message is received correctly.
//
// The return value is the result of calling Close on the connection. The connection is
// guaranteed to be closed once and only once, even if Quit is called multiple times,
// possibly by multiple goroutines.
func (m *Session) Quit() error {
return m.QuitWith(EndOfSession)
}
func (m *Session) closeConn() {
m.closeOnce.Do(func() {
m.closeErr = m.conn.Close()
})
}
// QuitWith is like Quit, but err is signaled instead of EndOfSession.
func (m *Session) QuitWith(err error) error {
if err == nil {
err = EndOfSession
}
m.errOnce.Do(func() {
m.err = err
})
select {
// encoder tries to send error to peer; if/when it succeeds,
// it closes the connection and we wake up and return
case <-m.closeOnce.done:
// if encoder takes too long, we close the connection ourself
case <-time.After(1 * time.Second):
m.closeConn()
}
return m.closeErr
}