bwmarrin / snowflake

A simple to use Go (golang) package to generate or parse Twitter snowflake IDs
BSD 2-Clause "Simplified" License
2.98k stars 371 forks source link

What will happen if generate more than 4096 in 1 millisecond? #25

Closed zhangzitao closed 4 years ago

zhangzitao commented 4 years ago

Hi,

What will node.Generate() return if it generated more than 4096 (such as 5000) in 1 millisecond?

Will the squeue number reset to 0? or block?

import (
    "fmt"

    "github.com/bwmarrin/snowflake"
)

func main() {
    node, _ := snowflake.NewNode(0)
    m := make(map[int64]bool)
    timeMap := make(map[int64]bool)
    for i := 0; i < 5000; i++ {
        id := node.Generate()
        timeMap[id.Time()] = true
        if _, ok := m[id.Int64()]; ok {
            fmt.Printf("wrong: same id %d", id)
        }
        m[id.Int64()] = true
    }
    fmt.Printf("%+v", timeMap)
}

Thanks!

zhangzitao commented 4 years ago

sorry, I found the source code

if n.step == 0 {
            for now <= n.time {
                now = time.Since(n.epoch).Nanoseconds() / 1000000
            }
        }