-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
94715ff
commit 6d5c67d
Showing
3 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
...ava-agent/appsec/src/main/java/com/datadog/appsec/api/security/PostProcessingCounter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.datadog.appsec.api.security; | ||
|
||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
// Number of post-processing tasks (e.g. AppSecRequestContext keep opened) | ||
public class PostProcessingCounter extends AtomicLong { | ||
public static final long MAX_POST_PROCESSING_TASKS = 16; | ||
|
||
public boolean tryIncrement() { | ||
while (true) { | ||
long current = this.get(); | ||
if (current >= MAX_POST_PROCESSING_TASKS) { | ||
// Do not increment it's already at the maximum | ||
return false; | ||
} | ||
if (this.compareAndSet(current, current + 1)) { | ||
return true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...t/appsec/src/test/groovy/com/datadog/appsec/api/security/PostProcessingCounterTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.datadog.appsec.api.security | ||
|
||
import spock.lang.Specification | ||
|
||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.Executors | ||
|
||
class PostProcessingCounterSpec extends Specification { | ||
|
||
def "should increment successfully if below the limit"() { | ||
given: | ||
def counter = new PostProcessingCounter() | ||
|
||
when: | ||
def result = counter.tryIncrement() | ||
|
||
then: | ||
result == true | ||
counter.get() == 1 | ||
} | ||
|
||
def "should not increment if max limit is reached"() { | ||
given: | ||
def counter = new PostProcessingCounter() | ||
counter.set(PostProcessingCounter.MAX_POST_PROCESSING_TASKS) // Manually setting to max | ||
|
||
when: | ||
def result = counter.tryIncrement() | ||
|
||
then: | ||
result == false | ||
counter.get() == PostProcessingCounter.MAX_POST_PROCESSING_TASKS // Should remain unchanged | ||
} | ||
|
||
def "should handle concurrent increments safely"() { | ||
given: | ||
def counter = new PostProcessingCounter() | ||
def threads = 20 | ||
def executor = Executors.newFixedThreadPool(threads) | ||
def latch = new CountDownLatch(threads) | ||
def successes = Collections.synchronizedList([]) | ||
|
||
when: | ||
(1..threads).each { | ||
executor.submit { | ||
def result = counter.tryIncrement() | ||
if (result) { | ||
successes.add(1) | ||
} | ||
latch.countDown() | ||
} | ||
} | ||
latch.await() | ||
executor.shutdown() | ||
|
||
then: | ||
successes.size() <= PostProcessingCounter.MAX_POST_PROCESSING_TASKS // Only max increments should succeed | ||
counter.get() == PostProcessingCounter.MAX_POST_PROCESSING_TASKS // Should be exactly at the limit | ||
} | ||
} |