Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yuanyu90221/go-snowflake-id-service
https://github.com/yuanyu90221/go-snowflake-id-service
Last synced: 16 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/yuanyu90221/go-snowflake-id-service
- Owner: yuanyu90221
- Created: 2023-01-14T15:51:22.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2023-01-16T05:34:32.000Z (about 2 years ago)
- Last Synced: 2024-06-21T18:52:41.018Z (7 months ago)
- Language: Go
- Size: 4.88 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# go-snowflake-id-service
This is a snowflake-id service implemented by golang
## [snowflake id](https://zh.wikipedia.org/zh-tw/%E9%9B%AA%E8%8A%B1%E7%AE%97%E6%B3%95)
A distributed monotone incremented Identifier generate algorithm
## format
| timestamp | shardId | seqId |
|-----------------|----------------|------------|
| timestamp difference in millisecond(41bit) | shardId(10bit)| seqId(12bit)|## functionality
1. in 1 millisecond could generate 2^12 different id
2. if more than 2^12 request in 1 millisecond, this service will block this request
## for atomic operation
use golang mutex lock for generate seqId operation
```go
type Shard struct {
mulock sync.Mutex
epoch int64
lastTime int64
shardId int64
seqId int64
}
```first get current timestamp
check current == lastTime then mean in same millisecond, then increase seqId
if current == lastTime and seqId > seqMax then mean need to wait until next millisecond
current > lastTime mean recount the seqId to 0
```go
func (s *Shard) NextID() int64 {
s.mulock.Lock()
defer s.mulock.Unlock()
now := currentMillisecond()
if s.lastTime == now {
s.seqId++
// seqId over the max , then loop until next millisecond
if s.seqId > seqIdMax {
for now <= s.lastTime {
now = currentMillisecond()
}
}
} else {
s.seqId = 0
s.lastTime = now
}
id := int64((now-s.epoch)<