Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why duplicate IDs are generated #12

Open
qzcsgs opened this issue Feb 21, 2022 · 2 comments
Open

Why duplicate IDs are generated #12

qzcsgs opened this issue Feb 21, 2022 · 2 comments

Comments

@qzcsgs
Copy link

qzcsgs commented Feb 21, 2022

function genFlakeId() {
    const flakeIdGen = new FlakeId();
    return intformat(flakeIdGen.next(), 'dec');
  }

const ids = [];
for (let i = 0; i < 50; i++) {
  ids.push(genFlakeId());
}
// ids
[
  "6901552045816283136",
  "6901552045820477440",
  "6901552045820477440",
  "6901552045833060352",
  "6901552045837254656",
  "6901552045841448960",
  "6901552045845643264",
  "6901552045849837568",
  "6901552045854031872",
  "6901552045858226176",
  "6901552045862420480",
  "6901552045875003392",
  "6901552045879197696",
  "6901552045879197696",
  "6901552045879197696",
  "6901552045883392000",
  "6901552045883392000",
  "6901552045883392000",
  "6901552045883392000",
  "6901552045883392000",
  "6901552045887586304",
  "6901552045887586304",
  "6901552045887586304",
  "6901552045887586304",
  "6901552045887586304",
  "6901552045891780608",
  "6901552045891780608",
  "6901552045891780608",
  "6901552045891780608",
  "6901552045891780608",
  "6901552045895974912",
  "6901552045895974912",
  "6901552045895974912",
  "6901552045895974912",
  "6901552045900169216",
  "6901552045900169216",
  "6901552045900169216",
  "6901552045900169216",
  "6901552045900169216",
  "6901552045904363520",
  "6901552045904363520",
  "6901552045904363520",
  "6901552045908557824",
  "6901552045908557824",
  "6901552045908557824",
  "6901552045912752128",
  "6901552045912752128",
  "6901552045912752128",
  "6901552045916946432",
  "6901552045916946432"
]
@seancheung
Copy link

seancheung commented Mar 10, 2022

'cause you made a new FakeId instance in every push. They were not related(with their counters) so they raced.

You should do something like this

const flakeIdGen = new FlakeId();
function genFlakeId() {
    return intformat(flakeIdGen.next(), 'dec');
}

const ids = [];
for (let i = 0; i < 50; i++) {
  ids.push(genFlakeId());
}

@qzcsgs
Copy link
Author

qzcsgs commented Mar 10, 2022

'cause you made a new FakeId instance in every push. They were not related(with their counters) so they raced.

You should do something like this

const flakeIdGen = new FlakeId();
function genFlakeId() {
    return intformat(flakeIdGen.next(), 'dec');
}

const ids = [];
for (let i = 0; i < 50; i++) {
  ids.push(genFlakeId());
}

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants