From 9a985adb14c1c5247cb3dcf17a888532ee3acb4b Mon Sep 17 00:00:00 2001 From: Bartosz Blimke Date: Wed, 21 Feb 2024 04:25:53 +0100 Subject: [PATCH] Since `webmock_responses` and `webmock_request_signatures` in HTTPClient adapter are now thread-local, there is no need to synchronize on mutex in order to access them. --- .../http_lib_adapters/httpclient_adapter.rb | 27 +++++-------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/lib/webmock/http_lib_adapters/httpclient_adapter.rb b/lib/webmock/http_lib_adapters/httpclient_adapter.rb index 26064991..809698ed 100644 --- a/lib/webmock/http_lib_adapters/httpclient_adapter.rb +++ b/lib/webmock/http_lib_adapters/httpclient_adapter.rb @@ -59,7 +59,6 @@ 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) @@ -67,7 +66,7 @@ def do_get(req, proxy, conn, stream = false, &block) 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) @@ -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) @@ -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 @@ -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 @@ -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 @@ -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