Skip to content

Commit 8043064

Browse files
authored
fix: adding retry delay for customers in sleepingBarber (#38)
1 parent d8b7b52 commit 8043064

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

sleepingBarber/src/main/kotlin/ac/at/uibk/dps/dapr/barber/customer/CustomerActorImpl.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import io.dapr.actors.runtime.AbstractActor
66
import io.dapr.actors.runtime.ActorRuntimeContext
77
import io.dapr.client.DaprClient
88
import io.dapr.client.DaprClientBuilder
9+
import java.security.SecureRandom
910
import java.util.concurrent.TimeUnit
11+
import kotlin.random.Random
1012
import kotlin.time.Clock
1113

1214
class CustomerActorImpl(runtimeContext: ActorRuntimeContext<CustomerActorImpl>, id: ActorId) :
@@ -15,6 +17,14 @@ class CustomerActorImpl(runtimeContext: ActorRuntimeContext<CustomerActorImpl>,
1517

1618
val client: DaprClient? = DaprClientBuilder().build()
1719

20+
private val seedGenerator = SecureRandom()
21+
private val threadRng =
22+
object : ThreadLocal<Random>() {
23+
override fun initialValue(): Random {
24+
return Random(seedGenerator.nextLong())
25+
}
26+
}
27+
1828
var count = 0
1929

2030
override fun request() {
@@ -23,6 +33,7 @@ class CustomerActorImpl(runtimeContext: ActorRuntimeContext<CustomerActorImpl>,
2333

2434
override fun full(data: Map<String, Any>) {
2535
measureEventTime(data)
36+
Thread.sleep(randomAround(10, 2).toLong())
2637
request()
2738
}
2839

@@ -53,4 +64,8 @@ class CustomerActorImpl(runtimeContext: ActorRuntimeContext<CustomerActorImpl>,
5364
.timer("event.latency")!!
5465
.update((deltaNanos), TimeUnit.NANOSECONDS)
5566
}
67+
68+
private fun randomAround(base: Int, delta: Int): Int {
69+
return (base - delta..base + delta).random(threadRng.get())
70+
}
5671
}

0 commit comments

Comments
 (0)