Skip to content

Commit

Permalink
Version update
Browse files Browse the repository at this point in the history
- Error handling of time moving backward
- Documentation update
  • Loading branch information
T-PWK committed Apr 16, 2020
1 parent 83e40d8 commit fcd6a2f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ It would give something like:
<Buffer 50 dd d6 49 f0 00 00 00>
```

### Counter overflow ###
Flake ID Generator can generate up to 4096 unique identifiers within a millisecond. When generator tries to generate more than 4096 identifiers within a millisecond, the following things will happen:
* When using `next()` without a callback function, an error is thrown.
* When using `next(cb)` with a callback function, the callback function is called in the following millisecond without any error.

### Additional generator setup parameters ###
Flake Id generator constructor takes optional parameter (generator configuration options) with the following properties:
* `datacenter` (5 bit) - datacenter identifier. It can have values from 0 to 31.
* `worker` (5 bit) - worker identifier. It can have values from 0 to 31.
Expand Down Expand Up @@ -182,7 +188,12 @@ undefined
0
```

### Formatting ###
### Clock moving backward ###
From time to time Node.js clock may move backward. In most cases it is only a few millisecond. However, as the generator relies on current timestamp, it won't be able to generate conflict-free identifiers (i.e. without duplicates) until the clock catches up with the last timestamp value. In case of clock move backward the following things will happen:
* When using `next()` without a callback function, an error is thrown.
* When using `next(cb)` with a callback function, the callback function is called with a new identifier generated once the clock catches up with the last timestamp.

## Formatting ##

Flake Id generator returns node Buffer representing 64-bit number for the sake of future extensions or returned buffer modifications. Node Buffer can also be very easily converted to string format. There is a NPM [biguint-format](https://npmjs.org/package/biguint-format) module which provides Buffer to string conversion functionality e.g.

Expand Down
7 changes: 7 additions & 0 deletions flake-id-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@
var time = Date.now() - this.epoch;

// Generates id in the same millisecond as the previous id
if (time < this.lastTime) {
if (cb) {
setTimeout(self.next.bind(self, cb), this.lastTime - time);
return;
}
throw new Error(`Clock moved backwards. Refusing to generate id for ${this.lastTime - time} milliseconds`);
}
if (time === this.lastTime) {

// If all sequence values (4096 unique values including 0) have been used
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flake-idgen",
"version": "1.3.0",
"version": "1.4.0",
"description": "Flake ID generator yields k-ordered, conflict-free ids in a distributed environment",
"main": "flake-id-gen.js",
"scripts": {
Expand Down

0 comments on commit fcd6a2f

Please sign in to comment.