-
Notifications
You must be signed in to change notification settings - Fork 54
/
v3.lua
1715 lines (1387 loc) · 44.7 KB
/
v3.lua
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- https://github.com/ledgetech/lua-resty-http
local require = require
local split = require("ngx.re").split
local typeof = require("typeof")
local cjson = require("cjson.safe")
local setmetatable = setmetatable
local random = math.random
local clear_tab = require("table.clear")
local utils = require("resty.etcd.utils")
local tab_nkeys = require("table.nkeys")
local encode_args = ngx.encode_args
local now = ngx.now
local sub_str = string.sub
local str_byte = string.byte
local str_char = string.char
local str_find = string.find
local ipairs = ipairs
local pairs = pairs
local pcall = pcall
local unpack = unpack
local re_match = ngx.re.match
local type = type
local tab_insert = table.insert
local str_lower = string.lower
local tab_clone = require("table.clone")
local decode_json = cjson.decode
local encode_json = cjson.encode
local encode_base64 = ngx.encode_base64
local decode_base64 = ngx.decode_base64
local semaphore = require("ngx.semaphore")
local health_check = require("resty.etcd.health_check")
local pl_path = require("pl.path")
local grpc_proto = require("resty.etcd.proto")
math.randomseed(now() * 1000 + ngx.worker.pid())
local INIT_COUNT_RESIZE = 2e8
local _M = {}
local _http_M = {}
local _grpc_M = {}
local http_mt = { __index = _http_M }
local grpc_mt = { __index = _grpc_M }
local unmodifiable_headers = {
["authorization"] = true,
["content-length"] = true,
["transfer-encoding"] = true,
["connection"] = true,
["upgrade"] = true,
}
-- define local refresh function variable
local refresh_jwt_token
local function ring_balancer(self)
local endpoints = self.endpoints
local endpoints_len = #endpoints
self.init_count = self.init_count + 1
local pos = self.init_count % endpoints_len + 1
if self.init_count >= INIT_COUNT_RESIZE then
self.init_count = 0
end
return endpoints[pos]
end
local function choose_endpoint(self)
for _, _ in ipairs(self.endpoints) do
local endpoint = ring_balancer(self)
if health_check.get_target_status(endpoint.http_host) then
utils.log_info("choose endpoint: ", endpoint.http_host)
return endpoint
end
end
return nil, "has no healthy etcd endpoint available"
end
local function request_uri_via_unix_socket(self, uri, params)
local parsed_uri, err = self:parse_uri(uri, false)
if not parsed_uri then
return nil, err
end
local path, query
params.scheme, params.host, params.port, path, query = unpack(parsed_uri)
local use_unix_socket = false
if params.unix_socket_proxy then
local sock_path = params.unix_socket_proxy:sub(#"unix:" + 1)
use_unix_socket = pl_path.exists(sock_path)
end
if use_unix_socket then
if not params.headers then
params.headers = {}
end
params.headers["Host"] = params.host
params.host = params.unix_socket_proxy
params.port = nil
end
params.path = params.path or path
params.query = params.query or query
params.ssl_server_name = params.ssl_server_name or params.host
local res
res, err = self:connect(params)
if not res then
return nil, err
end
res, err = self:request(params)
if not res then
self:close()
return nil, err
end
local body
body, err = res:read_body()
if not body then
self:close()
return nil, err
end
res.body = body
if params.keepalive == false then
self:close()
else
self:set_keepalive(params.keepalive_timeout, params.keepalive_pool)
end
return res, nil
end
local function http_request_uri(self, http_cli, method, uri, body, headers, keepalive)
local endpoint, err = choose_endpoint(self)
if not endpoint then
return nil, err
end
local full_uri
if utils.starts_with(uri, "/version") then
full_uri = endpoint.http_host .. uri
else
full_uri = endpoint.full_prefix .. uri
end
local res
res, err = request_uri_via_unix_socket(http_cli, full_uri, {
method = method,
body = body,
headers = headers,
keepalive = keepalive,
ssl_verify = self.ssl_verify,
ssl_cert_path = self.ssl_cert_path,
ssl_key_path = self.ssl_key_path,
ssl_cert = self.ssl_cert,
ssl_key = self.ssl_key,
ssl_server_name = self.sni,
unix_socket_proxy = self.unix_socket_proxy,
})
if err then
health_check.report_failure(endpoint.http_host)
return nil, endpoint.http_host .. ": " .. err
end
if res.status >= 500 then
health_check.report_failure(endpoint.http_host)
return nil, "invalid response code: " .. res.status
end
return res
end
local function _request_uri(self, method, uri, opts, timeout, ignore_auth)
utils.log_info("v3 request uri: ", uri, ", timeout: ", timeout)
local body
if opts and opts.body and tab_nkeys(opts.body) > 0 then
body = encode_json(opts.body) --encode_args(opts.body)
end
if opts and opts.query and tab_nkeys(opts.query) > 0 then
uri = uri .. '?' .. encode_args(opts.query)
end
local headers = {}
local keepalive = true
if self.is_auth then
if not ignore_auth then
-- authentication reqeust not need auth request
local _, err = refresh_jwt_token(self, timeout)
if err then
return nil, err
end
headers.Authorization = self.jwt_token
else
keepalive = false -- jwt_token not keepalive
end
end
if self.extra_headers and type(self.extra_headers) == "table" then
for key, value in pairs(self.extra_headers) do
if not unmodifiable_headers[str_lower(key)] then
headers[key] = value
end
end
utils.log_info("request uri headers: ", encode_json(headers))
end
local http_cli, err = utils.http.new()
if err then
return nil, err
end
if timeout then
http_cli:set_timeout(timeout * 1000)
end
local res
if health_check.conf.retry then
local max_retry = #self.endpoints * health_check.conf.max_fails + 1
for i = 1, max_retry do
res, err = http_request_uri(self, http_cli, method, uri, body, headers, keepalive)
if res then
break
end
if err == "has no healthy etcd endpoint available" then
return nil, err
end
if i < max_retry then
utils.log_warn(err .. ". Retrying")
end
end
else
res, err = http_request_uri(self, http_cli, method, uri, body, headers, keepalive)
end
if err then
return nil, err
end
if not typeof.string(res.body) then
return res
end
res.body = decode_json(res.body)
return res
end
local function serialize_grpc_value(serialize_fn, val)
local err
val, err = serialize_fn(val)
if not val then
return nil, err
end
return val
end
local function serialize_and_encode_base64(serialize_fn, data)
local err
data, err = serialize_fn(data)
if not data then
return nil, err
end
return encode_base64(data)
end
local function choose_grpc_endpoint(self)
local connect_opts = {
max_recv_msg_size = 2147483647,
}
local endpoint, err = choose_endpoint(self)
if not endpoint then
return nil, err, nil
end
if endpoint.scheme == "https" then
connect_opts.insecure = false
end
connect_opts.tls_verify = self.ssl_verify
connect_opts.client_cert = self.ssl_cert_path
connect_opts.client_key = self.ssl_key_path
connect_opts.trusted_ca = self.trusted_ca
local target
if self.unix_socket_proxy then
target = self.unix_socket_proxy
else
target = endpoint.address .. ":" .. endpoint.port
end
utils.log_info("etcd grpc connect to ", target)
local conn, err = self.grpc.connect(target, connect_opts)
if not conn then
return nil, err, endpoint.http_host
end
-- we disable health check when proxying via unix socket,
-- so the http_host will always point to a real address when the failure is reported
conn.http_host = endpoint.http_host
return conn
end
function _M.new(opts)
local timeout = opts.timeout
local ttl = opts.ttl
local api_prefix = opts.api_prefix
local key_prefix = opts.key_prefix or ""
local http_host = opts.http_host
local user = opts.user
local password = opts.password
local ssl_verify = opts.ssl_verify
if ssl_verify == nil then
ssl_verify = true
end
local serializer = opts.serializer
local extra_headers = opts.extra_headers
local sni = opts.sni
local unix_socket_proxy = opts.unix_socket_proxy
local init_count = opts.init_count
if not typeof.uint(timeout) then
return nil, 'opts.timeout must be unsigned integer'
end
if not typeof.string(http_host) and not typeof.table(http_host) then
return nil, 'opts.http_host must be string or string array'
end
if not typeof.int(ttl) then
return nil, 'opts.ttl must be integer'
end
if not typeof.string(api_prefix) then
return nil, 'opts.api_prefix must be string'
end
if not typeof.string(key_prefix) then
return nil, 'opts.key_prefix must be string'
end
if user and not typeof.string(user) then
return nil, 'opts.user must be string or ignore'
end
if password and not typeof.string(password) then
return nil, 'opts.password must be string or ignore'
end
if unix_socket_proxy and not typeof.string(unix_socket_proxy) then
return nil, 'opts.unix_socket_proxy must be string or ignore'
end
if not typeof.number(init_count) then
init_count = random(100)
end
local endpoints = {}
local http_hosts
if type(http_host) == 'string' then -- signle node
http_hosts = {http_host}
else
http_hosts = http_host
end
for _, host in ipairs(http_hosts) do
local m, err = re_match(host,
[=[([^\/]+):\/\/([\da-zA-Z.-]+|\[[\da-fA-F:]+\]):?(\d+)?$]=], "jo")
if not m then
return nil, "invalid http host: " .. host .. ", err: " .. (err or "not matched")
end
local addr
if unix_socket_proxy then
addr = unix_socket_proxy
else
addr = m[2] or "127.0.0.1"
end
local port
if not unix_socket_proxy then
port = m[3] or "2379"
end
tab_insert(endpoints, {
full_prefix = host .. utils.normalize(api_prefix),
http_host = host,
parsed_uri = m,
scheme = m[1],
host = m[2] or "127.0.0.1",
address = addr,
port = port,
api_prefix = api_prefix,
})
end
if health_check.conf == nil then
health_check.init()
end
local cli = {
last_auth_time = now(), -- save last Authentication time
last_refresh_jwt_err = nil,
jwt_token = nil, -- last Authentication token
grpc_token = nil, -- token used in gRPC
is_auth = not not (user and password),
user = user,
password = password,
timeout = timeout,
ttl = ttl,
is_cluster = #endpoints > 1,
endpoints = endpoints,
key_prefix = key_prefix,
ssl_verify = ssl_verify,
serializer = serializer,
ssl_cert_path = opts.ssl_cert_path,
ssl_key_path = opts.ssl_key_path,
ssl_cert = opts.ssl_cert,
ssl_key = opts.ssl_key,
trusted_ca = opts.trusted_ca,
extra_headers = extra_headers,
sni = sni,
unix_socket_proxy = unix_socket_proxy,
init_count = init_count,
}
if opts.use_grpc then
local _, grpc = pcall(require, "resty.grpc")
if type(grpc) ~= "table" then
-- The gRPC module is not available. In this case, the "grpc" is the error message
return nil, grpc
end
local ok, err = grpc.load(grpc_proto, grpc.PROTO_TYPE_STR)
if not ok then
return nil, err
end
cli.use_grpc = true
cli.grpc = grpc
cli.call_opts = {}
local conn, err = choose_grpc_endpoint(cli)
if not conn then
return nil, err
end
cli.conn = conn
return setmetatable(cli, grpc_mt)
end
local sema, err = semaphore.new()
if not sema then
return nil, err
end
cli.use_grpc = false
cli.sema = sema
return setmetatable(cli, http_mt)
end
function _grpc_M.close(self)
self.conn:close()
self.conn = nil
end
local function wake_up_everyone(self)
local count = -self.sema:count()
if count > 0 then
self.sema:post(count)
end
end
-- return refresh_is_ok, error
function refresh_jwt_token(self, timeout)
-- token exist and not expire
-- default is 5min, we use 3min plus random seconds to smooth the refresh across workers
-- https://github.com/etcd-io/etcd/issues/8287
if self.jwt_token and now() - self.last_auth_time < 60 * 3 + random(0, 60) then
return true, nil
end
if self.requesting_token then
local wait_timeout = timeout or self.timeout or 0
self.sema:wait(wait_timeout)
if self.jwt_token and now() - self.last_auth_time < 60 * 3 + random(0, 60) then
return true, nil
end
if self.last_refresh_jwt_err then
utils.log_info("v3 refresh jwt last err: ", self.last_refresh_jwt_err)
return nil, self.last_refresh_jwt_err
end
-- something unexpected happened, try again
utils.log_info("v3 try auth after waiting, timeout: ", timeout)
end
self.last_refresh_jwt_err = nil
self.requesting_token = true
local opts = {
body = {
name = self.user,
password = self.password,
}
}
local res, err
res, err = _request_uri(self, 'POST', "/auth/authenticate", opts, timeout, true)
self.requesting_token = false
if err then
self.last_refresh_jwt_err = err
wake_up_everyone(self)
return nil, err
end
if not res or not res.body or not res.body.token then
err = 'authenticate refresh token fail'
self.last_refresh_jwt_err = err
wake_up_everyone(self)
return nil, err
end
self.jwt_token = res.body.token
self.last_auth_time = now()
wake_up_everyone(self)
return true, nil
end
local function http_set(self, key, val, attr)
-- verify key
local _, err = utils.verify_key(key)
if err then
return nil, err
end
key = encode_base64(key)
val, err = serialize_and_encode_base64(self.serializer.serialize, val)
if not val then
return nil, err
end
attr = attr or {}
local lease
if attr.lease then
lease = attr.lease and attr.lease or 0
end
local prev_kv
if attr.prev_kv then
prev_kv = attr.prev_kv and true or false
end
local ignore_value
if attr.ignore_value then
ignore_value = attr.ignore_value and true or false
end
local ignore_lease
if attr.ignore_lease then
ignore_lease = attr.ignore_lease and true or false
end
local opts = {
body = {
value = val,
key = key,
lease = lease,
prev_kv = prev_kv,
ignore_value = ignore_value,
ignore_lease = ignore_lease,
}
}
local res
res, err = _request_uri(self, 'POST', "/kv/put", opts, self.timeout)
if err then
return nil, err
end
-- get
if res.status < 300 then
-- TODO(optimize): delay json encode
utils.log_info("v3 set body: ", encode_json(res.body))
end
return res
end
local function http_get(self, key, attr)
-- verify key
local _, err = utils.verify_key(key)
if err then
return nil, err
end
attr = attr or {}
local range_end
if attr.range_end then
range_end = encode_base64(attr.range_end)
end
local limit = attr.limit or 0
local revision = attr.revision or 0
local sort_order = attr.sort_order or 0
local sort_target = attr.sort_target or 0
local serializable = attr.serializable or false
local keys_only = attr.keys_only or false
local count_only = attr.count_only or false
local min_mod_revision = attr.min_mod_revision or 0
local max_mod_revision = attr.max_mod_revision or 0
local min_create_revision = attr.min_create_revision or 0
local max_create_revision = attr.max_create_revision or 0
key = encode_base64(key)
local opts = {
body = {
key = key,
range_end = range_end,
limit = limit,
revision = revision,
sort_order = sort_order,
sort_target = sort_target,
serializable = serializable,
keys_only = keys_only,
count_only = count_only,
min_mod_revision = min_mod_revision,
max_mod_revision = max_mod_revision,
min_create_revision = min_create_revision,
max_create_revision = max_create_revision
}
}
local res
res, err = _request_uri(self, "POST", "/kv/range", opts, attr and attr.timeout or self.timeout)
if res and res.status == 200 then
if res.body.kvs and tab_nkeys(res.body.kvs) > 0 then
for _, kv in ipairs(res.body.kvs) do
kv.key = decode_base64(kv.key)
kv.value = decode_base64(kv.value or "")
kv.value = self.serializer.deserialize(kv.value)
end
end
end
return res, err
end
local function http_delete(self, key, attr)
attr = attr and attr or {}
local range_end
if attr.range_end then
range_end = encode_base64(attr.range_end)
end
local prev_kv
if attr.prev_kv then
prev_kv = attr.prev_kv and true or false
end
key = encode_base64(key)
local opts = {
body = {
key = key,
range_end = range_end,
prev_kv = prev_kv,
},
}
return _request_uri(self, "POST", "/kv/deleterange", opts, self.timeout)
end
local function txn(self, opts_arg, compare, success, failure)
if #compare < 1 then
return nil, "compare couldn't be empty"
end
if (success == nil or #success < 1) and (failure == nil or #failure < 1) then
return nil, "success and failure couldn't be empty at the same time"
end
local body = {
compare = compare,
success = success,
failure = failure,
}
if self.use_grpc then
return self:grpc_call("etcdserverpb.KV", "Txn", body, nil, nil, opts_arg)
end
local timeout = opts_arg and opts_arg.timeout
local opts = {
body = body,
}
return _request_uri(self, "POST", "/kv/txn", opts, timeout or self.timeout)
end
local function http_request_chunk(self, http_cli)
local endpoint, err = choose_endpoint(self)
if not endpoint then
return nil, err
end
if not endpoint.port then
local sock_path = endpoint.address:sub(#"unix:" + 1)
if not pl_path.exists(sock_path) then
endpoint.address = endpoint.parsed_uri[2]
endpoint.port = endpoint.parsed_uri[3]
end
end
local ok
ok, err = http_cli:connect({
scheme = endpoint.scheme,
host = endpoint.address,
port = endpoint.port,
ssl_verify = self.ssl_verify,
ssl_cert_path = self.ssl_cert_path,
ssl_key_path = self.ssl_key_path,
ssl_cert = self.ssl_cert,
ssl_key = self.ssl_key,
ssl_server_name = self.sni,
})
if not ok then
health_check.report_failure(endpoint.http_host)
return nil, endpoint.http_host .. ": " .. err
end
return endpoint, err
end
local function request_chunk(self, method, path, opts, timeout)
local body, err, _
if opts and opts.body and tab_nkeys(opts.body) > 0 then
body, err = encode_json(opts.body)
if not body then
return nil, err
end
end
local query
if opts and opts.query and tab_nkeys(opts.query) > 0 then
query = encode_args(opts.query)
end
local headers = {}
if self.is_auth then
-- authentication reqeust not need auth request
_, err = refresh_jwt_token(self, timeout)
if err then
return nil, err
end
headers.Authorization = self.jwt_token
end
if self.extra_headers and type(self.extra_headers) == "table" then
for key, value in pairs(self.extra_headers) do
if not unmodifiable_headers[str_lower(key)] then
headers[key] = value
end
end
utils.log_info("request chunk headers: ", encode_json(headers))
end
local http_cli
http_cli, err = utils.http.new()
if err then
return nil, err
end
if timeout then
_, err = http_cli:set_timeout(timeout * 1000)
if err then
return nil, err
end
end
local endpoint
if health_check.conf.retry then
local max_retry = #self.endpoints * health_check.conf.max_fails + 1
for i = 1, max_retry do
endpoint, err = http_request_chunk(self, http_cli)
if endpoint then
break
end
if err == "has no healthy etcd endpoint available" then
return nil, err
end
if i < max_retry then
utils.log_warn(err .. ". Retrying")
end
end
else
endpoint, err = http_request_chunk(self, http_cli)
end
if err then
return nil, err
end
if self.unix_socket_proxy then
headers["Host"] = endpoint.host
end
local res
res, err = http_cli:request({
method = method,
path = endpoint.api_prefix .. path,
body = body,
query = query,
headers = headers,
})
utils.log_info("http request method: ", method, " path: ", path,
" body: ", body, " query: ", query)
if not res then
http_cli:close()
return nil, err
end
if res.status >= 300 then
http_cli:close()
return nil, "failed to watch data, response code: " .. res.status
end
local function read_watch()
body = nil
while(1) do
local chunk, read_err = res.body_reader()
if read_err then
return nil, read_err
end
if not chunk then
break
end
if not body then
body = chunk
else
-- this branch will only be executed in rare cases, for example, a single event json
-- is larger than the proxy_buffer_size of nginx which proxies etcd, so it would be
-- ok to use a string concat directly without worry about the performance.
body = body .. chunk
end
if #chunk > 0 and str_byte(chunk, -1) == str_byte("\n") then
break
end
end
if not body then
return nil, nil
end
local chunks, split_err = split(body, [[\n]], "jo")
if split_err then
return nil, "failed to split chunks: " .. split_err
end
local all_events = {}
for _, chunk in ipairs(chunks) do
body, err = decode_json(chunk)
if not body then
return nil, "failed to decode json body: " .. (err or " unknown")
elseif body.error and body.error.http_code >= 500 then
-- health_check retry should do nothing here
-- and let connection closed to create a new one
health_check.report_failure(endpoint.http_host)
return nil, endpoint.http_host .. ": " .. body.error.http_status
end
if body.result and body.result.events then
for _, event in ipairs(body.result.events) do
if event.kv.value then -- DELETE not have value
event.kv.value = decode_base64(event.kv.value or "")
event.kv.value = self.serializer.deserialize(event.kv.value)
end
event.kv.key = decode_base64(event.kv.key)
if event.prev_kv then
event.prev_kv.value = decode_base64(event.prev_kv.value or "")
event.prev_kv.value = self.serializer.deserialize(event.prev_kv.value)
event.prev_kv.key = decode_base64(event.prev_kv.key)
end
tab_insert(all_events, event)
end
else
if #chunks == 1 then
return body
end
end
end
if #all_events > 1 then
body.result.events = all_events
end
return body
end
if opts.need_cancel == true then
return read_watch, nil, http_cli
else
return read_watch
end
end
local function get_range_end(key)
if #key == 0 then
return str_char(0)
end
local last = sub_str(key, -1)
key = sub_str(key, 1, #key-1)
local ascii = str_byte(last) + 1
local str = str_char(ascii)
return key .. str
end
local function create_watch_request(key, attr)
-- verify key
if #key == 0 then
key = str_char(0)
end
local range_end
if attr.range_end then
range_end = attr.range_end
end
local prev_kv
if attr.prev_kv then
prev_kv = attr.prev_kv and true or false
end
local start_revision
if attr.start_revision then
start_revision = attr.start_revision and attr.start_revision or 0
end
local watch_id
if attr.watch_id then
watch_id = attr.watch_id and attr.watch_id or 0
end
local progress_notify
if attr.progress_notify then
progress_notify = attr.progress_notify and true or false
end