-
Notifications
You must be signed in to change notification settings - Fork 9
/
conn_handler.go
137 lines (113 loc) · 3.07 KB
/
conn_handler.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
package isolate
import (
"fmt"
"io"
"runtime"
"golang.org/x/net/context"
)
// Decoder decodes messages from Cocaine-runtime
type Decoder interface {
Decode(interface{}) error
}
// Encoder sends replies to the Cocaine-runtime
type Encoder interface {
Encode(interface{}) error
}
type message struct {
Channel int
Number int
Args []interface{}
}
func (m *message) String() string {
return fmt.Sprintf("%d %d %v", m.Channel, m.Number, m.Args)
}
// Dispatcher handles incoming messages and keeps the state of the channel
type Dispatcher interface {
Handle(msg *message) (Dispatcher, error)
}
// ConnectionHandler provides method to handle accepted connection for Listener
type ConnectionHandler struct {
ctx context.Context
session map[int]Dispatcher
highestChannel int
newDecoder decoderInit
newDispatcher dispatcherInit
}
// NewConnectionHandler creates new ConnectionHandler
func NewConnectionHandler(ctx context.Context) (*ConnectionHandler, error) {
ctx = withArgsUnpacker(ctx, msgpackArgsDecoder{})
return newConnectionHandler(ctx, newMsgpackDecoder, newInitialDispatch)
}
func newConnectionHandler(ctx context.Context, newDec decoderInit, newDisp dispatcherInit) (*ConnectionHandler, error) {
return &ConnectionHandler{
ctx: ctx,
session: make(map[int]Dispatcher),
highestChannel: 0,
newDecoder: newDec,
newDispatcher: newDisp,
}, nil
}
// HandleConn decodes commands from Cocaine runtime and calls dispatchers
func (h *ConnectionHandler) HandleConn(conn io.ReadWriteCloser) {
defer conn.Close()
decoder := h.newDecoder(conn)
for {
var msg message
err := decoder.Decode(&msg)
if err != nil {
if err == io.EOF {
GetLogger(h.ctx).Warnf("remote side has closed the connection")
} else {
GetLogger(h.ctx).WithError(err).Errorf("unable to Decode protocol message. Close the connection")
}
return
}
// NOTE: it can be the bottleneck
dispatcher, ok := h.session[msg.Channel]
if !ok {
if msg.Number < h.highestChannel {
GetLogger(h.ctx).Errorf("channel has been revoked: %d %d", msg.Number, h.highestChannel)
continue
}
// TODO: refactor
var dw = newDownstream(newMsgpackEncoder(conn), msg.Channel)
ctx := withDownstream(h.ctx, dw)
dispatcher = h.newDispatcher(ctx)
}
dispatcher, err = dispatcher.Handle(&msg)
if err != nil {
GetLogger(h.ctx).WithError(err).Errorf("Handle returned an error")
delete(h.session, msg.Channel)
continue
}
if dispatcher == nil {
delete(h.session, msg.Channel)
continue
}
h.session[msg.Channel] = dispatcher
}
}
type downstream struct {
enc Encoder
channel int
}
func newDownstream(enc Encoder, channel int) Downstream {
return &downstream{
enc: enc,
channel: channel,
}
}
func (d *downstream) Reply(code int, args ...interface{}) error {
if args == nil {
args = []interface{}{}
}
var msg = message{
Channel: d.channel,
Number: code,
Args: args,
}
pc, file, line, _ := runtime.Caller(2)
f := runtime.FuncForPC(pc)
fmt.Printf("%s:%d %s %v\n", file, line, f.Name(), msg)
return d.enc.Encode(msg)
}