Package me.ahoo.cosid.machine
Interface ClockBackwardsSynchronizer
- All Known Implementing Classes:
DefaultClockBackwardsSynchronizer
@ThreadSafe
public interface ClockBackwardsSynchronizer
Synchronizer for handling clock backwards issues in Snowflake ID generation.
When system clock moves backwards, this synchronizer waits until the clock catches up to ensure unique IDs.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ClockBackwardsSynchronizerDefault synchronizer instance. -
Method Summary
Modifier and TypeMethodDescriptionstatic longgetBackwardsTimeStamp(long lastTimestamp) Calculates how far backwards the clock has moved.voidsync(long lastTimestamp) Synchronizes clock by waiting until current time exceeds lastTimestamp.voidsyncUninterruptibly(long lastTimestamp) Synchronizes clock without throwing InterruptedException.
-
Field Details
-
DEFAULT
Default synchronizer instance.
-
-
Method Details
-
sync
Synchronizes clock by waiting until current time exceeds lastTimestamp.- Parameters:
lastTimestamp- the last timestamp that was generated- Throws:
InterruptedException- if thread is interrupted while waitingClockTooManyBackwardsException- if clock backwards exceeds threshold
-
syncUninterruptibly
Synchronizes clock without throwing InterruptedException.- Parameters:
lastTimestamp- the last timestamp that was generated- Throws:
ClockTooManyBackwardsException- if clock backwards exceeds threshold
-
getBackwardsTimeStamp
static long getBackwardsTimeStamp(long lastTimestamp) Calculates how far backwards the clock has moved.- Parameters:
lastTimestamp- the last timestamp- Returns:
- the backwards duration in milliseconds
-