-
Notifications
You must be signed in to change notification settings - Fork 88
/
router_context.go
259 lines (213 loc) · 6.01 KB
/
router_context.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
254
255
256
257
258
259
package easytcp
import (
"context"
"fmt"
"sync"
"time"
)
// Context is a generic context in a message routing.
// It allows us to pass variables between handler and middlewares.
type Context interface {
context.Context
// WithContext sets the underline context.
// It's very useful to control the workflow when send to response channel.
WithContext(ctx context.Context) Context
// Session returns the current session.
Session() Session
// SetSession sets session.
SetSession(sess Session) Context
// Request returns request message.
Request() *Message
// SetRequest encodes data with session's codec and sets request message.
SetRequest(id, data interface{}) error
// MustSetRequest encodes data with session's codec and sets request message.
// panics on error.
MustSetRequest(id, data interface{}) Context
// SetRequestMessage sets request message directly.
SetRequestMessage(msg *Message) Context
// Bind decodes request message to v.
Bind(v interface{}) error
// Response returns the response message.
Response() *Message
// SetResponse encodes data with session's codec and sets response message.
SetResponse(id, data interface{}) error
// MustSetResponse encodes data with session's codec and sets response message.
// panics on error.
MustSetResponse(id, data interface{}) Context
// SetResponseMessage sets response message directly.
SetResponseMessage(msg *Message) Context
// Send sends itself to current session.
Send() bool
// SendTo sends itself to session.
SendTo(session Session) bool
// Get returns key value from storage.
Get(key string) (value interface{}, exists bool)
// Set store key value into storage.
Set(key string, value interface{})
// Remove deletes the key from storage.
Remove(key string)
// Copy returns a copy of Context.
Copy() Context
}
var _ Context = &routeContext{} // implementation check
// newContext creates a routeContext pointer.
func newContext() *routeContext {
return &routeContext{
rawCtx: context.Background(),
}
}
// routeContext implements the Context interface.
type routeContext struct {
rawCtx context.Context
mu sync.RWMutex
storage map[string]interface{}
session Session
reqMsg *Message
respMsg *Message
}
// Deadline implements the context.Context Deadline method.
func (c *routeContext) Deadline() (time.Time, bool) {
return c.rawCtx.Deadline()
}
// Done implements the context.Context Done method.
func (c *routeContext) Done() <-chan struct{} {
return c.rawCtx.Done()
}
// Err implements the context.Context Err method.
func (c *routeContext) Err() error {
return c.rawCtx.Err()
}
// Value implements the context.Context Value method.
func (c *routeContext) Value(key interface{}) interface{} {
if keyAsString, ok := key.(string); ok {
val, _ := c.Get(keyAsString)
return val
}
return nil
}
// WithContext sets the underline context.
func (c *routeContext) WithContext(ctx context.Context) Context {
c.rawCtx = ctx
return c
}
// Session implements Context.Session method.
func (c *routeContext) Session() Session {
return c.session
}
// SetSession sets session.
func (c *routeContext) SetSession(sess Session) Context {
c.session = sess
return c
}
// Request implements Context.Request method.
func (c *routeContext) Request() *Message {
return c.reqMsg
}
// SetRequest sets request by id and data.
func (c *routeContext) SetRequest(id, data interface{}) error {
codec := c.session.Codec()
if codec == nil {
return fmt.Errorf("codec is nil")
}
dataBytes, err := codec.Encode(data)
if err != nil {
return err
}
c.reqMsg = NewMessage(id, dataBytes)
return nil
}
// MustSetRequest implements Context.MustSetRequest method.
func (c *routeContext) MustSetRequest(id, data interface{}) Context {
if err := c.SetRequest(id, data); err != nil {
panic(err)
}
return c
}
// SetRequestMessage sets request message.
func (c *routeContext) SetRequestMessage(msg *Message) Context {
c.reqMsg = msg
return c
}
// Bind implements Context.Bind method.
func (c *routeContext) Bind(v interface{}) error {
if c.session.Codec() == nil {
return fmt.Errorf("message codec is nil")
}
return c.session.Codec().Decode(c.reqMsg.Data(), v)
}
// Response implements Context.Response method.
func (c *routeContext) Response() *Message {
return c.respMsg
}
// SetResponse implements Context.SetResponse method.
func (c *routeContext) SetResponse(id, data interface{}) error {
codec := c.session.Codec()
if codec == nil {
return fmt.Errorf("codec is nil")
}
dataBytes, err := codec.Encode(data)
if err != nil {
return err
}
c.respMsg = NewMessage(id, dataBytes)
return nil
}
// MustSetResponse implements Context.MustSetResponse method.
func (c *routeContext) MustSetResponse(id, data interface{}) Context {
if err := c.SetResponse(id, data); err != nil {
panic(err)
}
return c
}
// SetResponseMessage implements Context.SetResponseMessage method.
func (c *routeContext) SetResponseMessage(msg *Message) Context {
c.respMsg = msg
return c
}
// Send implements Context.Send method.
func (c *routeContext) Send() bool {
return c.session.Send(c)
}
// SendTo implements Context.SendTo method.
func (c *routeContext) SendTo(sess Session) bool {
return sess.Send(c)
}
// Get implements Context.Get method.
func (c *routeContext) Get(key string) (value interface{}, exists bool) {
c.mu.RLock()
value, exists = c.storage[key]
c.mu.RUnlock()
return
}
// Set implements Context.Set method.
func (c *routeContext) Set(key string, value interface{}) {
c.mu.Lock()
if c.storage == nil {
c.storage = make(map[string]interface{})
}
c.storage[key] = value
c.mu.Unlock()
}
// Remove implements Context.Remove method.
func (c *routeContext) Remove(key string) {
c.mu.Lock()
delete(c.storage, key)
c.mu.Unlock()
}
// Copy implements Context.Copy method.
func (c *routeContext) Copy() Context {
return &routeContext{
rawCtx: c.rawCtx,
storage: c.storage,
session: c.session,
reqMsg: c.reqMsg,
respMsg: c.respMsg,
}
}
func (c *routeContext) reset() {
c.rawCtx = context.Background()
c.session = nil
c.reqMsg = nil
c.respMsg = nil
c.storage = nil
}