support back

This commit is contained in:
Looly
2020-05-28 01:30:18 +08:00
parent 6b6366e1ef
commit 8bcb1daf99
2 changed files with 10 additions and 3 deletions

View File

@@ -141,10 +141,16 @@ public class Snowflake implements Serializable {
public synchronized long nextId() {
long timestamp = genTime();
if (timestamp < lastTimestamp) {
// 如果服务器时间有问题(时钟后退) 报错。
throw new IllegalStateException(StrUtil.format("Clock moved backwards. Refusing to generate id for {}ms", lastTimestamp - timestamp));
if(lastTimestamp - timestamp < 2000){
// 容忍2秒内的回拨避免NTP校时造成的异常
timestamp = lastTimestamp;
} else{
// 如果服务器时间有问题(时钟后退) 报错。
throw new IllegalStateException(StrUtil.format("Clock moved backwards. Refusing to generate id for {}ms", lastTimestamp - timestamp));
}
}
if (lastTimestamp == timestamp) {
if (timestamp == lastTimestamp) {
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) {
timestamp = tilNextMillis(lastTimestamp);