Skip to content

Commit

Permalink
2 minor improvements from review
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanking committed Nov 30, 2010
1 parent 1b9782c commit 0815fda
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/main/scala/com/twitter/service/snowflake/IdWorker.scala
Expand Up @@ -26,7 +26,7 @@ class IdWorker(workerId: Long, datacenterId: Long) extends Snowflake.Iface {
private val datacenterIdBits = 5L
private val maxWorkerId = -1L ^ (-1L << workerIdBits)
private val maxDatacenterId = -1L ^ (-1L << datacenterIdBits)
private val sequenceBits = 12
private val sequenceBits = 12L

private val workerIdShift = sequenceBits
private val datacenterIdShift = sequenceBits + workerIdBits
Expand Down Expand Up @@ -68,12 +68,6 @@ class IdWorker(workerId: Long, datacenterId: Long) extends Snowflake.Iface {
protected[snowflake] def nextId(): Long = synchronized {
var timestamp = timeGen()

if (timestamp < lastTimestamp) {
exceptionCounter.incr(1)
log.error("clock is moving backwards. Rejecting requests until %d.", lastTimestamp);
throw new InvalidSystemClock("Clock moved backwards. Refusing to generate id for %d milliseconds".format(lastTimestamp - timestamp));
}

if (lastTimestamp == timestamp) {
sequence = (sequence + 1) & sequenceMask
if (sequence == 0) {
Expand All @@ -83,6 +77,13 @@ class IdWorker(workerId: Long, datacenterId: Long) extends Snowflake.Iface {
sequence = 0
}

if (timestamp < lastTimestamp) {
exceptionCounter.incr(1)
log.error("clock is moving backwards. Rejecting requests until %d.", lastTimestamp);
throw new InvalidSystemClock("Clock moved backwards. Refusing to generate id for %d milliseconds".format(lastTimestamp - timestamp));
}


lastTimestamp = timestamp
genCounter.incr()
((timestamp - twepoch) << timestampLeftShift) |
Expand Down

0 comments on commit 0815fda

Please sign in to comment.