-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.v
124 lines (113 loc) · 3.76 KB
/
options.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
module vredis
import context
import net
import time
import net.urllib
import pool
pub struct Options {
pub mut:
// addr - host:port address
addr string = 'localhost:6379'
username string
password string
db int = 1
dialer fn (context.Context, string) !&net.TcpConn
// max_retries - default 3; -1 disables retries.
max_retries int = 3
// Timeout for socket reads. If reached, commands will fail
// with a timeout instead of blocking. Use value -1 for no timeout and 0 for default.
// Default is 3 seconds.
read_timeout time.Duration = 3 * time.second
// Timeout for socket writes. If reached, commands will fail
// with a timeout instead of blocking.
// Default is ReadTimeout.
write_timeout time.Duration = 3 * time.second
// Amount of time after which client closes idle connections.
// Should be less than server's timeout.
// Default is 5 minutes. -1 disables idle timeout check.
idle_timeout time.Duration = 5 * time.minute
pool_fifo bool
pool_size int = 10
min_idle_conns int
max_conn_age time.Duration
pool_timeout time.Duration = time.second * 4
idle_check_freq time.Duration = time.minute
}
// ParseURL parses an URL into Options that can be used to connect to Redis.
// Scheme is required.
// There are two connection types: by tcp socket and by unix socket.
// Tcp connection:
// redis://<user>:<password>@<host>:<port>/<db_number>
// Most Option fields can be set using query parameters, with the following restrictions:
// - field names are mapped using snake-case conversion: to set MaxRetries, use max_retries
// - only scalar type fields are supported (bool, int, time.Duration)
// - for time.Duration fields, values must be a valid input for time.ParseDuration();
// additionally a plain integer as value (i.e. without unit) is intepreted as seconds
// - to disable a duration field, use value less than or equal to 0; to use the default
// value, leave the value blank or remove the parameter
// - only the last value is interpreted if a parameter is given multiple times
// - fields "network", "addr", "username" and "password" can only be set using other
// URL attributes (scheme, host, userinfo, resp.), query paremeters using these
// names will be treated as unknown parameters
// - unknown parameter names will result in an error
// Examples:
// redis://user:password@localhost:6789/3?db=1&read_timeout=6s&max_retries=2
// is equivalent to:
// Options{
// addr: "localhost:6789",
// db: 1, // path "/3" was overridden by "&db=1"
// read_timeout: 6 * time.Second,
// max_retries: 2,
// }
pub fn parse_url(redis_url string) !Options {
u := urllib.parse(redis_url)!
match u.scheme {
'redis', 'rediss' {
return setup_tcp_conn(u)
}
else {
panic(error('we didnt support UNIX socket'))
}
}
}
fn (mut o Options) init() {
o.dialer = fn (ctx context.Context, addr string) !&net.TcpConn {
// TODO: dial timeout
return net.dial_tcp(addr)
}
}
fn setup_tcp_conn(u urllib.URL) !Options {
mut o := Options{}
o.username, o.password = get_user_password(u)
o.addr = u.hostname()
p := u.port()
if p != '' {
o.addr += ':' + p
}
// TODO:
return o
}
fn get_user_password(u urllib.URL) (string, string) {
mut user, mut password := '', ''
if u.user.username != '' {
user = u.user.username
if u.user.password != '' {
password = u.user.password
}
}
return user, password
}
fn new_conn_pool(opt Options) &pool.ConnPool {
return pool.new_conn_pool(pool.Options{
dialer: fn [opt] (ctx context.Context) !&net.TcpConn {
return opt.dialer(ctx, opt.addr)
}
pool_fifo: opt.pool_fifo
pool_size: opt.pool_size
min_idle_conns: opt.min_idle_conns
max_conn_age: opt.max_conn_age
pool_timeout: opt.pool_timeout
idle_timeout: opt.idle_timeout
idle_check_freq: opt.idle_check_freq
})
}