-
Notifications
You must be signed in to change notification settings - Fork 18
/
stack.go
63 lines (58 loc) · 1.55 KB
/
stack.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
package styx
// Stack combines multiple handlers into one. When a new message is received
// from the client, it is passed to each handler, from left to right, until a
// response is sent. If no response is sent. by any handlers in the stack,
// the documented default response for that message type will be sent to
// the client. Handlers may use the UpdateRequest to pass information to
// downstream handlers.
func Stack(handlers ...Handler) Handler {
h := make([]Handler, len(handlers))
copy(h, handlers)
return stack(h)
}
type stack []Handler
func (handlers stack) Serve9P(s *Session) {
running := make([]Session, len(handlers))
for i, handler := range handlers {
sub := &running[i]
sub.User = s.User
sub.Access = s.Access
sub.requests = make(chan Request)
sub.pipeline = make(chan Request)
sub.authC = s.authC
sub.conn = s.conn
sub.RefCount = s.RefCount
sub.files = s.files
go func(h Handler) {
h.Serve9P(sub)
close(sub.pipeline)
}(handler)
}
for s.Next() {
req := s.Request()
for i := range running {
session := &running[i]
req.setSession(session)
session.requests <- req
if next, ok := <-session.pipeline; !ok {
// A handler has exited prematurely. abort
goto Cleanup
} else if next == nil {
// The request has been handled, no point
// in passing it down the chain.
s.unhandled = false
break
} else {
req = next
}
}
req.setSession(s)
}
Cleanup:
for i := range running {
close(running[i].requests)
// Wait for the handler to exit
for range running[i].pipeline {
}
}
}