You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stormcrawler.apache.org by rz...@apache.org on 2026/04/01 17:33:24 UTC

(stormcrawler) 01/01: Fix thread-safety and minor issues in OkHttp protocol

This is an automated email from the ASF dual-hosted git repository.

rzo1 pushed a commit to branch okhttp-fixes
in repository https://gitbox.apache.org/repos/asf/stormcrawler.git

commit 72b1b1f1d63a083e843fd098b68e8947375653f5
Author: Richard Zowalla <rz...@apache.org>
AuthorDate: Wed Apr 1 19:33:14 2026 +0200

    Fix thread-safety and minor issues in OkHttp protocol
    
      - Replace HashMap with ConcurrentHashMap for DNS timing map to prevent
        data races between event listener callbacks and fetch threads
      - Remove unnecessary (int) cast on long value to prevent potential
        truncation in content buffering loop
      - Use isEmpty() instead of size() > 0
---
 .../org/apache/stormcrawler/protocol/okhttp/HttpProtocol.java     | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/core/src/main/java/org/apache/stormcrawler/protocol/okhttp/HttpProtocol.java b/core/src/main/java/org/apache/stormcrawler/protocol/okhttp/HttpProtocol.java
index 8defb8f0..1a939c9c 100644
--- a/core/src/main/java/org/apache/stormcrawler/protocol/okhttp/HttpProtocol.java
+++ b/core/src/main/java/org/apache/stormcrawler/protocol/okhttp/HttpProtocol.java
@@ -25,13 +25,13 @@ import java.nio.charset.StandardCharsets;
 import java.security.cert.CertificateException;
 import java.util.ArrayList;
 import java.util.Base64;
-import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
 import javax.net.ssl.HostnameVerifier;
 import javax.net.ssl.SSLContext;
@@ -98,7 +98,7 @@ public class HttpProtocol extends AbstractHttpProtocol {
     private final List<KeyValue> customRequestHeaders = new LinkedList<>();
 
     // track the time spent for each URL in DNS resolution
-    private final Map<String, Long> DNStimes = new HashMap<>();
+    private final Map<String, Long> DNStimes = new ConcurrentHashMap<>();
 
     private OkHttpClient.Builder builder;
 
@@ -186,7 +186,7 @@ public class HttpProtocol extends AbstractHttpProtocol {
                     break;
             }
         }
-        if (protocols.size() > 0) {
+        if (!protocols.isEmpty()) {
             LOG.info("Using protocol versions: {}", protocols);
             builder.protocols(protocols);
         }
@@ -512,7 +512,7 @@ public class HttpProtocol extends AbstractHttpProtocol {
 
             // okhttp may fetch more content than requested, quickly "increment"
             // bytes
-            bytesRequested = (int) source.getBuffer().size();
+            bytesRequested = source.getBuffer().size();
         }
         int bytesToCopy = (int) source.getBuffer().size(); // bytesBuffered
         if (maxContent != -1 && bytesToCopy > maxContent) {