vd2org / snowflake Goto Github PK
View Code? Open in Web Editor NEWThe Snowflake generator done right.
License: MIT License
The Snowflake generator done right.
License: MIT License
Hello.
SnowflakeGenerator.__next__(self)
returns optional integer.
I am not sure how to handle the situation when None
value returned because I do need a value.
Should I wait for the next millisecond to guarantee non empty result?
previous question
I want to use snowflake ID generation in my project. so I am trying to use this library. but I am getting unique constraint error for the field primary key. I am using Django. can you please guide me, on how to set it up correctly to avoid getting duplicate IDs. like do I need any additional configurations?_
new question
I'm sorry for not getting back to you sooner. if I use a locking mechanism then can it be solved? if not can you please suggest to me how should I do it I am new to it. and our project is using UUID which I don't like. it'll be very helpful if you can provide in document, how to use it in a multi-threaded environment. I saw it everywhere but I couldn`t find it.
Here is my suggested answer.
from snowflake import SnowflakeGenerator
import threading
class ThreadSafeIdGeneratorService:
def __init__(self, instance_id):
self.gen = SnowflakeGenerator(ip_address_int)
self.lock = threading.Lock()
def generate_unique_id(self):
with self.lock:
return next(self.gen)
```
The generator method bug.
The maximum timestamp has been reached in selected epoch
OverflowError: The maximum timestamp has been reached in selected epoch, so Snowflake cannot generate more IDs!
When initialize the generator class, we get the current time, if time greater the MAX_TS, that means the epoch params is invalid, the program throws the error directly.
current = int(time() * 1000)
if current >= MAX_TS
Hello, I have two questions about using Snowflake. Can you help answer them?
1st: This is my first time encountering Snowflake ID. Can you explain what the 42
means in gen = SnowflakeGenerator(42)
? What guidelines should I follow when setting this value? Also, what are the roles of epoch
and seq
in the parse
method?
2nd: When I ran the following code, I noticed that the displayed times were inconsistent.
from snowflake import SnowflakeGenerator
import datetime
current_time = datetime.datetime.now()
print("current time:", current_time)
gen = SnowflakeGenerator(42)
snow_id = next(gen)
print("generate snowflake id:", snow_id)
from snowflake import Snowflake
sf = Snowflake.parse(snow_id)
print(f"{sf.timestamp = }")
print(f"{sf.instance = }")
print(f"{sf.epoch = }")
print(f"{sf.seq = }")
print(f"{sf.seconds = }")
print(f"{sf.milliseconds = }")
print(f"{sf.datetime = }")
print(f"{int(sf) = }")
result:
current time: 2023-08-30 17:05:43.994868
generate snowflake id: 7102577116159582209
sf.timestamp = 1693386343994
sf.instance = 42
sf.epoch = 0
sf.seq = 1
sf.seconds = 1693386343.994
sf.milliseconds = 1693386343994
sf.datetime = datetime.datetime(2023, 8, 30, 9, 5, 43, 994000)
int(sf) = 7102577116159582209
I think we can optimize runtime in SnowflakeGenerator
by change
L74: current = int(time() * 1000)
-> current = int(time() * 1000.)
L112: current = int(time() * 1000) - self._epo
-> current = int(time() * 1000.) - self._epo
Because multiply 2 float is faster twice than multiply a float
and int
. (note that time.time() return float)
I want to use snowflake ID generation in my project. so I am trying to use this library. but I am getting unique constraint error for the field primary key. I am using Django. can you please guide me, on how to set it up correctly to avoid getting duplicate IDs. like do I need any additional configurations?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.