Skip to content

Commit 3035455

Browse files
nic-chenmembphis
authored andcommitted
change: prefix issue (#44)
1 parent 7f95abf commit 3035455

File tree

7 files changed

+132
-35
lines changed

7 files changed

+132
-35
lines changed

api_v2.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Method
3131
- `http_host`: string - default `http://127.0.0.1:2379`
3232
- `ttl`: int - default `-1`
3333
default ttl for key operation. set -1 to disable ttl.
34-
- `prefix`: string
34+
- `key_prefix`: string
3535
append this prefix path string to key operation url `'/v2/keys'`.
3636
- `timeout`: int
3737
request timeout seconds.

api_v3.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ API V3
1414
* [watchdir](#watchdir)
1515
* [rmdir](#rmdir)
1616
* [txn](#txn)
17+
* [version](#version)
1718

1819
Method
1920
======
@@ -27,10 +28,13 @@ Method
2728
- `http_host`: string - default `http://127.0.0.1:2379`
2829
- `ttl`: int - default `-1`
2930
default ttl for key operation. set -1 to disable ttl.
30-
- `prefix`: string
31+
- `key_prefix`: string
3132
append this prefix path string to key operation url.
3233
- `timeout`: int
3334
default request timeout seconds.
35+
- `api_prefix`: string
36+
to suit [etcd v3 api gateway](https://github.com/etcd-io/etcd/blob/master/Documentation/dev-guide/api_grpc_gateway.md#notes).
37+
it will autofill by fetching etcd version if this option empty.
3438

3539
The client methods returns either a `etcd` object or an `error string`.
3640

lib/resty/etcd.lua

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local etcdv2 = require("resty.etcd.v2")
22
local etcdv3 = require("resty.etcd.v3")
3+
local utils = require("resty.etcd.utils")
34
local typeof = require("typeof")
45
local prefix_v3 = {
56
["3.5."] = "/v3",
@@ -33,36 +34,27 @@ function _M.new(opts)
3334
return nil, 'opts must be table'
3435
end
3536

36-
if opts.host and not typeof.string(opts.host) then
37-
return nil, 'opts.host must be string'
38-
end
39-
40-
if opts.port and not typeof.int(opts.port) then
41-
return nil, 'opts.port must be integer'
42-
end
43-
4437
opts.timeout = opts.timeout or 5 -- 5 sec
45-
opts.http_host = opts.http_host or "http://" .. (opts.host or "127.0.0.1")
46-
.. ":" .. (opts.port or 2379)
38+
opts.http_host = opts.http_host or "http://127.0.0.1:2379"
4739
opts.ttl = opts.ttl or -1
4840

4941
local protocol = opts and opts.protocol or "v2"
50-
if protocol == "v3" then
5142

52-
local etcd_prefix = opts.etcd_prefix
53-
-- if opts special the etcd_prefix,no need to check version
54-
if not etcd_prefix then
43+
if protocol == "v3" then
44+
-- if opts special the api_prefix,no need to check version
45+
if not opts.api_prefix or not utils.has_value(prefix_v3, opts.api_prefix) then
5546
local ver, err = etcd_version(opts)
5647
if not ver then
5748
return nil, err
5849
end
5950
local sub_ver = ver.etcdserver:sub(1, 4)
60-
etcd_prefix = prefix_v3[sub_ver] or "/v3beta"
51+
opts.api_prefix = prefix_v3[sub_ver] or "/v3beta"
6152
end
62-
opts.api_prefix = etcd_prefix .. (opts.api_prefix or "")
6353
return etcdv3.new(opts)
6454
end
6555

56+
opts.api_prefix = "/v2"
57+
6658
return etcdv2.new(opts)
6759
end
6860

lib/resty/etcd/utils.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ end
6363
_M.normalize = normalize
6464

6565

66+
function _M.get_real_key(prefix, key)
67+
return (type(prefix) == 'string' and prefix or "") .. key
68+
end
69+
70+
71+
function _M.has_value(arr, val)
72+
for index, value in ipairs(arr) do
73+
if value == val then
74+
return index
75+
end
76+
end
77+
78+
return false
79+
end
80+
6681
local ngx_log = ngx.log
6782
local ngx_ERR = ngx.ERR
6883
local ngx_INFO = ngx.INFO

lib/resty/etcd/v2.lua

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ end
4444
function _M.new(opts)
4545
local timeout = opts.timeout
4646
local ttl = opts.ttl
47-
local prefix = opts.prefix or "/v2/keys"
47+
local api_prefix = opts.api_prefix or ""
48+
local key_prefix = opts.key_prefix or ""
4849
local http_host = opts.http_host
4950
local user = opts.user
5051
local password = opts.password
@@ -54,15 +55,19 @@ function _M.new(opts)
5455
end
5556

5657
if not typeof.string(http_host) and not typeof.table(http_host) then
57-
return nil, 'opts.host must be string or string array'
58+
return nil, 'opts.http_host must be string or string array'
5859
end
5960

6061
if not typeof.int(ttl) then
6162
return nil, 'opts.ttl must be integer'
6263
end
6364

64-
if not typeof.string(prefix) then
65-
return nil, 'opts.prefix must be string'
65+
if not typeof.string(api_prefix) then
66+
return nil, 'opts.api_prefix must be string'
67+
end
68+
69+
if not typeof.string(key_prefix) then
70+
return nil, 'opts.key_prefix must be string'
6671
end
6772

6873
if user and not typeof.string(user) then
@@ -83,9 +88,9 @@ function _M.new(opts)
8388

8489
for _, host in ipairs(http_hosts) do
8590
table.insert(endpoints, {
86-
full_prefix = host .. utils.normalize(prefix),
91+
full_prefix = host .. utils.normalize(api_prefix),
8792
http_host = host,
88-
prefix = prefix,
93+
api_prefix = api_prefix,
8994
version = host .. '/version',
9095
stats_leader = host .. '/v2/stats/leader',
9196
stats_self = host .. '/v2/stats/self',
@@ -98,6 +103,7 @@ function _M.new(opts)
98103
init_count = 0,
99104
timeout = timeout,
100105
ttl = ttl,
106+
key_prefix = key_prefix,
101107
is_cluster = #endpoints > 1,
102108
user = user,
103109
password = password,
@@ -237,8 +243,9 @@ local function set(self, key, val, attr)
237243

238244
local res
239245
res, err = _request(self, attr.in_order and 'POST' or 'PUT',
240-
choose_endpoint(self).full_prefix .. key,
246+
choose_endpoint(self).full_prefix .. "/keys" .. key,
241247
opts, self.timeout)
248+
242249
if err then
243250
return nil, err
244251
end
@@ -306,7 +313,7 @@ local function get(self, key, attr)
306313
end
307314

308315
local res, err = _request(self, "GET",
309-
choose_endpoint(self).full_prefix .. utils.normalize(key),
316+
choose_endpoint(self).full_prefix .. "/keys" .. utils.normalize(key),
310317
opts, attr and attr.timeout or self.timeout)
311318
if err then
312319
return res, err
@@ -367,7 +374,7 @@ local function delete(self, key, attr)
367374

368375
-- todo: check arguments
369376
return _request(self, "DELETE",
370-
choose_endpoint(self).full_prefix .. utils.normalize(key),
377+
choose_endpoint(self).full_prefix .. "/keys" .. utils.normalize(key),
371378
opts, self.timeout)
372379
end
373380

@@ -378,6 +385,8 @@ function _M.get(self, key)
378385
return nil, 'key must be string'
379386
end
380387

388+
key = utils.get_real_key(self.key_prefix, key)
389+
381390
return get(self, key)
382391
end
383392

@@ -388,6 +397,8 @@ function _M.wait(self, key, modified_index, timeout)
388397
attr.wait_index = modified_index
389398
attr.timeout = timeout
390399

400+
key = utils.get_real_key(self.key_prefix, key)
401+
391402
return get(self, key, attr)
392403
end
393404

@@ -396,6 +407,8 @@ function _M.readdir(self, key, recursive)
396407
attr.dir = true
397408
attr.recursive = recursive
398409

410+
key = utils.get_real_key(self.key_prefix, key)
411+
399412
return get(self, key, attr)
400413
end
401414

@@ -408,6 +421,8 @@ function _M.waitdir(self, key, modified_index, timeout)
408421
attr.wait_index = modified_index
409422
attr.timeout = timeout
410423

424+
key = utils.get_real_key(self.key_prefix, key)
425+
411426
return get(self, key, attr)
412427
end
413428

@@ -442,6 +457,8 @@ function _M.set(self, key, val, ttl)
442457
clear_tab(attr)
443458
attr.ttl = ttl
444459

460+
key = utils.get_real_key(self.key_prefix, key)
461+
445462
return set(self, key, val, attr)
446463
end
447464

@@ -451,6 +468,8 @@ function _M.setnx(self, key, val, ttl)
451468
attr.ttl = ttl
452469
attr.prev_exist = false
453470

471+
key = utils.get_real_key(self.key_prefix, key)
472+
454473
return set(self, key, val, attr)
455474
end
456475

@@ -461,6 +480,8 @@ function _M.setx(self, key, val, ttl, modified_index)
461480
attr.prev_exist = true
462481
attr.prev_index = modified_index
463482

483+
key = utils.get_real_key(self.key_prefix, key)
484+
464485
return set(self, key, val, attr)
465486
end
466487

@@ -470,6 +491,8 @@ function _M.mkdir(self, key, ttl)
470491
attr.ttl = ttl
471492
attr.dir = true
472493

494+
key = utils.get_real_key(self.key_prefix, key)
495+
473496
return set(self, key, nil, attr)
474497
end
475498

@@ -480,6 +503,8 @@ function _M.mkdirnx(self, key, ttl)
480503
attr.dir = true
481504
attr.prev_exist = false
482505

506+
key = utils.get_real_key(self.key_prefix, key)
507+
483508
return set(self, key, nil, attr)
484509
end
485510

@@ -489,6 +514,8 @@ function _M.push(self, key, val, ttl)
489514
attr.ttl = ttl
490515
attr.in_order = true
491516

517+
key = utils.get_real_key(self.key_prefix, key)
518+
492519
return set(self, key, val, attr)
493520
end
494521

@@ -502,6 +529,8 @@ function _M.delete(self, key, prev_val, modified_index)
502529
attr.prev_value = prev_val
503530
attr.prev_index = modified_index
504531

532+
key = utils.get_real_key(self.key_prefix, key)
533+
505534
return delete(self, key, attr)
506535
end
507536

@@ -510,6 +539,8 @@ function _M.rmdir(self, key, recursive)
510539
attr.dir = true
511540
attr.recursive = recursive
512541

542+
key = utils.get_real_key(self.key_prefix, key)
543+
513544
return delete(self, key, attr)
514545
end
515546

0 commit comments

Comments
 (0)