Skip to content

Commit ae09aa4

Browse files
Added post-processing limit
1 parent 94715ff commit ae09aa4

File tree

6 files changed

+18
-10
lines changed

6 files changed

+18
-10
lines changed

dd-java-agent/agent-iast/src/main/java/com/datadog/iast/overhead/OverheadContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import static datadog.trace.api.iast.IastDetectionMode.UNLIMITED;
44

5-
import com.datadog.iast.util.NonBlockingSemaphore;
5+
import datadog.trace.util.NonBlockingSemaphore;
66

77
public class OverheadContext {
88

dd-java-agent/agent-iast/src/main/java/com/datadog/iast/overhead/OverheadController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import com.datadog.iast.IastRequestContext;
66
import com.datadog.iast.IastSystem;
7-
import com.datadog.iast.util.NonBlockingSemaphore;
87
import datadog.trace.api.Config;
98
import datadog.trace.api.gateway.RequestContext;
109
import datadog.trace.api.gateway.RequestContextSlot;
@@ -13,6 +12,7 @@
1312
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
1413
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
1514
import datadog.trace.util.AgentTaskScheduler;
15+
import datadog.trace.util.NonBlockingSemaphore;
1616
import java.util.concurrent.TimeUnit;
1717
import java.util.concurrent.atomic.AtomicLong;
1818
import javax.annotation.Nullable;

dd-java-agent/appsec/src/main/java/com/datadog/appsec/api/security/ApiAccessTracker.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ public ApiAccessTracker(int capacity, long expirationTimeInMs) {
3838
}
3939

4040
/**
41-
* Updates the API access log with the given route, method, and status code. If the record exists
42-
* and is outdated, it is updated by moving to the end of the list. If the record does not exist,
43-
* a new record is added. If the capacity limit is reached, the oldest record is removed. Returns
44-
* true if the record was updated or added, false otherwise.
41+
* Updates the API access log with the given route, method, and status code. If the record already
42+
* exists and is outdated, it is updated by moving to the end of the list. If the record does not
43+
* exist, a new record is added. If the capacity limit is reached, the oldest record is removed.
44+
* This method should not be called concurrently by multiple threads, due absence of additional
45+
* synchronization for updating data structures is not required.
4546
*
4647
* @param route The route of the API endpoint request
4748
* @param method The method of the API request
@@ -66,7 +67,7 @@ public boolean updateApiAccessIfExpired(String route, String method, int statusC
6667
isNewOrUpdated = true;
6768

6869
// Remove the oldest hash if capacity is reached
69-
while (apiAccessQueue.size() > this.capacity) {
70+
while (apiAccessMap.size() > this.capacity) {
7071
Long oldestHash = apiAccessQueue.pollFirst();
7172
if (oldestHash != null) {
7273
apiAccessMap.remove(oldestHash);

dd-java-agent/appsec/src/main/java/com/datadog/appsec/gateway/GatewayBridge.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import datadog.trace.api.telemetry.WafMetricCollector;
4242
import datadog.trace.bootstrap.instrumentation.api.Tags;
4343
import datadog.trace.bootstrap.instrumentation.api.URIDataAdapter;
44+
import datadog.trace.util.NonBlockingSemaphore;
4445
import datadog.trace.util.stacktrace.StackTraceEvent;
4546
import datadog.trace.util.stacktrace.StackUtils;
4647
import java.net.URI;
@@ -94,6 +95,10 @@ public class GatewayBridge {
9495

9596
private static final String METASTRUCT_EXPLOIT = "exploit";
9697

98+
private final int MAX_POST_PROCESSING_TASKS = 16;
99+
private final NonBlockingSemaphore postProcessingCounter =
100+
NonBlockingSemaphore.withPermitCount(MAX_POST_PROCESSING_TASKS);
101+
97102
private final SubscriptionService subscriptionService;
98103
private final EventProducerService producerService;
99104
private final ApiSecurityRequestSampler requestSampler;
@@ -833,7 +838,7 @@ private NoopFlow onRequestEnded(RequestContext ctx_, IGSpanInfo spanInfo) {
833838
if (route instanceof String) {
834839
ctx.setRoute((String) route);
835840
}
836-
if (requestSampler.preSampleRequest(ctx)) {
841+
if (requestSampler.preSampleRequest(ctx) && postProcessingCounter.acquire()) {
837842
// The request is pre-sampled - we need to post-process it
838843
spanInfo.setRequiresPostProcessing(true);
839844
}
@@ -905,6 +910,8 @@ private void onPostProcessing(RequestContext ctx_) {
905910

906911
maybeExtractSchemas(ctx);
907912
ctx.close();
913+
// Decrease the counter to allow the next request to be post-processed
914+
postProcessingCounter.release();
908915
}
909916

910917
public void stop() {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.datadog.iast.util;
1+
package datadog.trace.util;
22

33
import java.util.concurrent.atomic.AtomicBoolean;
44
import java.util.concurrent.atomic.AtomicInteger;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.datadog.iast.util
1+
package datadog.trace.util
22

33
import datadog.trace.test.util.DDSpecification
44
import groovy.transform.CompileDynamic

0 commit comments

Comments
 (0)