Skip to content

Commit ee77ce0

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

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

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

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

37+
internal val random: RestateRandom =
38+
RestateRandom(handlerContext.request().invocationId().toRandomSeed())
39+
3740
override fun key(): String {
3841
return this.handlerContext.objectKey()
3942
}
@@ -191,7 +194,7 @@ internal constructor(
191194
}
192195

193196
override fun random(): RestateRandom {
194-
return RestateRandom(handlerContext.request().invocationId().toRandomSeed())
197+
return this.random
195198
}
196199

197200
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)