RobThree/IdGen: Twitter Snowflake-alike ID generator for .Net
文章推薦指數: 80 %
Twitter Snowflake-alike ID generator for .Net. Contribute to RobThree/IdGen development by creating an account on GitHub.
Skiptocontent
{{message}}
RobThree
/
IdGen
Public
Notifications
Fork
112
Star
768
TwitterSnowflake-alikeIDgeneratorfor.Net
License
MITlicense
768
stars
112
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
10
tags
Code
Latestcommit
Gitstats
113
commits
Files
Permalink
Failedtoloadlatestcommitinformation.
Type
Name
Latestcommitmessage
Committime
.github
CreateFUNDING.yml
Feb4,2021
IdGen.Configuration
AddedIdGen.DependencyInjectionproject
May28,2022
IdGen.DependencyInjection
Keepmajorversionsinsync
May28,2022
IdGen
Updateddocumentation
May28,2022
IdGenDocumentation
Updateddocumentation
May28,2022
IdGenTests
AddedIdGen.DependencyInjectionproject
May28,2022
.editorconfig
Somecleanupandinternalrefactoring
May28,2022
.gitignore
*Addmissingfile
Apr2,2015
IdGen.sln
AddedIdGen.DependencyInjectionproject
May28,2022
LICENSE
Initialcommit
Apr2,2015
README.md
AddremarkinREADME
May28,2022
logo.png
MovedtoNetStandard1.1.Droppedconfigurationfilesupport(GetFrom…
Feb18,2020
Viewcode
IdGen
Why
Howitworks
SystemClockDependency
Gettingstarted
DependencyInjection
Upgradingfrom2.xto3.x
FAQ
README.md
IdGen
TwitterSnowflake-alikeIDgeneratorfor.Net.AvailableasNugetpackage
Why
Incertainsituationsyouneedalow-latency,distributed,uncoordinated,(roughly)timeordered,compactandhighlyavailableIdgenerationsystem.ThisprojectwasinspiredbyTwitter'sSnowflakeprojectwhichhasbeenretired.NotethatthisprojectwasinspiredbySnowflakebutisnotanexactimplementation.ThislibraryprovidesabasisforIdgeneration;itdoesnotprovideaserviceforhandingouttheseId'snordoesitprovidegenerator-id('worker-id')coordination.
Howitworks
IdGengenerates,likeSnowflake,64bitId's.TheSignBitisunusedsincethiscancauseincorrectorderingonsomesystemsthatcannotuseunsignedtypesand/ormakeithardtogetcorrectordering.So,ineffect,IdGengenerates63bitId's.AnIdconsistsof3parts:
Timestamp
Generator-id
Sequence
AnIdgeneratedwithaDefaultIdStructureisstructuredasfollows:
However,usingtheIdStructureclassyoucantunethestructureofthecreatedId'stoyourownneeds;youcanuse45bitsforthetimestamp,2bitsforthegenerator-idand16bitsforthesequenceifyouprefer.Aslongasall3parts(timestamp,generatorandsequence)addupto63bitsyou'regoodtogo!
Thetimestamp-partoftheIdshouldspeakforitself;bydefaultthisisincrementedeverymillisecondandrepresentsthenumberofmillisecondssinceacertainepoch.However,IdGenreliesonanITimeSourcewhichusesa'tick'thatcanbedefinedtobeanything;beitamillisecond(default),asecondorevenadayornanosecond(hardwaresupportetc.permitting).BydefaultIdGenuses2015-01-010:00:00Zasepoch,butyoucanspecifyacustomepochtoo.
Thegenerator-id-partoftheIdisthepartthatyou'configure';itcouldcorrespondtoahost,thread,datacenterorcontinent:it'suptoyou.However,thegenerator-idshouldbeuniqueinthesystem:ifyouhaveseveralhostsorthreadsgeneratingId's,eachhostorthreadshouldhaveit'sowngenerator-id.Thiscouldbebasedonthehostname,aconfig-filevalueorevenberetrievedfromancoordinatingservice.Remember:agenerator-idshouldbeuniquewithintheentiresystemtoavoidcollisions!
Thesequence-partissimplyavaluethatisincrementedeachtimeanewIdisgeneratedwithinthesametick(again,bydefault,amillisecondbutcanbeanything);itisreseteverytimethetickchanges.
SystemClockDependency
WerecommendyouuseNTPtokeepyoursystemclockaccurate.IdGenprotectsfromnon-monotonicclocks,i.e.clocksthatrunbackwards.TheDefaultTimeSourcereliesona64bitmonotonic,increasingonly,systemcounter.However,westillrecommendyouuseNTPtokeepyoursystemclockaccurate;thiswillpreventduplicateId'sbetweensystemrestartsforexample.
TheDefaultTimeSourcereliesonaStopwatchforcalculatingthe'ticks'butyoucanimplementyourowntimesourcebysimplyimplementingtheITimeSourceinterface.
Gettingstarted
InstalltheNugetpackageandwritethefollowingcode:
usingIdGen;
usingSystem.Linq;
classProgram
{
staticvoidMain(string[]args)
{
vargenerator=newIdGenerator(0);
varid=generator.CreateId();
//Exampleid:862817670527975424
}
}
Voila.YouhavecreatedyourfirstId!Wanttocreate100Id's?Insteadof:
varid=generator.CreateId();
write:
varid=generator.Take(100);
ThisisbecausetheIdGenerator()implementsIEnumerableprovidingyouwithanever-endingstreamofId's(soyoumightwanttobecarefuldoinga.Select(...)orCount()onit!).
TheaboveexamplecreatesadefaultIdGeneratorwiththeGeneratorId(or:'WorkerId')setto0andusingaDefaultTimeSource.Ifyou'reusingmultiplegenerators(acrossmachinesorinseparatethreadsor...)you'llwanttomakesureeachgeneratorisassignedit'sownuniqueId.Onewayofdoingthisisbysimplystoringavalueinyourconfigurationfileforexample,anotherwaymayinvolveaservicehandingoutGeneratorId'stomachines/threads.IdGendoesnotprovideasolutionforthissinceeachprojectorsetupmayhavedifferentrequirementsorinfrastructuretoprovidethesegenerator-id's.
Thebelowsampleisabitmorecomplicated;wesetacustomepoch,defineourownid-structureforgeneratedId'sandthendisplaysomeinformationaboutthesetup:
usingIdGen;
usingSystem;
classProgram
{
staticvoidMain(string[]args)
{
//Let'ssaywetakeapril1st2020asourepoch
varepoch=newDateTime(2020,4,1,0,0,0,DateTimeKind.Utc);
//CreateanIDwith45bitsfortimestamp,2forgenerator-id
//and16forsequence
varstructure=newIdStructure(45,2,16);
//Prepareoptions
varoptions=newIdGeneratorOptions(structure,newDefaultTimeSource(epoch));
//CreateanIdGeneratorwithit'sgenerator-idsetto0,ourcustomepoch
//andid-structure
vargenerator=newIdGenerator(0,options);
//Let'sasktheid-structurehowmanygeneratorswecouldinstantiate
//inthissetup(2bits)
Console.WriteLine("Max.generators:{0}",structure.MaxGenerators);
//Let'sasktheid-structurehowmanysequentialId'swecouldgenerate
//inasinglemsinthissetup(16bits)
Console.WriteLine("Id's/mspergenerator:{0}",structure.MaxSequenceIds);
//Let'scalculatethenumberofId'swecouldgenerate,perms,shouldweuse
//themaximumnumberofgenerators
Console.WriteLine("Id's/mstotal:{0}",structure.MaxGenerators*structure.MaxSequenceIds);
//Let'sasktheid-structureconfigurationforhowlongwecouldgenerateId'sbefore
//weexperiencea'wraparound'ofthetimestamp
Console.WriteLine("Wraparoundinterval:{0}",structure.WraparoundInterval(generator.Options.TimeSource));
//Andfinally:let'sasktheid-structurewhenthiswraparoundwillhappen
//(we'llhavetotellitthegenerator'sepoch)
Console.WriteLine("Wraparounddate:{0}",structure.WraparoundDate(generator.Options.TimeSource.Epoch,generator.Options.TimeSource).ToString("O"));
}
}
Output:
Max.generators:4
Id's/mspergenerator:65536
Id's/mstotal:262144
Wraparoundinterval:407226.12:41:28.8320000(about1114years)
Wraparounddate:3135-03-14T12:41:28.8320000+00:00
IdGenalsoprovidesanITimeSouceinterface;thiscanbehandyforunittestingpurposesorifyouwanttoprovideatime-sourceforthetimestamppartofyourId'sthatisnotbasedonthesystemtime.ForunittestingweuseourownMockTimeSource.
延伸文章資訊
- 1godruoyi/go-snowflake: An Lock Free ID Generator ... - GitHub
An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced). - GitHub -...
- 2Snowflake ID - Wikipedia
- 3RobThree/IdGen: Twitter Snowflake-alike ID generator for .Net
Twitter Snowflake-alike ID generator for .Net. Contribute to RobThree/IdGen development by creati...
- 4Twitter's Snowflake: A distributed time-based unique ID ...
Twitter's Snowflake: A distributed time-based unique ID generator - GitHub - jtejido/snowflake: T...
- 5Snowflake algorithm (improved version) - Programmer Group