Skip to content

Commit

Permalink
Use concurrent skiplist map for NettyRequest arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
justinlin-linkedin committed Oct 24, 2024
1 parent 639fa06 commit a5dba9c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -69,7 +69,10 @@ public class NettyRequest implements RestRequest {
protected final HttpRequest request;
protected final Channel channel;
protected final NettyMetrics nettyMetrics;
protected final Map<String, Object> allArgs = new ConcurrentHashMap<>();
// This allArgs map needs to be
// 1. case-insensitive, as headers and query parameters in http request should be case-insensitive
// 2. thread safe, as we might access/update the map in different threads.
protected final Map<String, Object> allArgs = new ConcurrentSkipListMap<>(String.CASE_INSENSITIVE_ORDER);
protected final Queue<HttpContent> requestContents = new LinkedBlockingQueue<>();
protected final ReentrantLock contentLock = new ReentrantLock();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,15 @@ private void validateRequest(NettyRequest nettyRequest, RestMethod restMethod, S

for (Map.Entry<String, String> e : headers) {
if (!e.getKey().equalsIgnoreCase(HttpHeaderNames.COOKIE.toString())) {
// Make sure we have this key in receivedArgs
assertTrue("Did not find key: " + e.getKey(), receivedArgs.containsKey(e.getKey()));
// Now make sure we can find a case-insensitive key in the request
String lowerKey = e.getKey().toLowerCase();
assertTrue("Case insensitive key should exist for lower key" + e.getKey(),
nettyRequest.getArgs().containsKey(lowerKey));
String upperKey = e.getKey().toUpperCase();
assertTrue("Case insensitive key should exist for upper key" + e.getKey(),
nettyRequest.getArgs().containsKey(upperKey));
if (!keyValueCount.containsKey(e.getKey())) {
keyValueCount.put(e.getKey(), 0);
}
Expand All @@ -769,8 +777,7 @@ private void validateRequest(NettyRequest nettyRequest, RestMethod restMethod, S
assertEquals("Number of args does not match", keyValueCount.size(), receivedArgs.size());
for (Map.Entry<String, Integer> e : keyValueCount.entrySet()) {
assertEquals("Value count for key " + e.getKey() + " does not match",
e.getValue() == 0 ? 1 : e.getValue().intValue(),
receivedArgs.get(e.getKey()).size());
e.getValue() == 0 ? 1 : e.getValue().intValue(), receivedArgs.get(e.getKey()).size());
}

assertEquals("Auto-read is in an invalid state",
Expand Down

0 comments on commit a5dba9c

Please sign in to comment.