Skip to content

Commit 43de367

Browse files
committed
refactor: generate unique monotonic ids
Signed-off-by: Matheus Cruz <[email protected]>
1 parent 36a6322 commit 43de367

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

impl/core/src/main/java/io/serverlessworkflow/impl/MonotonicUlidWorkflowInstanceIdFactory.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
package io.serverlessworkflow.impl;
1717

1818
import de.huxhorn.sulky.ulid.ULID;
19+
1920
import java.security.SecureRandom;
20-
import java.util.concurrent.locks.ReentrantLock;
21+
import java.util.concurrent.atomic.AtomicReference;
2122

2223
/**
2324
* A {@link WorkflowInstanceIdFactory} implementation that generates Monotonic ULIDs as workflow
@@ -26,16 +27,14 @@
2627
public class MonotonicUlidWorkflowInstanceIdFactory implements WorkflowInstanceIdFactory {
2728

2829
private final ULID ulid;
29-
private ULID.Value current;
30-
private final ReentrantLock lock = new ReentrantLock();
30+
private final AtomicReference<ULID.Value> currentUlid;
3131

3232
public MonotonicUlidWorkflowInstanceIdFactory() {
3333
this.ulid = new ULID(new SecureRandom());
34-
this.current = ulid.nextValue();
34+
this.currentUlid = new AtomicReference<>(ulid.nextValue());
3535
}
3636

3737
public String get() {
38-
this.current = ulid.nextMonotonicValue(this.current);
39-
return current.toString();
38+
return currentUlid.getAndUpdate(previousUlid -> ulid.nextMonotonicValue(previousUlid)).toString();
4039
}
4140
}

0 commit comments

Comments
 (0)