Giter Site home page Giter Site logo

snowflake's Introduction

Hi there ๐Ÿ‘‹

snowflake's People

Contributors

knotgrass avatar vd2org avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

snowflake's Issues

How to handle None result properly?

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?

getting duplicate id error in django project multi threaded environment. (#6 re-open)

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)
    ```

class SnowflakeGenerator MAX_TS bug

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

two questions about using Snowflake

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

optimize runtime

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)

getting duplicate id in multi-threded environment (django)

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.