1 Star 1 Fork 0

vincent/gcutil

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
Snowflake.go 3.06 KB
Copy Edit Raw Blame History
vincent authored 2022-02-08 13:44 . feat(): 20220208 年后的更新
package token
import (
"fmt"
"strconv"
"sync"
"time"
)
var defaultIdWorder *IdWorker
func init() {
defaultIdWorder = &IdWorker{}
defaultIdWorder.InitIdWorker(999, 999)
//for i:= 0;i<100;i++ {
// token, _ := DefaultIdWorder.NextId()
// println(fmt.Sprintf("the defaultWorker token[%d]:%#v",i, token))
//}
}
type IdWorker struct {
startTime int64
workerIdBits uint
datacenterIdBits uint
maxWorkerId int64
maxDatacenterId int64
sequenceBits uint
workerIdLeftShift uint
datacenterIdLeftShift uint
timestampLeftShift uint
sequenceMask int64
workerId int64
datacenterId int64
sequence int64
lastTimestamp int64
signMask int64
idLock *sync.Mutex
}
func (this *IdWorker) InitIdWorker(workerId, datacenterId int64) error {
var baseValue int64 = -1
this.startTime = 1577836800000
this.workerIdBits = 5
this.datacenterIdBits = 5
this.maxWorkerId = baseValue ^ (baseValue << this.workerIdBits)
this.maxDatacenterId = baseValue ^ (baseValue << this.datacenterIdBits)
this.sequenceBits = 12
this.workerIdLeftShift = this.sequenceBits
this.datacenterIdLeftShift = this.workerIdBits + this.workerIdLeftShift
this.timestampLeftShift = this.datacenterIdBits + this.datacenterIdLeftShift
this.sequenceMask = baseValue ^ (baseValue << this.sequenceBits)
this.sequence = 0
this.lastTimestamp = -1
this.signMask = ^baseValue + 1
this.idLock = &sync.Mutex{}
if this.workerId < 0 || this.workerId > this.maxWorkerId {
return fmt.Errorf("workerId[%v] is less than 0 or greater than maxWorkerId[%v].", workerId, datacenterId)
}
if this.datacenterId < 0 || this.datacenterId > this.maxDatacenterId {
return fmt.Errorf("datacenterId[%d] is less than 0 or greater than maxDatacenterId[%d].", workerId, datacenterId)
}
this.workerId = workerId
this.datacenterId = datacenterId
return nil
}
func (this *IdWorker) NextId() (int64, error) {
this.idLock.Lock()
timestamp := time.Now().UnixNano()
if timestamp < this.lastTimestamp {
return -1, fmt.Errorf("Clock moved backwards. Refusing to generate id for %d milliseconds", this.lastTimestamp-timestamp)
}
if timestamp == this.lastTimestamp {
this.sequence = (this.sequence + 1) & this.sequenceMask
if this.sequence == 0 {
timestamp = this.tilNextMillis()
this.sequence = 0
}
} else {
this.sequence = 0
}
this.lastTimestamp = timestamp
this.idLock.Unlock()
id := ((timestamp - this.startTime) << this.timestampLeftShift) |
(this.datacenterId << this.datacenterIdLeftShift) |
(this.workerId << this.workerIdLeftShift) |
this.sequence
if id < 0 {
id = -id
}
return id, nil
}
func (this *IdWorker) tilNextMillis() int64 {
timestamp := time.Now().UnixNano()
if timestamp <= this.lastTimestamp {
timestamp = time.Now().UnixNano() / int64(time.Millisecond)
}
return timestamp
}
func NextToken() int64 {
id, err := defaultIdWorder.NextId()
if err != nil {
return 0
} else {
return id
}
}
func NextStrToken() string {
return strconv.FormatInt(NextToken(), 10)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/vincent78/gcutil.git
git@gitee.com:vincent78/gcutil.git
vincent78
gcutil
gcutil
v1.0.1

Search