Skip to content

Commit 8ebfa21

Browse files
Create the Random instance only once
1 parent 1c56be5 commit 8ebfa21

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

sdk-api-kotlin/src/main/kotlin/dev/restate/sdk/kotlin/ContextImpl.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ internal constructor(
3434
internal val contextSerdeFactory: SerdeFactory,
3535
) : WorkflowContext {
3636

37+
internal val random: RestateRandom = RestateRandom(handlerContext.request().invocationId().toRandomSeed());
38+
3739
override fun key(): String {
3840
return this.handlerContext.objectKey()
3941
}
@@ -191,7 +193,7 @@ internal constructor(
191193
}
192194

193195
override fun random(): RestateRandom {
194-
return RestateRandom(handlerContext.request().invocationId().toRandomSeed())
196+
return this.random
195197
}
196198

197199
override fun <T : Any> promise(key: DurablePromiseKey<T>): DurablePromise<T> {

sdk-api/src/main/java/dev/restate/sdk/ContextImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ class ContextImpl implements ObjectContext, WorkflowContext {
3636
private final HandlerContext handlerContext;
3737
private final Executor serviceExecutor;
3838
private final SerdeFactory serdeFactory;
39+
private final RestateRandom random;
3940

4041
ContextImpl(HandlerContext handlerContext, Executor serviceExecutor, SerdeFactory serdeFactory) {
4142
this.handlerContext = handlerContext;
4243
this.serviceExecutor = serviceExecutor;
4344
this.serdeFactory = serdeFactory;
45+
this.random = new RestateRandom(this.request().invocationId().toRandomSeed());
4446
}
4547

4648
static void checkNotInsideRun() {
@@ -275,7 +277,7 @@ public void reject(String reason) {
275277

276278
@Override
277279
public RestateRandom random() {
278-
return new RestateRandom(this.request().invocationId().toRandomSeed());
280+
return this.random;
279281
}
280282

281283
@Override

0 commit comments

Comments
 (0)