Skip to content

Commit

Permalink
handle clock drift #12
Browse files Browse the repository at this point in the history
Added drift tolerance of 10 seconds to preserve monotonicity when the
system clock is adjusted by NTP after a small clock drift or when the
system clock jumps back by 1 second due to leap second.

The payload component is incremented when the current time:
- is the same as the previous time;
- has moved backwards up to 10 seconds.

The time component is also incremented if the payload component is
exceeded, which is quite rare.
  • Loading branch information
fabiolimace committed Apr 22, 2022
1 parent a194293 commit 015f252
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 109 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ All notable changes to this project will be documented in this file.

Nothing unreleaded.

## [3.1.0] - 2022-04-21

Handle clock drift. #12

## [3.0.0] - 2022-01-30

Implemented KSUID with sub-second precision. #11
Expand Down Expand Up @@ -118,7 +122,8 @@ Project created as an alternative Java implementation of [K-Sortable Unique IDen
- Added `DefaultRandomGenerator`
- Added test cases

[unreleased]: https://github.com/f4b6a3/ksuid-creator/compare/ksuid-creator-3.0.0...HEAD
[unreleased]: https://github.com/f4b6a3/ksuid-creator/compare/ksuid-creator-3.1.0...HEAD
[3.1.0]: https://github.com/f4b6a3/ksuid-creator/compare/ksuid-creator-3.0.0...ksuid-creator-3.1.0
[3.0.0]: https://github.com/f4b6a3/ksuid-creator/compare/ksuid-creator-2.3.0...ksuid-creator-3.0.0
[2.3.0]: https://github.com/f4b6a3/ksuid-creator/compare/ksuid-creator-2.2.0...ksuid-creator-2.3.0
[2.2.0]: https://github.com/f4b6a3/ksuid-creator/compare/ksuid-creator-2.1.2...ksuid-creator-2.2.0
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Fabio Lima
Copyright (c) 2021-2022 Fabio Lima

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Add these lines to your `pom.xml`.
<dependency>
<groupId>com.github.f4b6a3</groupId>
<artifactId>ksuid-creator</artifactId>
<version>3.0.0</version>
<version>3.1.0</version>
</dependency>
```

Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/github/f4b6a3/ksuid/Ksuid.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2021 Fabio Lima
* Copyright (c) 2021-2022 Fabio Lima
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -383,6 +383,14 @@ public Ksuid increment() {
for (int i = PAYLOAD_BYTES - 1; i >= 0; i--) {
if (++ksuid.payload[i] != overflow) {
break; // stop if did't overflow
} else {
if (i == 0) {
// If the payload component overflows,
// increment the time component by 1.
// This event is quite rare to occur.
final long time = ksuid.getTime() + 1;
ksuid = new Ksuid(time, ksuid.getPayload());
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/f4b6a3/ksuid/KsuidCreator.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2021 Fabio Lima
* Copyright (c) 2021-2022 Fabio Lima
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Loading

0 comments on commit 015f252

Please sign in to comment.