Skip to content

Commit 36a6322

Browse files
committed
refactor: apply pull request suggestions
Signed-off-by: Matheus Cruz <[email protected]>
1 parent 3d4b647 commit 36a6322

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,25 @@
1717

1818
import de.huxhorn.sulky.ulid.ULID;
1919
import java.security.SecureRandom;
20+
import java.util.concurrent.locks.ReentrantLock;
2021

2122
/**
2223
* A {@link WorkflowInstanceIdFactory} implementation that generates Monotonic ULIDs as workflow
2324
* instance IDs.
2425
*/
2526
public class MonotonicUlidWorkflowInstanceIdFactory implements WorkflowInstanceIdFactory {
2627

27-
private final SecureRandom random = new SecureRandom();
28-
private final ULID ulid = new ULID(random);
29-
private ULID.Value previousUlid;
28+
private final ULID ulid;
29+
private ULID.Value current;
30+
private final ReentrantLock lock = new ReentrantLock();
3031

31-
@Override
32-
public synchronized String get() {
33-
if (previousUlid == null) {
34-
previousUlid = ulid.nextValue();
35-
} else {
36-
previousUlid = ulid.nextMonotonicValue(previousUlid);
37-
}
38-
return previousUlid.toString();
32+
public MonotonicUlidWorkflowInstanceIdFactory() {
33+
this.ulid = new ULID(new SecureRandom());
34+
this.current = ulid.nextValue();
35+
}
36+
37+
public String get() {
38+
this.current = ulid.nextMonotonicValue(this.current);
39+
return current.toString();
3940
}
4041
}

0 commit comments

Comments
 (0)