hjpotter92 / sonyflake-py

A distributed unique ID generator inspired by Twitter's Snowflake, rewritten in python
https://pypi.org/project/sonyflake-py/
MIT License
33 stars 10 forks source link

59 bit values #6

Closed tausif-deepsource closed 3 years ago

tausif-deepsource commented 3 years ago

hey, i am getting 59 bit values what am i missing or not doing correctly?

hjpotter92 commented 3 years ago

There's nothing wrong with 59-bit values. The final representation depends on the formatting, but the value will not exceed the set (39 + 16 + 8) 63 bits.

A sample equivalent generation from golang gives:

362637573840961538       10100001000010110001111001110111011000000000000000000000010

which is also 59-bits. The code used:

func main() {
        var st sonyflake.Settings
        sf := sonyflake.NewSonyflake(st)
        id, err := sf.NextID()
        fmt.Printf("%d \t %b\n", id, id)
}
tausif-deepsource commented 3 years ago

Got it thanks.

Just one follow up question what is the correct way to generate ids in a production setting i mean should i instantiate it once globally and generate ids from the instance? or is there a better optimal way kindly suggest.

hjpotter92 commented 3 years ago

Got it thanks.

Just one follow up question what is the correct way to generate ids in a production setting i mean should i instantiate it once globally and generate ids from the instance? or is there a better optimal way kindly suggest.

The library uses a thread lock, so a single instance should work fine. Try and please ping if you face any issues.

tausif-deepsource commented 3 years ago

Thanks for being responsive i will be using this in production so will reach out if i face any issues :)