Skip to content

Commit

Permalink
Use more specific thread key name for cache-id
Browse files Browse the repository at this point in the history
and following:

* Use `Thread#[]` style
    * because it is the common for Fluentd code
* Rename `connection_cache_id` to `connection_cache_next_id`
    * for clarity.

Signed-off-by: Daijiro Fukuda <fukuda@clear-code.com>
  • Loading branch information
daipom committed Apr 30, 2024
1 parent 6763f3d commit 9ce635c
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions lib/fluent/plugin/out_http.rb
Expand Up @@ -104,6 +104,18 @@ class RetryableResponse < StandardError; end
config_param :aws_role_arn, :string, default: nil
end

def connection_cache_id_thread_key
"#{plugin_id}_connection_cache_id"
end

def connection_cache_id_for_thread
Thread.current[connection_cache_id_thread_key]
end

def connection_cache_id_for_thread=(id)
Thread.current[connection_cache_id_thread_key] = id
end

def initialize
super

Expand All @@ -113,6 +125,7 @@ def initialize

@connection_cache = []
@connection_cache_id_mutex = Mutex.new
@connection_cache_next_id = 0
end

def close
Expand All @@ -125,7 +138,6 @@ def configure(conf)
super

@connection_cache = Array.new(actual_flush_thread_count, ConnectionCache.new("", nil)) if @reuse_connections
@connection_cache_id = 0

if @retryable_response_codes.nil?
log.warn('Status code 503 is going to be removed from default `retryable_response_codes` from fluentd v2. Please add it by yourself if you wish')
Expand Down Expand Up @@ -319,13 +331,13 @@ def create_request(chunk, uri)
end

def make_request_cached(uri, req)
id = Thread.current.thread_variable_get(plugin_id)
id = self.connection_cache_id_for_thread
if id.nil?
@connection_cache_id_mutex.synchronize {
id = @connection_cache_id
@connection_cache_id += 1
id = @connection_cache_next_id
@connection_cache_next_id += 1
}
Thread.current.thread_variable_set(plugin_id, id)
self.connection_cache_id_for_thread = id
end
uri_str = uri.to_s
if @connection_cache[id].uri != uri_str
Expand Down

0 comments on commit 9ce635c

Please sign in to comment.