From 1b94a89a86426490383467f2f0b4db297e27361d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Pervill=C3=A9?= Date: Tue, 21 Nov 2023 10:49:21 +0100 Subject: [PATCH] feat: add a configurable keep_alive_timeout for watches --- lib/kubeclient/common.rb | 8 +++++++- lib/kubeclient/watch_stream.rb | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/kubeclient/common.rb b/lib/kubeclient/common.rb index 4576a55b..304df81b 100644 --- a/lib/kubeclient/common.rb +++ b/lib/kubeclient/common.rb @@ -36,6 +36,8 @@ module ClientMixin DEFAULT_HTTP_PROXY_URI = nil DEFAULT_HTTP_MAX_REDIRECTS = 10 + DEFAULT_KEEP_ALIVE_TIMEOUT = 60 + SEARCH_ARGUMENTS = { 'labelSelector' => :label_selector, 'fieldSelector' => :field_selector, @@ -55,6 +57,7 @@ module ClientMixin attr_reader :auth_options attr_reader :http_proxy_uri attr_reader :http_max_redirects + attr_reader :keep_alive_timeout attr_reader :headers attr_reader :discovered @@ -68,6 +71,7 @@ def initialize_client( timeouts: DEFAULT_TIMEOUTS, http_proxy_uri: DEFAULT_HTTP_PROXY_URI, http_max_redirects: DEFAULT_HTTP_MAX_REDIRECTS, + keep_alive_timeout: DEFAULT_KEEP_ALIVE_TIMEOUT, as: :ros ) validate_auth_options(auth_options) @@ -85,6 +89,7 @@ def initialize_client( @timeouts = DEFAULT_TIMEOUTS.merge(timeouts) @http_proxy_uri = http_proxy_uri ? http_proxy_uri.to_s : nil @http_max_redirects = http_max_redirects + @keep_alive_timeout = keep_alive_timeout @as = as if auth_options[:bearer_token_file] @@ -645,7 +650,8 @@ def http_options(uri) basic_auth_password: @auth_options[:password], headers: get_headers, http_proxy_uri: @http_proxy_uri, - http_max_redirects: http_max_redirects + http_max_redirects: http_max_redirects, + keep_alive_timeout: keep_alive_timeout } options[:bearer_token_file] = @auth_options[:bearer_token_file] if @auth_options[:bearer_token_file] if uri.scheme == 'https' diff --git a/lib/kubeclient/watch_stream.rb b/lib/kubeclient/watch_stream.rb index cfa79140..f80046fc 100644 --- a/lib/kubeclient/watch_stream.rb +++ b/lib/kubeclient/watch_stream.rb @@ -9,6 +9,7 @@ def initialize(uri, http_options, formatter:) @http_client = nil @http_options = http_options @http_options[:http_max_redirects] ||= Kubeclient::Client::DEFAULT_HTTP_MAX_REDIRECTS + @http_options[:keep_alive_timeout] ||= Kubeclient::Client::DEFAULT_KEEP_ALIVE_TIMEOUT @formatter = formatter end @@ -63,6 +64,10 @@ def build_client ) end + if @http_options[:keep_alive_timeout].positive? + client = client.persistent(@uri.origin, timeout: @http_options[:keep_alive_timeout]) + end + client end