godruoyi/go-snowflake: An Lock Free ID Generator ... - GitHub
文章推薦指數: 80 %
An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced). - GitHub - godruoyi/go-snowflake: ❄ An Lock Free ID Generator for ... Skiptocontent {{message}} godruoyi / go-snowflake Public Notifications Fork 18 Star 180 ❄AnLockFreeIDGeneratorforGolangbasedonSnowflakeAlgorithm(Twitterannounced). godruoyi.com/posts/golang-snowflake License MITlicense 180 stars 18 forks Star Notifications Code Issues 0 Pullrequests 0 Actions Projects 0 Wiki Security Insights More Code Issues Pullrequests Actions Projects Wiki Security Insights Thiscommitdoesnotbelongtoanybranchonthisrepository,andmaybelongtoaforkoutsideoftherepository. master Branches Tags Couldnotloadbranches Nothingtoshow {{refName}} default Couldnotloadtags Nothingtoshow {{refName}} default 1 branch 4 tags Code Latestcommit Gitstats 17 commits Files Permalink Failedtoloadlatestcommitinformation. Type Name Latestcommitmessage Committime .github example .gitignore LICENSE atomic_resolver.go atomic_resolver_test.go go.mod package.go privateip.go privateip_test.go readme.md snowflake.go snowflake_test.go Viewcode Description Feature Installation Usage Bestpractices Advanced License readme.md AnLockFreeIDGeneratorforGolangbasedonSnowflakeAlgorithm(Twitterannounced). Description AnLockFreeIDGeneratorforGolangimplementation. SnowflakeisanetworkserviceforgeneratinguniqueIDnumbersathighscalewithsomesimpleguarantees. Thefirstbitisunusedsignbit. Thesecondpartconsistsofa41-bittimestamp(milliseconds)whosevalueistheoffsetofthecurrenttimerelativetoacertaintime. The10bitsmachineID(5bitworkid+5bitdatacenterid),maxvalueis2^10-1=1023. Thelastpartconsistsof12bits,itsmeansthelengthoftheserialnumbergeneratedpermillisecondperworkingnode,amaximumof2^12-1=4095IDscanbegeneratedinthesamemillisecond. Thebinarylengthof41bitsisatmost2^41-1millisecond=69years.Sothesnowflakealgorithmcanbeusedforupto69years,Inordertomaximizetheuseofthealgorithm,youshouldspecifyastarttimeforit. TheIDgeneratedbythesnowflakealgorithmisnotguaranteedtobeunique.Forexample,whentwodifferentrequestsenterthesamemachineatthesametime,andthesequencegeneratedbythenodeisthesame,thegeneratedIDwillbeduplicated. SoifyouwantusethesnowflakealgorithmtogenerateuniqueID,Youmustensure:Thesequence-numbergeneratedinthesamemillisecondofthesamenodeisunique. Basedonthis,wecreatedthispackageandintegratedmultiplesequence-numberprovidersintoit. AtomicResolver(basesync/atomic) Eachprovideronlyneedstoensurethattheserialnumbergeneratedinthesamemillisecondisdifferent.YoucangetauniqueID. Feature ✅LockFree 🎈Zeroconfiguration,outofthebox 🚀Concurrencysafety 🌵Supportprivateiptomachineid 🐡Supportcustomsequenceresolver Installation $gogetgithub.com/godruoyi/go-snowflake Usage simpletouse. packagemain import( "fmt" "github.com/godruoyi/go-snowflake" ) funcmain(){ id:=snowflake.ID() fmt.Println(id) //1537200202186752 } SpecifytheMachineID. packagemain import( "fmt" "github.com/godruoyi/go-snowflake" ) funcmain(){ snowflake.SetMachineID(1) //Orsetprivateiptomachineid,testing... //snowflake.SetMachineID(snowflake.PrivateIPToMachineID()) id:=snowflake.ID() fmt.Println(id) } Specifystarttime. packagemain import( "fmt" "time" "github.com/godruoyi/go-snowflake" ) funcmain(){ snowflake.SetStartTime(time.Date(2014,9,1,0,0,0,0,time.UTC)) id:=snowflake.ID() fmt.Println(id) } ParseID. packagemain import( "fmt" "time" "github.com/godruoyi/go-snowflake" ) funcmain(){ 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-1023:00:00+0000UTC } Bestpractices ⚠️⚠️AllSetXXXmethodisthread-unsafe,recommendedyoucallhiminthemainfunction. packagemain import( "fmt" "time" "net/http" "github.com/godruoyi/go-snowflake" ) funcmain(){ snowflake.SetMachineID(1)//changetoyourmachineID snowflake.SetStartTime(time.Date(2014,9,1,0,0,0,0,time.UTC)) http.HandleFunc("/order",submitOrder) http.ListenAndServe(":8090",nil) } funcsubmitOrder(whttp.ResponseWriter,req*http.Request){ orderId:=snowflake.ID() //saveorder } Advanced Customsequenceresolver.youcancustomizethesequence-numberresolverbyfollowingway: packagemain import( "fmt" "time" "github.com/godruoyi/go-snowflake" ) funcyourSequenceNumber(msint64)(uint16,error){ } //usage snowflake.SetSequenceResolver(yourSequenceNumber) snowflake.ID() License MIT About ❄AnLockFreeIDGeneratorforGolangbasedonSnowflakeAlgorithm(Twitterannounced). godruoyi.com/posts/golang-snowflake Topics snowflake go-snowflake Resources Readme License MITlicense Stars 180 stars Watchers 6 watching Forks 18 forks Releases 4 tags Sponsorthisproject https://images.godruoyi.com/wechat.png Packages0 Nopackagespublished Usedby25 +17 Contributors2 Languages Go 100.0% Youcan’tperformthatactionatthistime. Yousignedinwithanothertaborwindow.Reloadtorefreshyoursession. Yousignedoutinanothertaborwindow.Reloadtorefreshyoursession.
延伸文章資訊
- 1bwmarrin/snowflake: A simple to use Go (golang ... - GitHub
A very simple Twitter snowflake generator. · Methods to parse existing snowflake IDs. · Methods t...
- 2A .NET port of Twitter Snowflake ID generation algorithm.
移植 Snowflake 的 ID 生成算法。有关该算法的详情,请参阅. /// https://github.com/twitter/snowflake/tree/snowflake-2010...
- 3twitter-archive/snowflake - GitHub
GitHub - twitter-archive/snowflake: Snowflake is a network service for generating unique ID numbe...
- 4Twitter's Snowflake: A distributed time-based unique ID ...
Twitter's Snowflake: A distributed time-based unique ID generator - GitHub - jtejido/snowflake: T...
- 5sony/sonyflake: A distributed unique ID generator ... - GitHub
Sonyflake is a distributed unique ID generator inspired by Twitter's Snowflake. Sonyflake focuses...