Skip to content

Commit

Permalink
Feat: watches use a persistent http connection to prevent zombification.
Browse files Browse the repository at this point in the history
To configure the keep alive timeout, instanciate KubeClient::Client with
the `:keep_alive_timeout` keyword argument, defaulting to 60 (seconds).

Fixes issue ManageIQ#512.
  • Loading branch information
jperville committed Nov 21, 2023
1 parent 054bff2 commit a0f6aec
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/kubeclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class Client
DEFAULT_HTTP_PROXY_URI = nil
DEFAULT_HTTP_MAX_REDIRECTS = 10

DEFAULT_KEEP_ALIVE_TIMEOUT = 60

SEARCH_ARGUMENTS = {
'labelSelector' => :label_selector,
'fieldSelector' => :field_selector,
Expand Down Expand Up @@ -88,6 +90,7 @@ class Client
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

Expand Down Expand Up @@ -117,6 +120,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)
Expand All @@ -134,6 +138,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

validate_bearer_token_file
Expand Down Expand Up @@ -789,6 +794,7 @@ def http_options(uri)
headers: @headers,
http_proxy_uri: @http_proxy_uri,
http_max_redirects: http_max_redirects,
keep_alive_timeout: keep_alive_timeout,
bearer_token_file: @auth_options[:bearer_token_file],
bearer_token: @auth_options[:bearer_token]
}
Expand Down
5 changes: 5 additions & 0 deletions lib/kubeclient/watch_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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

Expand Down Expand Up @@ -65,6 +66,10 @@ def build_client
)
end

if @http_options[:keep_alive_timeout].positive?
client = client.persistent(@uri.origin, timeout: @http_options[:keep_alive_timeout])
end

bearer_token = nil
if @http_options[:bearer_token_file]
bearer_token_file = @http_options[:bearer_token_file]
Expand Down

0 comments on commit a0f6aec

Please sign in to comment.