{"id":20459767,"url":"https://github.com/rosbit/id-generator","last_synced_at":"2025-10-09T16:19:59.221Z","repository":{"id":144202516,"uuid":"186393342","full_name":"rosbit/id-generator","owner":"rosbit","description":"distributed id generators, with a snowflake-like id generator and an auto-incremented id generator. 分布式id生成器，包括snowflake id、自增id、订单id","archived":false,"fork":false,"pushed_at":"2021-04-17T00:39:12.000Z","size":20,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-01-16T00:24:47.379Z","etag":null,"topics":["auto-increment-id","dirstributed","generator","id","orderid","sequence","snowflake","uid"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rosbit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-13T09:57:11.000Z","updated_at":"2024-07-24T09:33:28.000Z","dependencies_parsed_at":"2023-06-18T19:38:58.685Z","dependency_job_id":null,"html_url":"https://github.com/rosbit/id-generator","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rosbit%2Fid-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rosbit%2Fid-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rosbit%2Fid-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rosbit%2Fid-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rosbit","download_url":"https://codeload.github.com/rosbit/id-generator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242014722,"owners_count":20057880,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["auto-increment-id","dirstributed","generator","id","orderid","sequence","snowflake","uid"],"created_at":"2024-11-15T12:17:13.217Z","updated_at":"2025-10-09T16:19:54.188Z","avatar_url":"https://github.com/rosbit.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Distributed unique ID Generator\n\nid-generator is a dirstributed unique ID generator. There are 3 kinds of generators\nprovided by id-generator:\n\n 1. snowflake-like ID generator\n    - inspired by [Twitter's Snowflake](https://blog.twitter.com/2010/announcing-snowflake).\n    - A snowflake-like ID is composed of\n\n        ```\n        31 bits for time in units of 1 second (limit: 2**31/3600/24/365 = 68 years)\n        22 bits for a sequence number         (limit: 2**22-1 = 4,194,303 per second)\n        10 bits for a worker id               (limit: 2**10 = 1024)\n        ```\n\n 1. auto-inremented sequence ID generator\n    - id auto-incremented\n    - sequence ID is composed of\n\n        ```\n        53 bits for a sequence number  (limit: 2**53-1 = 9,007,199,254,740,991)\n        10 bits for a worker id        (limit: 2**10 = 1024)\n        ```\n\n 1. order ID generator\n    - id is of type uint64\n    - as a digit string, its head 6 chars are date with layout \"YYMMDD\"\n    - its tail chars form the workerId specified when initing\n    - the digit of the whole order id is composed of 3 parts\n        - short order id\n\n        ```\n        parts format: YYMMDDxxxxxxxWW\n           YYMMDD  stands for Year, Month, Day.   (upper limit: 991231)\n           xxxxxxx stands for order Id sequence.  (upper limit: 10,000,000 per day)\n           WW      stands for worker id.          (upper limit: 100)\n        ```\n\n        - long order id\n\n        ```\n        parts format: YYMMDDhhmmxxxxxxWW\n           YYMMDDhhmm  stands for Year, Month, Day, Hour, Minute. (upper limit: 9912312359)\n           xxxxxx      stands for order Id sequence.              (upper limit: 1,000,000 per minute)\n           WW          stands for worker id.                      (upper limit: 100)\n        ```\n\n## Installation\n\nThe package is fully go-getable without any dependency, So, just type\n\n   `go get github.com/rosbit/id-generator`\n\n## Usage\n\n```go\npackage main\n\nimport (\n\t\"github.com/rosbit/id-generator\"\n\t\"time\"\n)\n\nfunc main() {\n\t// 1. for snowflake-like ID\n\tepochTime := time.Now().AddDate(0, 0, -1) // set your epochTime, a history time\n\tworkerId := uint16(1) // set your own workerId\n\tsf := idgen.NewFlakeIdGenerator(echochTime, workerId)   // 64bit id\n\t// sf := idgen.NewFlakeIdGenerator32(echochTime, workerId) // 32bit id, most of time, 32bit is ok.\n\tfor i:=0; i\u003c10; i++ {\n\t\tid := sf.NextID()\n\t\t// id\n\t}\n\tsf.Exit()\n\n\t// 2. for order id\n\tord := idgen.NewOrderIdGenerator(workerId, \"Asia/Shanghai\") // any valid tz string is ok\n\tfor i:=0; i\u003c10; i++ {\n\t\tid := ord.NextID()\n\t\t// id\n\t}\n\tord.Exit()\n\n\tord2 := idgen.NewLongOrderIdGenerator(workerId, \"Asia/Shanghai\") // any valid tz string is ok\n\tfor i:=0; i\u003c10; i++ {\n\t\tid := ord2.NextID()\n\t\t// id\n\t}\n\tord2.Exit()\n\n\t// 3. for sequence id\n\tseq := idgen.NewSeqIdGenerator(workerId, 0) // startId, any uint64 is ok\n\tfor i:=0; i\u003c10; i++ {\n\t\tid := seq.NextID()\n\t\t// id\n\t}\n\tseq.Exit()\n}\n```\n\n## Benchmarks\n\nUnder my aliyun-ECS: 1core CPU, Intel(R) Xeon(R) CPU E5-2650 v2 @ 2.60GHz\n\nThe benchmark result is:\n\n```\ngoos: linux\ngoarch: amd64\npkg: github.com/rosbit/id-generator\nBenchmark_orderNextId     \t 3000000\t       479 ns/op\nBenchmark_seqNextId       \t 3000000\t       469 ns/op\nBenchmark_snowFlakeNextId \t 3000000\t       472 ns/op\nPASS\nok  \tgithub.com/rosbit/id-generator\t3.781s\n```\n\n## Contribution\n\nPull requests are welcome! Also, if you want to discuss something,\nsend a pull request with proposal and changes.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frosbit%2Fid-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frosbit%2Fid-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frosbit%2Fid-generator/lists"}