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

Improve machine_id ergonomics #9

Merged
merged 2 commits into from
Aug 16, 2024

Conversation

ZipFile
Copy link
Contributor

@ZipFile ZipFile commented Aug 9, 2024

I'm successfully using this library in production for a quite a while, but API around machine ids caused me some degree of discomfort. This PR fixes #8 as well as some improvements:

  • Generating a lot of (millions) of ids efficiently is possible with multiple SonyFlake instances. Using callbacks to pass constant ids is counter-intuitive and not as performant. In this case you want to pre-generate set of random machine ids and iterate over list of id generators.
  • Issue not generating unique id for single machine in celery tasks/threads. #8 mentions Celery, but the problem is much more general. If several id generators share the same IP, issue would re-appear. To some extent it can be mitigated by using random machine ids. On somehow related note, new UUID standard no longer recommends using non-random node ids which is essentially has the same role as machine_id in SonyFlake.
  • Removed __new__ method, since it does not make sense. If some input params are invalid, None would be unpleasant surprise to encounter. Better be explicit and raise an error.
  • Add option to pass now callback, for test reproducibility.

Overall, public API should be more-or-less backward compatible.

@ZipFile ZipFile force-pushed the fix/machine-ids branch 3 times, most recently from 7844732 to d88e29f Compare August 9, 2024 14:11
@hjpotter92
Copy link
Owner

Thanks for the PR. It looks really promising, and definitely something that can be published.

@hjpotter92 hjpotter92 merged commit 90239ec into hjpotter92:master Aug 16, 2024
1 check passed
@ZipFile ZipFile deleted the fix/machine-ids branch August 16, 2024 16:45
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

Successfully merging this pull request may close these issues.

not generating unique id for single machine in celery tasks/threads.
2 participants