Skip to content

Commit

Permalink
Since webmock_responses and webmock_request_signatures in HTTPCli…
Browse files Browse the repository at this point in the history
…ent adapter are now thread-local, there is no need to synchronize on mutex in order to access them.
  • Loading branch information
bblimke committed Feb 21, 2024
1 parent 3ed6b48 commit 9a985ad
Showing 1 changed file with 6 additions and 21 deletions.
27 changes: 6 additions & 21 deletions lib/webmock/http_lib_adapters/httpclient_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,14 @@ def do_get_stream(req, proxy, conn, &block)
end

def do_get(req, proxy, conn, stream = false, &block)

clear_thread_variables unless conn.async_thread

request_signature = build_request_signature(req, :reuse_existing)

WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)

if webmock_responses[request_signature]
webmock_response = synchronize_request_response { webmock_responses.delete(request_signature) }
webmock_response = webmock_responses.delete(request_signature)
response = build_httpclient_response(webmock_response, stream, req.header, &block)
@request_filter.each do |filter|
filter.filter_response(req, response)
Expand All @@ -78,7 +77,7 @@ def do_get(req, proxy, conn, stream = false, &block)
res
elsif WebMock.net_connect_allowed?(request_signature.uri)
# in case there is a nil entry in the hash...
synchronize_request_response { webmock_responses.delete(request_signature) }
webmock_responses.delete(request_signature)

res = if stream
do_get_stream_without_webmock(req, proxy, conn, &block)
Expand Down Expand Up @@ -111,7 +110,7 @@ def do_request_async(method, uri, query, body, extheader)
clear_thread_variables
req = create_request(method, uri, query, body, extheader)
request_signature = build_request_signature(req)
synchronize_request_response { webmock_request_signatures << request_signature }
webmock_request_signatures << request_signature

if webmock_responses[request_signature] || WebMock.net_connect_allowed?(request_signature.uri)
conn = super
Expand Down Expand Up @@ -198,9 +197,7 @@ def build_request_signature(req, reuse_existing = false)

def webmock_responses
Thread.current[WEBMOCK_HTTPCLIENT_RESPONSES] ||= Hash.new do |hash, request_signature|
synchronize_request_response do
hash[request_signature] = WebMock::StubRegistry.instance.response_for_request(request_signature)
end
hash[request_signature] = WebMock::StubRegistry.instance.response_for_request(request_signature)
end
end

Expand All @@ -209,10 +206,8 @@ def webmock_request_signatures
end

def previous_signature_for(signature)
synchronize_request_response do
return nil unless index = webmock_request_signatures.index(signature)
webmock_request_signatures.delete_at(index)
end
return nil unless index = webmock_request_signatures.index(signature)
webmock_request_signatures.delete_at(index)
end

private
Expand All @@ -228,16 +223,6 @@ def headers_from_session(uri)
end
end

def synchronize_request_response
if REQUEST_RESPONSE_LOCK.owned?
yield
else
REQUEST_RESPONSE_LOCK.synchronize do
yield
end
end
end

def clear_thread_variables
Thread.current[WEBMOCK_HTTPCLIENT_REQUEST_SIGNATURES] = nil
Thread.current[WEBMOCK_HTTPCLIENT_RESPONSES] = nil
Expand Down

0 comments on commit 9a985ad

Please sign in to comment.