forked from lesismal/arpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
192 lines (168 loc) · 4.31 KB
/
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
// Copyright 2020 lesismal. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package arpc
import (
"math"
"sync"
"time"
)
var (
contextPool = sync.Pool{
New: func() interface{} {
return &Context{}
},
}
emptyContext = Context{}
)
// Context represents an arpc Call's context.
type Context struct {
Client *Client
Message *Message
index int
handlers []HandlerFunc
}
func (ctx *Context) Release() {
ctx.Message.Release()
*ctx = emptyContext
contextPool.Put(ctx)
}
// Get returns value for key.
func (ctx *Context) Get(key interface{}) (interface{}, bool) {
if len(ctx.Message.values) == 0 {
return nil, false
}
value, ok := ctx.Message.values[key]
return value, ok
}
// Set sets key-value pair.
func (ctx *Context) Set(key interface{}, value interface{}) {
if key == nil || value == nil {
return
}
if ctx.Message.values == nil {
ctx.Message.values = map[interface{}]interface{}{}
}
ctx.Message.values[key] = value
}
// Values returns values.
func (ctx *Context) Values() map[interface{}]interface{} {
if ctx.Message == nil {
return nil
}
return ctx.Message.values
}
// Body returns body.
func (ctx *Context) Body() []byte {
return ctx.Message.Data()
}
// Bind parses the body data and stores the result
// in the value pointed to by v.
func (ctx *Context) Bind(v interface{}) error {
msg := ctx.Message
if msg.IsError() {
return msg.Error()
}
if v != nil {
data := msg.Data()
switch vt := v.(type) {
case *[]byte:
*vt = data
case *string:
*vt = string(data)
// case *error:
// *vt = errors.New(util.BytesToStr(data))
default:
return ctx.Client.Codec.Unmarshal(data, v)
}
}
return nil
}
// Write responses a Message to the Client.
func (ctx *Context) Write(v interface{}) error {
return ctx.write(v, false, TimeForever)
}
// WriteWithTimeout responses a Message to the Client with timeout.
func (ctx *Context) WriteWithTimeout(v interface{}, timeout time.Duration) error {
return ctx.write(v, false, timeout)
}
// Error responses an error Message to the Client.
func (ctx *Context) Error(v interface{}) error {
return ctx.write(v, v != nil, TimeForever)
}
// Next calls next middleware or method/router handler.
func (ctx *Context) Next() {
index := int(ctx.index)
if index < len(ctx.handlers) {
ctx.index++
ctx.handlers[index](ctx)
}
}
// Abort stops the one-by-one-calling of middlewares and method/router handler.
func (ctx *Context) Abort() {
ctx.index = int(math.MaxInt8)
}
// Deadline implements stdlib's Context.
func (ctx *Context) Deadline() (deadline time.Time, ok bool) {
return
}
// Done implements stdlib's Context.
func (ctx *Context) Done() <-chan struct{} {
return nil
}
// Err implements stdlib's Context.
func (ctx *Context) Err() error {
return nil
}
// Value returns the value associated with this context for key, implements stdlib's Context.
func (ctx *Context) Value(key interface{}) interface{} {
value, _ := ctx.Get(key)
return value
}
func (ctx *Context) write(v interface{}, isError bool, timeout time.Duration) error {
cli := ctx.Client
if !cli.Handler.AsyncWrite() {
return ctx.writeDirectly(v, isError)
}
req := ctx.Message
if req.Cmd() != CmdRequest {
return ErrContextResponseToNotify
}
if _, ok := v.(error); ok {
isError = true
}
rsp := newMessage(CmdResponse, req.method(), v, isError, req.IsAsync(), req.Seq(), cli.Handler, cli.Codec, ctx.Message.values)
return cli.PushMsg(rsp, timeout)
}
func (ctx *Context) writeDirectly(v interface{}, isError bool) error {
cli := ctx.Client
req := ctx.Message
if req.Cmd() != CmdRequest {
return ErrContextResponseToNotify
}
if _, ok := v.(error); ok {
isError = true
}
rsp := newMessage(CmdResponse, req.method(), v, isError, req.IsAsync(), req.Seq(), cli.Handler, cli.Codec, ctx.Message.values)
if !cli.reconnecting {
coders := cli.Handler.Coders()
for j := 0; j < len(coders); j++ {
rsp = coders[j].Encode(cli, rsp)
}
_, err := cli.Handler.Send(cli.Conn, rsp.Buffer)
if err != nil {
cli.Conn.Close()
}
return err
}
cli.dropMessage(rsp)
return ErrClientReconnecting
}
func newContext(cli *Client, msg *Message, handlers []HandlerFunc) *Context {
ctx := contextPool.Get().(*Context)
ctx.Client = cli
ctx.Message = msg
ctx.Message.values = msg.values
ctx.handlers = handlers
return ctx
}