f4b6a3 / tsid-creator

A Java library for generating Time-Sorted Unique Identifiers (TSID).
MIT License
475 stars 50 forks source link

Remove the overrun exception #2

Closed fabiolimace closed 4 years ago

fabiolimace commented 4 years ago

Make the TimeIdCreator wait the next millisecond instead of throwing a TsidCreatorException.

Twitter Snowflake also waits the next millisecond:

sequence number - 12 bits - rolls over every 4096 per machine (with protection to avoid rollover in the same ms)

  protected def tilNextMillis(lastTimestamp: Long): Long = {
    var timestamp = timeGen()
    while (timestamp <= lastTimestamp) {
      timestamp = timeGen()
    }
    timestamp
  }

See this line: https://github.com/twitter-archive/snowflake/blob/b3f6a3c6ca8e1b6847baa6ff42bf72201e2c2231/src/main/scala/com/twitter/service/snowflake/IdWorker.scala#L97

fabiolimace commented 4 years ago

See the commit ebc864b2f8942b1be783f3d3030c29e556013235

New version: 2.3.0