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

got same ids from different machines #17

Closed doudouwyh closed 5 years ago

doudouwyh commented 5 years ago

we got same ids from two machine, code likes below:

`var node *sf.Node func init_node() (err error) { node, err = sf.NewNode(1) if err != nil { panic("init node failed!") return } return }

func GenOrderid() { for { for i := 0; i < 4000; i++ { id := node.Generate() sorderid := strconv.FormatInt(id,10) if strings.TrimSpace(sorderid) == "" { continue }

        orderidchn <- strings.TrimSpace(sorderid)
    }
    time.Sleep(1 * time.Millisecond)
}

}`

doudouwyh commented 5 years ago

node, err = sf.NewNode(1) all machines init with the same number 1

bwmarrin commented 5 years ago

I'm guessing you're last comment is to explain why you had the issue, but to be clear.

All nodes must be set to a unique node number. If you configure multiple nodes with the same node number then you will receive duplicate IDs.