@@ -513,8 +513,9 @@ local function request_chunk(self, method, host, port, path, opts, timeout)
513513 return nil , " failed to watch data, response code: " .. res .status
514514 end
515515
516- return function ()
517- body , err = res .body_reader ()
516+ local function read_watch ()
517+ local body , err = res .body_reader ()
518+
518519 if not body then
519520 return nil , err
520521 end
@@ -541,6 +542,12 @@ local function request_chunk(self, method, host, port, path, opts, timeout)
541542
542543 return body
543544 end
545+
546+ if opts .need_cancel == true then
547+ return read_watch , nil , http_cli
548+ else
549+ return read_watch
550+ end
544551end
545552
546553
@@ -602,6 +609,11 @@ local function watch(self, key, attr)
602609 filters = attr .filters and attr .filters or 0
603610 end
604611
612+ local need_cancel
613+ if attr .need_cancel then
614+ need_cancel = attr .need_cancel and true or false
615+ end
616+
605617 local opts = {
606618 body = {
607619 create_request = {
@@ -614,19 +626,23 @@ local function watch(self, key, attr)
614626 fragment = fragment ,
615627 filters = filters ,
616628 }
617- }
629+ },
630+ need_cancel = need_cancel ,
618631 }
619632
620633 local endpoint = choose_endpoint (self )
621- local callback_fun , err = request_chunk (self , ' POST' ,
634+
635+ local callback_fun , err , http_cli = request_chunk (self , ' POST' ,
622636 endpoint .host ,
623637 endpoint .port ,
624638 endpoint .api_prefix .. ' /watch' , opts ,
625639 attr .timeout or self .timeout )
626640 if not callback_fun then
627641 return nil , err
628642 end
629-
643+ if opts .need_cancel == true then
644+ return callback_fun , nil , http_cli
645+ end
630646 return callback_fun
631647end
632648
@@ -658,10 +674,16 @@ function _M.watch(self, key, opts)
658674 attr .prev_kv = opts and opts .prev_kv
659675 attr .watch_id = opts and opts .watch_id
660676 attr .fragment = opts and opts .fragment
677+ attr .need_cancel = opts and opts .need_cancel
661678
662679 return watch (self , key , attr )
663680end
664681
682+ function _M .watchcancel (self , http_cli )
683+ local res , err = http_cli :close ()
684+ return res , err
685+ end
686+
665687function _M .readdir (self , key , opts )
666688
667689 clear_tab (attr )
@@ -693,6 +715,7 @@ function _M.watchdir(self, key, opts)
693715 attr .prev_kv = opts and opts .prev_kv
694716 attr .watch_id = opts and opts .watch_id
695717 attr .fragment = opts and opts .fragment
718+ attr .need_cancel = opts and opts .need_cancel
696719
697720 return watch (self , key , attr )
698721end
0 commit comments