-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis.v
278 lines (226 loc) · 5.47 KB
/
redis.v
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
module vredis
import context
import pool
import proto
pub const (
nil_value = proto.nil_value
)
[heap]
struct BaseClient {
opt Options
mut:
conn_pool pool.Pooler
}
pub struct Client {
BaseClient
Cmdble_
}
pub fn new_client(mut opt Options) &Client {
opt.init()
pool := new_conn_pool(opt)
mut b := new_base_client(opt, pool)
mut c := &Client{
conn_pool: pool
BaseClient: b
Cmdble_: Cmdble_{
f: b.process
}
}
return c
}
fn new_base_client(opt Options, pool pool.Pooler) &BaseClient {
return &BaseClient{
opt: opt
conn_pool: pool
}
}
pub fn (mut c BaseClient) pipeline() &Pipeline {
mut pipe := Pipeline{
exec_: c.process_pipeline
}
pipe.init()
return &pipe
}
fn (mut c BaseClient) process_pipeline(mut ctx context.Context, mut cmds []Cmd) ! {
unsafe {
c.general_process_pipeline(mut ctx, mut cmds, c.pipeline_process_cmds)!
}
}
fn (mut c BaseClient) general_process_pipeline(mut ctx context.Context, mut cmds []Cmd, p fn (context.Context, mut pool.Conn, mut []Cmd) !bool) ! {
c.with_conn(mut ctx, fn [p, mut cmds] (ctx context.Context, mut cn pool.Conn) ! {
p(ctx, mut cn, mut cmds)!
}) or { return err }
}
fn write_cmds(mut wr proto.Writer, cmds []Cmd) ! {
for _, cmd in cmds {
write_cmd(mut wr, cmd)!
}
}
pub fn (mut c BaseClient) pipeline_process_cmds(ctx context.Context, mut cn pool.Conn, mut cmds []Cmd) !bool {
mut wr := cn.with_writer(ctx, c.opt.write_timeout)!
write_cmds(mut wr, cmds)!
// io.new_buffered_reader({reader: io.make_reader(con)})
mut rd := cn.with_reader(ctx, c.opt.read_timeout)!
pipeline_read_cmds(mut rd, mut cmds)!
return true
}
fn pipeline_read_cmds(mut rd proto.Reader, mut cmds []Cmd) ! {
for _, mut cmd in mut cmds {
cmd.read_reply(mut rd)!
}
}
fn (mut c BaseClient) get_conn(mut ctx context.Context) ?pool.Conn {
// println('get_conn: starting get conn')
mut cn := c.conn_pool.get(mut ctx) or { return err }
// println('get_conn: got connect from pool')
if cn.inited {
return cn
}
// println('get_conn: init_conn')
c.init_conn(mut ctx, mut cn) or {
// println('get_conn: err remove $err')
c.conn_pool.remove(ctx, mut cn, err)
return err
}
// println('get_conn: init_conn_after')
return cn
}
type ConnCallback = fn (context.Context, mut pool.Conn) !
fn (mut c BaseClient) with_conn(mut ctx context.Context, f ConnCallback) ? {
mut cn := c.get_conn(mut ctx) or { return err }
mut last_err := IError(none)
defer {
c.release_conn(ctx, mut cn, error('123'))
}
// TODO: remove after fix
done := ctx.done()
match ctx {
context.EmptyContext {
f(ctx, mut cn) or {
last_err = err
return err
}
return
}
else {}
}
d := chan IError{}
// eprintln('with_conn: try to go spawn')
spawn fn (d chan IError, ctx context.Context, mut cn pool.Conn, f ConnCallback) {
f(ctx, mut cn) or {
d <- err
return
}
d <- IError(none)
}(d, ctx, mut &cn, f)
select {
_ := <-done {
cn.close() or {}
_ := <-d
last_err = ctx.err()
return last_err
}
last_err = <-d {
return last_err
}
}
return
}
fn (mut c BaseClient) process(mut ctx context.Context, mut cmd Cmd) ! {
mut last_err := IError(none)
// println('process: start processing')
for attempt := 0; attempt < c.opt.max_retries; attempt++ {
retry := c.process_(mut ctx, mut cmd, attempt) or {
last_err = err
// TODO: second bool param
if should_retry(err, false) {
continue
}
return last_err
}
if !retry {
return
}
}
return last_err
}
fn (mut c BaseClient) process_(mut ctx context.Context, mut cmd Cmd, attempt int) !bool {
if attempt > 0 {
// TODO: timeoutry
}
// retry_timeout := u32(1)
c.with_conn(mut ctx, fn [mut c, mut cmd] (ctx context.Context, mut cn pool.Conn) ! {
mut wr := cn.with_writer(ctx, c.opt.write_timeout)!
write_cmd(mut wr, cmd)!
// io.new_buffered_reader({reader: io.make_reader(con)})
// TODO: custom timeout for read
mut rd := cn.with_reader(ctx, c.opt.read_timeout)!
cmd.read_reply(mut rd)!
return
}) or {
// println('with conn err $err')
return err
}
// TODO: retry := shouldRetry(err, atomic.LoadUint32(&retryTimeout) == 1)
return false
}
fn (mut c BaseClient) release_conn(ctx context.Context, mut cn pool.Conn, err IError) {
// println('release connection')
c.conn_pool.put(ctx, mut cn)
/*
if is_bad_conn(err, false, c.opt.addr) {
c.conn_pool.remove(ctx, cn, err)
} else {
c.conn_pool.put(ctx, cn)
}*/
}
fn (c BaseClient) init_conn(mut ctx context.Context, mut cn pool.Conn) ! {
if cn.inited {
return
}
cn.inited = true
username := c.opt.username
password := c.opt.password
// println('init_conn: new_signle_pool_conn')
mut conn_pool := pool.new_single_pool_conn(c.conn_pool, cn)
mut conn := new_conn(c.opt, conn_pool)
conn.hello(mut ctx, '3', username, password, '') or { return err }
if password != '' {
if username != '' {
conn.auth_acl(mut ctx, username, password)!
} else {
conn.auth(mut ctx, password)!
}
}
// println('init_conn: new_conn')
// TODO: pipeliner
}
/*
fn (mut c BaseClient) with_conn(ctx context.Context, f fn(context.Context, &pool.Conn) ?) {
cn := c.get_conn(ctx)?
mut res_err := IError(none)
defer {
c.release_conn(ctx, cn, res_err)
}
done := ctx.done()
}
*/
pub struct Conn {
BaseClient
Cmdble_
StatefulCmdble
}
fn new_conn(opt Options, conn_pool pool.Pooler) &Conn {
// println('redis_new_conn')
mut b := new_base_client(opt, conn_pool)
return &Conn{
conn_pool: conn_pool
BaseClient: b
Cmdble_: Cmdble_{
f: b.process
}
StatefulCmdble: StatefulCmdble{
f: b.process
}
}
}