Blame


1 4b33cdd0 2015-11-30 stephen.d package p9p
2 4b33cdd0 2015-11-30 stephen.d
3 4b33cdd0 2015-11-30 stephen.d import (
4 11b5e5c7 2016-05-25 stevvooe "errors"
5 4b33cdd0 2015-11-30 stephen.d "fmt"
6 4b33cdd0 2015-11-30 stephen.d "log"
7 4b33cdd0 2015-11-30 stephen.d "net"
8 b9021eb7 2016-09-19 noreply "sync"
9 4b33cdd0 2015-11-30 stephen.d
10 529e2b2e 2016-11-14 noreply "context"
11 4b33cdd0 2015-11-30 stephen.d )
12 4b33cdd0 2015-11-30 stephen.d
13 4b33cdd0 2015-11-30 stephen.d // roundTripper manages the request and response from the client-side. A
14 4b33cdd0 2015-11-30 stephen.d // roundTripper must abide by similar rules to the http.RoundTripper.
15 4b33cdd0 2015-11-30 stephen.d // Typically, the roundTripper will manage tag assignment and message
16 4b33cdd0 2015-11-30 stephen.d // serialization.
17 4b33cdd0 2015-11-30 stephen.d type roundTripper interface {
18 4b33cdd0 2015-11-30 stephen.d send(ctx context.Context, msg Message) (Message, error)
19 4b33cdd0 2015-11-30 stephen.d }
20 4b33cdd0 2015-11-30 stephen.d
21 4b33cdd0 2015-11-30 stephen.d // transport plays the role of being a client channel manager. It multiplexes
22 4b33cdd0 2015-11-30 stephen.d // function calls onto the wire and dispatches responses to blocking calls to
23 4b33cdd0 2015-11-30 stephen.d // send. On the whole, transport is thread-safe for calling send
24 4b33cdd0 2015-11-30 stephen.d type transport struct {
25 4b33cdd0 2015-11-30 stephen.d ctx context.Context
26 4b33cdd0 2015-11-30 stephen.d ch Channel
27 4b33cdd0 2015-11-30 stephen.d requests chan *fcallRequest
28 b9021eb7 2016-09-19 noreply
29 b9021eb7 2016-09-19 noreply shutdown chan struct{}
30 b9021eb7 2016-09-19 noreply once sync.Once // protect closure of shutdown
31 4b33cdd0 2015-11-30 stephen.d closed chan struct{}
32 4b33cdd0 2015-11-30 stephen.d
33 4b33cdd0 2015-11-30 stephen.d tags uint16
34 4b33cdd0 2015-11-30 stephen.d }
35 4b33cdd0 2015-11-30 stephen.d
36 4b33cdd0 2015-11-30 stephen.d var _ roundTripper = &transport{}
37 4b33cdd0 2015-11-30 stephen.d
38 20882a7b 2016-05-25 stevvooe func newTransport(ctx context.Context, ch Channel) roundTripper {
39 4b33cdd0 2015-11-30 stephen.d t := &transport{
40 4b33cdd0 2015-11-30 stephen.d ctx: ctx,
41 4b33cdd0 2015-11-30 stephen.d ch: ch,
42 4b33cdd0 2015-11-30 stephen.d requests: make(chan *fcallRequest),
43 b9021eb7 2016-09-19 noreply shutdown: make(chan struct{}),
44 4b33cdd0 2015-11-30 stephen.d closed: make(chan struct{}),
45 4b33cdd0 2015-11-30 stephen.d }
46 4b33cdd0 2015-11-30 stephen.d
47 4b33cdd0 2015-11-30 stephen.d go t.handle()
48 4b33cdd0 2015-11-30 stephen.d
49 4b33cdd0 2015-11-30 stephen.d return t
50 4b33cdd0 2015-11-30 stephen.d }
51 4b33cdd0 2015-11-30 stephen.d
52 4b33cdd0 2015-11-30 stephen.d // fcallRequest encompasses the request to send a message via fcall.
53 4b33cdd0 2015-11-30 stephen.d type fcallRequest struct {
54 4b33cdd0 2015-11-30 stephen.d ctx context.Context
55 4b33cdd0 2015-11-30 stephen.d message Message
56 4b33cdd0 2015-11-30 stephen.d response chan *Fcall
57 4b33cdd0 2015-11-30 stephen.d err chan error
58 4b33cdd0 2015-11-30 stephen.d }
59 4b33cdd0 2015-11-30 stephen.d
60 4b33cdd0 2015-11-30 stephen.d func newFcallRequest(ctx context.Context, msg Message) *fcallRequest {
61 4b33cdd0 2015-11-30 stephen.d return &fcallRequest{
62 4b33cdd0 2015-11-30 stephen.d ctx: ctx,
63 4b33cdd0 2015-11-30 stephen.d message: msg,
64 4b33cdd0 2015-11-30 stephen.d response: make(chan *Fcall, 1),
65 4b33cdd0 2015-11-30 stephen.d err: make(chan error, 1),
66 4b33cdd0 2015-11-30 stephen.d }
67 4b33cdd0 2015-11-30 stephen.d }
68 4b33cdd0 2015-11-30 stephen.d
69 4b33cdd0 2015-11-30 stephen.d func (t *transport) send(ctx context.Context, msg Message) (Message, error) {
70 4b33cdd0 2015-11-30 stephen.d req := newFcallRequest(ctx, msg)
71 4b33cdd0 2015-11-30 stephen.d
72 4b33cdd0 2015-11-30 stephen.d // dispatch the request.
73 4b33cdd0 2015-11-30 stephen.d select {
74 4b33cdd0 2015-11-30 stephen.d case <-t.closed:
75 4b33cdd0 2015-11-30 stephen.d return nil, ErrClosed
76 4b33cdd0 2015-11-30 stephen.d case <-ctx.Done():
77 4b33cdd0 2015-11-30 stephen.d return nil, ctx.Err()
78 4b33cdd0 2015-11-30 stephen.d case t.requests <- req:
79 4b33cdd0 2015-11-30 stephen.d }
80 4b33cdd0 2015-11-30 stephen.d
81 4b33cdd0 2015-11-30 stephen.d // wait for the response.
82 4b33cdd0 2015-11-30 stephen.d select {
83 4b33cdd0 2015-11-30 stephen.d case <-t.closed:
84 4b33cdd0 2015-11-30 stephen.d return nil, ErrClosed
85 4b33cdd0 2015-11-30 stephen.d case <-ctx.Done():
86 4b33cdd0 2015-11-30 stephen.d return nil, ctx.Err()
87 4b33cdd0 2015-11-30 stephen.d case err := <-req.err:
88 4b33cdd0 2015-11-30 stephen.d return nil, err
89 4b33cdd0 2015-11-30 stephen.d case resp := <-req.response:
90 4b33cdd0 2015-11-30 stephen.d if resp.Type == Rerror {
91 4b33cdd0 2015-11-30 stephen.d // pack the error into something useful
92 4b33cdd0 2015-11-30 stephen.d respmesg, ok := resp.Message.(MessageRerror)
93 4b33cdd0 2015-11-30 stephen.d if !ok {
94 4b33cdd0 2015-11-30 stephen.d return nil, fmt.Errorf("invalid error response: %v", resp)
95 4b33cdd0 2015-11-30 stephen.d }
96 4b33cdd0 2015-11-30 stephen.d
97 4b33cdd0 2015-11-30 stephen.d return nil, respmesg
98 4b33cdd0 2015-11-30 stephen.d }
99 4b33cdd0 2015-11-30 stephen.d
100 4b33cdd0 2015-11-30 stephen.d return resp.Message, nil
101 4b33cdd0 2015-11-30 stephen.d }
102 4b33cdd0 2015-11-30 stephen.d }
103 4b33cdd0 2015-11-30 stephen.d
104 11b5e5c7 2016-05-25 stevvooe // allocateTag returns a valid tag given a tag pool map. It receives a hint as
105 11b5e5c7 2016-05-25 stevvooe // to where to start the tag search. It returns an error if the allocation is
106 11b5e5c7 2016-05-25 stevvooe // not possible. The provided map must not contain NOTAG as a key.
107 11b5e5c7 2016-05-25 stevvooe func allocateTag(r *fcallRequest, m map[Tag]*fcallRequest, hint Tag) (Tag, error) {
108 11b5e5c7 2016-05-25 stevvooe // The tag pool is depleted if 65535 (0xFFFF) tags are taken.
109 11b5e5c7 2016-05-25 stevvooe if len(m) >= 0xFFFF {
110 11b5e5c7 2016-05-25 stevvooe return 0, errors.New("tag pool depleted")
111 11b5e5c7 2016-05-25 stevvooe }
112 11b5e5c7 2016-05-25 stevvooe
113 11b5e5c7 2016-05-25 stevvooe // Look for the first tag that doesn't exist in the map and return it.
114 11b5e5c7 2016-05-25 stevvooe for i := 0; i < 0xFFFF; i++ {
115 11b5e5c7 2016-05-25 stevvooe hint++
116 11b5e5c7 2016-05-25 stevvooe if hint == NOTAG {
117 11b5e5c7 2016-05-25 stevvooe hint = 0
118 11b5e5c7 2016-05-25 stevvooe }
119 11b5e5c7 2016-05-25 stevvooe
120 11b5e5c7 2016-05-25 stevvooe if _, exists := m[hint]; !exists {
121 11b5e5c7 2016-05-25 stevvooe return hint, nil
122 11b5e5c7 2016-05-25 stevvooe }
123 11b5e5c7 2016-05-25 stevvooe }
124 11b5e5c7 2016-05-25 stevvooe
125 11b5e5c7 2016-05-25 stevvooe return 0, errors.New("allocateTag: unexpected error")
126 11b5e5c7 2016-05-25 stevvooe }
127 11b5e5c7 2016-05-25 stevvooe
128 4b33cdd0 2015-11-30 stephen.d // handle takes messages off the wire and wakes up the waiting tag call.
129 4b33cdd0 2015-11-30 stephen.d func (t *transport) handle() {
130 4b33cdd0 2015-11-30 stephen.d defer func() {
131 b9021eb7 2016-09-19 noreply close(t.closed)
132 4b33cdd0 2015-11-30 stephen.d }()
133 b9021eb7 2016-09-19 noreply
134 4b33cdd0 2015-11-30 stephen.d // the following variable block are protected components owned by this thread.
135 4b33cdd0 2015-11-30 stephen.d var (
136 4b33cdd0 2015-11-30 stephen.d responses = make(chan *Fcall)
137 4b33cdd0 2015-11-30 stephen.d // outstanding provides a map of tags to outstanding requests.
138 4b33cdd0 2015-11-30 stephen.d outstanding = map[Tag]*fcallRequest{}
139 11b5e5c7 2016-05-25 stevvooe selected Tag
140 4b33cdd0 2015-11-30 stephen.d )
141 4b33cdd0 2015-11-30 stephen.d
142 4b33cdd0 2015-11-30 stephen.d // loop to read messages off of the connection
143 4b33cdd0 2015-11-30 stephen.d go func() {
144 4b33cdd0 2015-11-30 stephen.d defer func() {
145 b9021eb7 2016-09-19 noreply t.close() // single main loop
146 4b33cdd0 2015-11-30 stephen.d }()
147 4b33cdd0 2015-11-30 stephen.d loop:
148 4b33cdd0 2015-11-30 stephen.d for {
149 4b33cdd0 2015-11-30 stephen.d fcall := new(Fcall)
150 4b33cdd0 2015-11-30 stephen.d if err := t.ch.ReadFcall(t.ctx, fcall); err != nil {
151 4b33cdd0 2015-11-30 stephen.d switch err := err.(type) {
152 4b33cdd0 2015-11-30 stephen.d case net.Error:
153 4b33cdd0 2015-11-30 stephen.d if err.Timeout() || err.Temporary() {
154 4b33cdd0 2015-11-30 stephen.d // BUG(stevvooe): There may be partial reads under
155 4b33cdd0 2015-11-30 stephen.d // timeout errors where this is actually fatal.
156 4b33cdd0 2015-11-30 stephen.d
157 4b33cdd0 2015-11-30 stephen.d // can only retry if we haven't offset the frame.
158 4b33cdd0 2015-11-30 stephen.d continue loop
159 4b33cdd0 2015-11-30 stephen.d }
160 4b33cdd0 2015-11-30 stephen.d }
161 4b33cdd0 2015-11-30 stephen.d
162 2da28ffa 2017-11-10 noreply log.Println("p9p: fatal error reading msg:", err)
163 4b33cdd0 2015-11-30 stephen.d return
164 4b33cdd0 2015-11-30 stephen.d }
165 4b33cdd0 2015-11-30 stephen.d
166 4b33cdd0 2015-11-30 stephen.d select {
167 4b33cdd0 2015-11-30 stephen.d case <-t.ctx.Done():
168 4b33cdd0 2015-11-30 stephen.d return
169 4b33cdd0 2015-11-30 stephen.d case <-t.closed:
170 4b33cdd0 2015-11-30 stephen.d return
171 4b33cdd0 2015-11-30 stephen.d case responses <- fcall:
172 4b33cdd0 2015-11-30 stephen.d }
173 4b33cdd0 2015-11-30 stephen.d }
174 4b33cdd0 2015-11-30 stephen.d }()
175 4b33cdd0 2015-11-30 stephen.d
176 4b33cdd0 2015-11-30 stephen.d for {
177 4b33cdd0 2015-11-30 stephen.d select {
178 4b33cdd0 2015-11-30 stephen.d case req := <-t.requests:
179 11b5e5c7 2016-05-25 stevvooe var err error
180 4b33cdd0 2015-11-30 stephen.d
181 11b5e5c7 2016-05-25 stevvooe selected, err = allocateTag(req, outstanding, selected)
182 11b5e5c7 2016-05-25 stevvooe if err != nil {
183 11b5e5c7 2016-05-25 stevvooe req.err <- err
184 11b5e5c7 2016-05-25 stevvooe continue
185 11b5e5c7 2016-05-25 stevvooe }
186 11b5e5c7 2016-05-25 stevvooe
187 11b5e5c7 2016-05-25 stevvooe outstanding[selected] = req
188 11b5e5c7 2016-05-25 stevvooe fcall := newFcall(selected, req.message)
189 11b5e5c7 2016-05-25 stevvooe
190 4b33cdd0 2015-11-30 stephen.d // TODO(stevvooe): Consider the case of requests that never
191 4b33cdd0 2015-11-30 stephen.d // receive a response. We need to remove the fcall context from
192 4b33cdd0 2015-11-30 stephen.d // the tag map and dealloc the tag. We may also want to send a
193 4b33cdd0 2015-11-30 stephen.d // flush for the tag.
194 4b33cdd0 2015-11-30 stephen.d if err := t.ch.WriteFcall(req.ctx, fcall); err != nil {
195 4b33cdd0 2015-11-30 stephen.d delete(outstanding, fcall.Tag)
196 4b33cdd0 2015-11-30 stephen.d req.err <- err
197 4b33cdd0 2015-11-30 stephen.d }
198 4b33cdd0 2015-11-30 stephen.d case b := <-responses:
199 4b33cdd0 2015-11-30 stephen.d req, ok := outstanding[b.Tag]
200 4b33cdd0 2015-11-30 stephen.d if !ok {
201 f52b8701 2016-05-19 stevvooe // BUG(stevvooe): The exact handling of an unknown tag is
202 f52b8701 2016-05-19 stevvooe // unclear at this point. These may not necessarily fatal to
203 f52b8701 2016-05-19 stevvooe // the session, since they could be messages that the client no
204 f52b8701 2016-05-19 stevvooe // longer cares for. When we figure this out, replace this
205 f52b8701 2016-05-19 stevvooe // panic with something more sensible.
206 f52b8701 2016-05-19 stevvooe panic(fmt.Sprintf("unknown tag received: %v", b))
207 4b33cdd0 2015-11-30 stephen.d }
208 4b33cdd0 2015-11-30 stephen.d
209 4b33cdd0 2015-11-30 stephen.d // BUG(stevvooe): Must detect duplicate tag and ensure that we are
210 4b33cdd0 2015-11-30 stephen.d // waking up the right caller. If a duplicate is received, the
211 4b33cdd0 2015-11-30 stephen.d // entry should not be deleted.
212 4b33cdd0 2015-11-30 stephen.d delete(outstanding, b.Tag)
213 4b33cdd0 2015-11-30 stephen.d
214 4b33cdd0 2015-11-30 stephen.d req.response <- b
215 4b33cdd0 2015-11-30 stephen.d
216 4b33cdd0 2015-11-30 stephen.d // TODO(stevvooe): Reclaim tag id.
217 b9021eb7 2016-09-19 noreply case <-t.shutdown:
218 b9021eb7 2016-09-19 noreply return
219 4b33cdd0 2015-11-30 stephen.d case <-t.ctx.Done():
220 4b33cdd0 2015-11-30 stephen.d return
221 4b33cdd0 2015-11-30 stephen.d }
222 4b33cdd0 2015-11-30 stephen.d }
223 4b33cdd0 2015-11-30 stephen.d }
224 4b33cdd0 2015-11-30 stephen.d
225 4b33cdd0 2015-11-30 stephen.d func (t *transport) flush(ctx context.Context, tag Tag) error {
226 4b33cdd0 2015-11-30 stephen.d // TODO(stevvooe): We need to fire and forget flush messages when a call
227 4b33cdd0 2015-11-30 stephen.d // context gets cancelled.
228 4b33cdd0 2015-11-30 stephen.d panic("not implemented")
229 4b33cdd0 2015-11-30 stephen.d }
230 4b33cdd0 2015-11-30 stephen.d
231 4b33cdd0 2015-11-30 stephen.d func (t *transport) Close() error {
232 b9021eb7 2016-09-19 noreply t.close()
233 b9021eb7 2016-09-19 noreply
234 4b33cdd0 2015-11-30 stephen.d select {
235 4b33cdd0 2015-11-30 stephen.d case <-t.closed:
236 b9021eb7 2016-09-19 noreply return nil
237 4b33cdd0 2015-11-30 stephen.d case <-t.ctx.Done():
238 4b33cdd0 2015-11-30 stephen.d return t.ctx.Err()
239 4b33cdd0 2015-11-30 stephen.d }
240 b9021eb7 2016-09-19 noreply }
241 4b33cdd0 2015-11-30 stephen.d
242 b9021eb7 2016-09-19 noreply // close starts the shutdown process.
243 b9021eb7 2016-09-19 noreply func (t *transport) close() {
244 b9021eb7 2016-09-19 noreply t.once.Do(func() {
245 b9021eb7 2016-09-19 noreply close(t.shutdown)
246 b9021eb7 2016-09-19 noreply })
247 4b33cdd0 2015-11-30 stephen.d }