This repository has been archived by the owner on Mar 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
server.go
253 lines (218 loc) · 6.31 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
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
package p9p
import (
"fmt"
"log"
"net"
"sync"
"time"
"context"
)
// TODO(stevvooe): Add net/http.Server-like type here to manage connections.
// Coupled with Handler mux, we can get a very http-like experience for 9p
// servers.
// ServeConn the 9p handler over the provided network connection.
func ServeConn(ctx context.Context, cn net.Conn, handler Handler) error {
// TODO(stevvooe): It would be nice if the handler could declare the
// supported version. Before we had handler, we used the session to get
// the version (msize, version := session.Version()). We must decided if
// we want to proxy version and message size decisions all the back to the
// origin server or make those decisions at each link of a proxy chain.
ch := newChannel(cn, codec9p{}, DefaultMSize)
negctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
// TODO(stevvooe): For now, we negotiate here. It probably makes sense to
// do this outside of this function and then pass in a ready made channel.
// We are not really ready to export the channel type yet.
if err := servernegotiate(negctx, ch, DefaultVersion); err != nil {
// TODO(stevvooe): Need better error handling and retry support here.
return fmt.Errorf("error negotiating version: %s", err)
}
ctx = withVersion(ctx, DefaultVersion)
c := &conn{
ctx: ctx,
ch: ch,
handler: handler,
closed: make(chan struct{}),
}
return c.serve()
}
// conn plays role of session dispatch for handler in a server.
type conn struct {
ctx context.Context
session Session
ch Channel
handler Handler
once sync.Once
closed chan struct{}
err error // terminal error for the conn
}
// activeRequest includes information about the active request.
type activeRequest struct {
ctx context.Context
request *Fcall
cancel context.CancelFunc
}
// serve messages on the connection until an error is encountered.
func (c *conn) serve() error {
tags := map[Tag]*activeRequest{} // active requests
requests := make(chan *Fcall) // sync, read-limited
responses := make(chan *Fcall) // sync, goroutine consumed
completed := make(chan *Fcall) // sync, send in goroutine per request
// read loop
go c.read(requests)
go c.write(responses)
for {
select {
case req := <-requests:
if _, ok := tags[req.Tag]; ok {
select {
case responses <- newErrorFcall(req.Tag, ErrDuptag):
// Send to responses, bypass tag management.
case <-c.ctx.Done():
return c.ctx.Err()
case <-c.closed:
return c.err
}
continue
}
switch msg := req.Message.(type) {
case MessageTflush:
var resp *Fcall
// check if we have actually know about the requested flush
active, ok := tags[msg.Oldtag]
if ok {
active.cancel() // propagate cancellation to callees
delete(tags, msg.Oldtag)
resp = newFcall(req.Tag, MessageRflush{})
} else {
resp = newErrorFcall(req.Tag, ErrUnknownTag)
}
select {
case responses <- resp:
// bypass tag management in completed.
case <-c.ctx.Done():
return c.ctx.Err()
case <-c.closed:
return c.err
}
default:
// Allows us to session handlers to cancel processing of the fcall
// through context.
ctx, cancel := context.WithCancel(c.ctx)
// The contents of these instances are only writable in the main
// server loop. The value of tag will not change.
tags[req.Tag] = &activeRequest{
ctx: ctx,
request: req,
cancel: cancel,
}
go func(ctx context.Context, req *Fcall) {
// TODO(stevvooe): Re-write incoming Treads so that handler
// can always respond with a message of the correct msize.
var resp *Fcall
msg, err := c.handler.Handle(ctx, req.Message)
if err != nil {
// all handler errors are forwarded as protocol errors.
resp = newErrorFcall(req.Tag, err)
} else {
resp = newFcall(req.Tag, msg)
}
select {
case completed <- resp:
case <-ctx.Done():
return
case <-c.closed:
return
}
}(ctx, req)
}
case resp := <-completed:
// only responses that flip the tag state traverse this section.
active, ok := tags[resp.Tag]
if !ok {
// The tag is no longer active. Likely a flushed message.
continue
}
select {
case responses <- resp:
case <-active.ctx.Done():
// the context was canceled for some reason, perhaps timeout or
// due to a flush call. We treat this as a condition where a
// response should not be sent.
}
delete(tags, resp.Tag)
case <-c.ctx.Done():
return c.ctx.Err()
case <-c.closed:
return c.err
}
}
}
// read takes requests off the channel and sends them on requests.
func (c *conn) read(requests chan *Fcall) {
for {
req := new(Fcall)
if err := c.ch.ReadFcall(c.ctx, req); err != nil {
if err, ok := err.(net.Error); ok {
if err.Timeout() || err.Temporary() {
// TODO(stevvooe): A full idle timeout on the connection
// should be enforced here. No logging because it is quite
// chatty.
continue
}
}
c.CloseWithError(fmt.Errorf("error reading fcall: %v", err))
return
}
select {
case requests <- req:
case <-c.ctx.Done():
c.CloseWithError(c.ctx.Err())
return
case <-c.closed:
return
}
}
}
func (c *conn) write(responses chan *Fcall) {
for {
select {
case resp := <-responses:
// TODO(stevvooe): Correctly protect againt overflowing msize from
// handler. This can be done above, in the main message handler
// loop, by adjusting incoming Tread calls to have a Count that
// won't overflow the msize.
if err := c.ch.WriteFcall(c.ctx, resp); err != nil {
if err, ok := err.(net.Error); ok {
if err.Timeout() || err.Temporary() {
// TODO(stevvooe): A full idle timeout on the
// connection should be enforced here. We log here,
// since this is less common.
log.Printf("p9p: temporary error writing fcall: %v", err)
continue
}
}
c.CloseWithError(fmt.Errorf("error writing fcall: %v", err))
return
}
case <-c.ctx.Done():
c.CloseWithError(c.ctx.Err())
return
case <-c.closed:
return
}
}
}
func (c *conn) Close() error {
return c.CloseWithError(nil)
}
func (c *conn) CloseWithError(err error) error {
c.once.Do(func() {
if err == nil {
err = ErrClosed
}
c.err = err
close(c.closed)
})
return c.err
}