An Lock Free ID Generator for Golang implementation View on GitHub.
Snowflake is a network service for generating unique ID numbers at high scale with some simple guarantees.
The ID generated by the snowflake algorithm is not guaranteed to be unique. For example, when two different requests enter the same machine at the same time, and the sequence generated by the node is the same, the generated ID will be duplicated.
So if you want use the snowflake algorithm to generate unique ID, You must ensure: The sequence-number generated in the same millisecond of the same node is unique.
Based on this, we created this package and integrated multiple sequence-number providers into it.
Each provider only needs to ensure that the serial number generated in the same millisecond is different. You can get a unique ID.
$ go get github.com/godruoyi/go-snowflake
package main
import (
"fmt"
"github.com/godruoyi/go-snowflake"
)
func main() {
id := snowflake.ID()
fmt.Println(id)
// 1537200202186752
}
package main
import (
"fmt"
"github.com/godruoyi/go-snowflake"
)
func main() {
snowflake.SetMachineID(1)
// Or set private ip to machineid, testing...
// snowflake.SetMachineID(snowflake.PrivateIPToMachineID())
id := snowflake.ID()
fmt.Println(id)
}
package main
import (
"fmt"
"time"
"github.com/godruoyi/go-snowflake"
)
func main() {
snowflake.SetStartTime(time.Date(2014, 9, 1, 0, 0, 0, 0, time.UTC))
id := snowflake.ID()
fmt.Println(id)
}
package main
import (
"fmt"
"time"
"github.com/godruoyi/go-snowflake"
)
func main() {
id := snowflake.ID()
sid := snowflake.ParseID(id)
fmt.Println(sid.ID) // 132271570944000000
fmt.Println(sid.MachineID) // 0
fmt.Println(sid.Sequence) // 0
fmt.Println(sid.Timestamp) // 31536000000
fmt.Println(sid.GenerateTime()) // 2009-11-10 23:00:00 +0000 UTC
}
???? All SetXXX method is thread-unsafe, recommended you call him in the main function.
package main
import (
"fmt"
"time"
"net/http"
"github.com/godruoyi/go-snowflake"
)
func main() {
snowflake.SetMachineID(1) // change to your machineID
snowflake.SetStartTime(time.Date(2014, 9, 1, 0, 0, 0, 0, time.UTC))
http.HandleFunc("/order", submitOrder)
http.ListenAndServe(":8090", nil)
}
func submitOrder(w http.ResponseWriter, req *http.Request) {
orderId := snowflake.ID()
// save order
}
Custom sequence resolver. you can customize the sequence-number resolver by following way:
package main
import (
"fmt"
"time"
"github.com/godruoyi/go-snowflake"
)
func yourSequenceNumber(ms int64) (uint16, error) {
}
// usage
snowflake.SetSequenceResolver(yourSequenceNumber)
snowflake.ID()
记录下一个疑问,最近在重新看canvas做点Demo这样,时间是写在2019年11月5日,以...
今天给大家讲一下清楚浮动,在讲清除浮动前,需要了解什么是浮动,这里我就不给...
前言 刚参加工作时被面试官一连串居中问题虐的体无完肤得场景历历在目。你是如何...
1.青春就像一场烟火,美得让人窒息,却也短暂到让人落泪。 2.怎知我用一转身离...
某天,领导提了一个需求,主页面里面嵌套iframe,父子页面双向发送消息。然后我...
DocumentFragment DocumentFragment 是什么 ? DocumentFragment,文档片段接口...
序 本文主要研究一下dddsample-core的model Entity public interface EntityT { ...
1.我给不了你世界上最好的一切,可是我一定会把我最好的一切都给你。 2.遇到你...
1.有时候,你必须做自己的英雄。 2.生活就像个洋葱,你只能一层一层剥开它,还...
XHTML是W3C推荐的一种标准,它定义了一种与XML兼容的HTML版本。XHTML文档是一个...