-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptions.v
More file actions
120 lines (102 loc) · 2.79 KB
/
options.v
File metadata and controls
120 lines (102 loc) · 2.79 KB
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
module redict
import net
import net.urllib
import runtime
pub const protocol_flag_string = 'redict://'
pub const default_host_string = 'localhost'
pub const default_port_string = '6379'
pub struct Options {
pub:
// [redict://]<user>:<pass>@<host>:<port>[/<db>]
// Defaults to @localhost:6379/0
url string
// pool_size defaults to 10 connections per CPU thread.
// it represents the maximum number of connections in the pool.
pool_size int
// mind_idle_connections defaults to 0.
min_idle_connections int
// max_idle_connections defaults to 0, idle connections will not be closed.
max_idle_connections int
// max_retries is the maximum number of retries before giving up.
// Defaults to 3, 0 is set to 3. -1 disables retries.
max_retries int
}
struct ParsedOptions {
address string
username string
password string
db int
dialer fn (addr string) !&net.TcpConn @[required]
url string
pool_size int
min_idle_connections int
max_idle_connections int
max_retries int
}
fn parse_url(s string) !urllib.URL {
if s.starts_with(protocol_flag_string) {
return urllib.parse(s)!
}
return urllib.parse('${protocol_flag_string}${s}')!
}
fn get_address(s string) string {
if s == '' {
return '${default_host_string}:${default_port_string}'
}
if s.contains(':') {
r := s.split(':')
if r[0] == '' { // if no host, use default
return '${default_host_string}:${r[1]}'
}
if r[1] == '' { // if no port, use default
return '${r[0]}:${default_port_string}'
}
return s
}
// assume no port
return '${s}:${default_port_string}'
}
fn (opts Options) init() !ParsedOptions {
url := parse_url(opts.url)!
address := get_address(url.host)
mut username := ''
mut password := ''
if user := url.user {
username = user.username
password = user.password
}
db := url.path.trim('/').int()
mut pool_size := 10 * runtime.nr_cpus()
if opts.pool_size != 0 {
pool_size = opts.pool_size
}
mut max_retries := 3
if opts.max_retries != 0 {
max_retries = opts.max_retries
}
return ParsedOptions{
address: address
username: username
password: password
db: db
dialer: new_default_dialer()
pool_size: pool_size
min_idle_connections: opts.min_idle_connections
max_idle_connections: opts.max_idle_connections
max_retries: max_retries
}
}
fn new_default_dialer() fn (string) !&net.TcpConn {
return fn (s string) !&net.TcpConn {
return net.dial_tcp(s)!
}
}
fn private_new_connection_pool(opts ParsedOptions) &ConnectionPool {
pool_opts := &PoolOptions{
dialer: fn [opts] () !&net.TcpConn {
return opts.dialer(opts.address)
}
pool_size: opts.pool_size
}
return new_connection_pool(pool_opts)
}