Skip to content

Commit 96deab0

Browse files
authored
bug: make hit throttle atomic (#139)
* bug: make hit throttle atomic * style: don't need
1 parent 5bbb74e commit 96deab0

File tree

2 files changed

+23
-28
lines changed

2 files changed

+23
-28
lines changed

services/src/main/kotlin/spp/probe/services/common/model/HitThrottle.kt

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,40 @@
1717
package spp.probe.services.common.model
1818

1919
import spp.protocol.instrument.throttle.ThrottleStep
20-
import kotlin.jvm.Transient
20+
import java.util.concurrent.atomic.AtomicInteger
21+
import java.util.concurrent.atomic.AtomicLong
2122

22-
class HitThrottle(private val limit: Int, step: ThrottleStep) {
23+
class HitThrottle(private val limit: Int, private val step: ThrottleStep) {
2324

24-
private val step: ThrottleStep
25+
private val lastReset = AtomicLong(-1)
26+
private val hitCount = AtomicInteger(0)
2527

26-
@Transient
27-
private var lastReset: Long = -1
28+
private val _totalHitCount = AtomicInteger(0)
29+
val totalHitCount: Int
30+
get() = _totalHitCount.get()
2831

29-
@Transient
30-
private var hitCount = 0
3132

32-
@Transient
33-
var totalHitCount = 0
34-
private set
35-
36-
@Transient
37-
var totalLimitedCount = 0
38-
private set
39-
40-
init {
41-
this.step = step
42-
}
33+
private val _totalLimitedCount = AtomicInteger(0)
34+
val totalLimitedCount: Int
35+
get() = _totalLimitedCount.get()
4336

4437
fun isRateLimited(): Boolean {
45-
if (hitCount++ < limit) {
46-
totalHitCount++
38+
if (hitCount.getAndIncrement() < limit) {
39+
if (lastReset.get() == -1L) {
40+
lastReset.set(System.currentTimeMillis())
41+
}
42+
43+
_totalHitCount.incrementAndGet()
4744
return false
4845
}
49-
return if (System.currentTimeMillis() - lastReset > step.toMillis(1)) {
50-
hitCount = 1
51-
totalHitCount++
52-
lastReset = System.currentTimeMillis()
46+
47+
return if (System.currentTimeMillis() - lastReset.get() > step.toMillis(1)) {
48+
hitCount.set(1)
49+
_totalHitCount.incrementAndGet()
50+
lastReset.set(System.currentTimeMillis())
5351
false
5452
} else {
55-
totalLimitedCount++
53+
_totalLimitedCount.incrementAndGet()
5654
true
5755
}
5856
}

services/src/test/kotlin/spp/probe/services/common/model/HitThrottleTest.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import java.util.concurrent.TimeUnit
2525
class HitThrottleTest {
2626

2727
@Test
28-
@Throws(Exception::class)
2928
fun oneASecond() {
3029
val hitThrottle = HitThrottle(1, ThrottleStep.SECOND)
3130
val scheduler = Executors.newScheduledThreadPool(1)
@@ -37,7 +36,6 @@ class HitThrottleTest {
3736
}
3837

3938
@Test
40-
@Throws(Exception::class)
4139
fun twiceASecond() {
4240
val hitThrottle = HitThrottle(2, ThrottleStep.SECOND)
4341
val scheduler = Executors.newScheduledThreadPool(1)
@@ -49,7 +47,6 @@ class HitThrottleTest {
4947
}
5048

5149
@Test
52-
@Throws(Exception::class)
5350
fun fourTimesASecond() {
5451
val hitThrottle = HitThrottle(4, ThrottleStep.SECOND)
5552
val scheduler = Executors.newScheduledThreadPool(1)

0 commit comments

Comments
 (0)