8 "golang.org/x/net/context"
11 // roundTripper manages the request and response from the client-side. A
12 // roundTripper must abide by many of the rules of http.RoundTripper.
13 // Typically, the roundTripper will manage tag assignment and message
15 type roundTripper interface {
16 send(ctx context.Context, fc *Fcall) (*Fcall, error)
19 type transport struct {
22 requests chan *fcallRequest
28 func newTransport(ctx context.Context, ch *channel) roundTripper {
32 requests: make(chan *fcallRequest),
33 closed: make(chan struct{}),
41 type fcallRequest struct {
48 func newFcallRequest(ctx context.Context, fcall *Fcall) *fcallRequest {
52 response: make(chan *Fcall, 1),
53 err: make(chan error, 1),
57 func (t *transport) send(ctx context.Context, fcall *Fcall) (*Fcall, error) {
58 req := newFcallRequest(ctx, fcall)
60 log.Println("dispatch", fcall)
61 // dispatch the request.
67 case t.requests <- req:
70 log.Println("wait", fcall)
71 // wait for the response.
77 case err := <-req.err:
79 case resp := <-req.response:
80 log.Println("resp", resp)
81 if resp.Type == Rerror {
82 // pack the error into something useful
83 respmesg, ok := resp.Message.(*MessageRerror)
85 return nil, fmt.Errorf("invalid error response: %v", resp)
88 return nil, new9pError(respmesg.Ename)
95 // handle takes messages off the wire and wakes up the waiting tag call.
96 func (t *transport) handle() {
98 log.Println("exited handle loop")
101 // the following variable block are protected components owned by this thread.
103 responses = make(chan *Fcall)
105 // outstanding provides a map of tags to outstanding requests.
106 outstanding = map[Tag]*fcallRequest{}
109 // loop to read messages off of the connection
112 log.Println("exited read loop")
118 if err := t.ch.readFcall(t.ctx, fcall); err != nil {
119 switch err := err.(type) {
121 if err.Timeout() || err.Temporary() {
122 // BUG(stevvooe): There may be partial reads under
123 // timeout errors where this is actually fatal.
125 // can only retry if we haven't offset the frame.
130 log.Println("fatal error reading msg:", err)
137 log.Println("ctx done")
140 log.Println("transport closed")
142 case responses <- fcall:
148 log.Println("wait...")
150 case req := <-t.requests:
151 if req.fcall.Tag == NOTAG {
152 // NOTE(stevvooe): We disallow fcalls with NOTAG to come
153 // through this path since we can't join the tagged response
154 // with the waiting caller. This is typically used for the
155 // Tversion/Rversion round trip to setup a session.
157 // It may be better to allow these through but block all
158 // requests until a notag message has a response.
160 req.err <- fmt.Errorf("disallowed tag through transport")
164 // BUG(stevvooe): This is an awful tag allocation procedure.
165 // Replace this with something that let's us allocate tags and
166 // associate data with them, returning to them to a pool when
167 // complete. Such a system would provide a lot of information
168 // about outstanding requests.
171 outstanding[req.fcall.Tag] = req
173 // TODO(stevvooe): Consider the case of requests that never
174 // receive a response. We need to remove the fcall context from
175 // the tag map and dealloc the tag. We may also want to send a
176 // flush for the tag.
177 if err := t.ch.writeFcall(req.ctx, req.fcall); err != nil {
178 log.Println("error writing fcall", err, req.fcall)
179 delete(outstanding, req.fcall.Tag)
183 log.Println("sent", req.fcall)
184 case b := <-responses:
185 log.Println("recv", b)
186 req, ok := outstanding[b.Tag]
188 panic("unknown tag received")
190 delete(outstanding, req.fcall.Tag)
194 // TODO(stevvooe): Reclaim tag id.
203 func (t *transport) Close() error {