Browse Source

recodeGo

tags/v1.0.0
zhouzj 4 years ago
parent
commit
1f5cc01168
3 changed files with 42 additions and 38 deletions
  1. +4
    -0
      Go/README.md
  2. +22
    -22
      Go/source/core/snowWorkerM1.go
  3. +16
    -16
      Go/source/idgen/DefaultIdGenerator.go

+ 4
- 0
Go/README.md View File

@@ -5,6 +5,10 @@
1.go 1.16 1.go 1.16


2. 启用Go-Modules 2. 启用Go-Modules
```
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,https://goproxy.io,direct
```




## Go代码示例 ## Go代码示例


+ 22
- 22
Go/source/core/snowWorkerM1.go View File

@@ -34,46 +34,46 @@ type SnowWorkerM1 struct {
} }


func NewSnowWorkerM1(options *contract.IdGeneratorOptions) contract.ISnowWorker { func NewSnowWorkerM1(options *contract.IdGeneratorOptions) contract.ISnowWorker {
var WorkerIdBitLength byte
var SeqBitLength byte
var MaxSeqNumber uint32
var workerIdBitLength byte
var seqBitLength byte
var maxSeqNumber uint32


var WorkerId = options.WorkerId
var workerId = options.WorkerId


if options.WorkerIdBitLength == 0 { if options.WorkerIdBitLength == 0 {
WorkerIdBitLength = 6
workerIdBitLength = 6
} else { } else {
WorkerIdBitLength = options.WorkerIdBitLength
workerIdBitLength = options.WorkerIdBitLength
} }
if options.SeqBitLength == 0 { if options.SeqBitLength == 0 {
SeqBitLength = 6
seqBitLength = 6
} else { } else {
SeqBitLength = options.SeqBitLength
seqBitLength = options.SeqBitLength
} }
if options.MaxSeqNumber > 0 { if options.MaxSeqNumber > 0 {
MaxSeqNumber = options.MaxSeqNumber
maxSeqNumber = options.MaxSeqNumber
} else { } else {
MaxSeqNumber = uint32(math.Pow(2, float64(options.SeqBitLength))) - 1
maxSeqNumber = uint32(math.Pow(2, float64(options.SeqBitLength))) - 1
} }
var MinSeqNumber = options.MinSeqNumber
var TopOverCostCount = options.TopOverCostCount
var minSeqNumber = options.MinSeqNumber
var topOverCostCount = options.TopOverCostCount


var BaseTime int64
var baseTime int64
if options.BaseTime != 0 { if options.BaseTime != 0 {
BaseTime = options.BaseTime
baseTime = options.BaseTime
} else { } else {
BaseTime = 1582136402000
baseTime = 1582136402000
} }
timestampShift := (byte)(options.WorkerIdBitLength + options.SeqBitLength) timestampShift := (byte)(options.WorkerIdBitLength + options.SeqBitLength)
currentSeqNumber := options.MinSeqNumber currentSeqNumber := options.MinSeqNumber
return &SnowWorkerM1{ return &SnowWorkerM1{
BaseTime: BaseTime,
WorkerId: WorkerId,
WorkerIdBitLength: WorkerIdBitLength,
SeqBitLength: SeqBitLength,
MaxSeqNumber: MaxSeqNumber,
MinSeqNumber: MinSeqNumber,
TopOverCostCount: TopOverCostCount,
BaseTime: baseTime,
WorkerId: workerId,
WorkerIdBitLength: workerIdBitLength,
SeqBitLength: seqBitLength,
MaxSeqNumber: maxSeqNumber,
MinSeqNumber: minSeqNumber,
TopOverCostCount: topOverCostCount,
_TimestampShift: timestampShift, _TimestampShift: timestampShift,
_CurrentSeqNumber: currentSeqNumber} _CurrentSeqNumber: currentSeqNumber}
} }


+ 16
- 16
Go/source/idgen/DefaultIdGenerator.go View File

@@ -19,54 +19,54 @@ type DefaultIdGenerator struct {
IdGeneratorException contract.IdGeneratorException IdGeneratorException contract.IdGeneratorException
} }


func NewDefaultIdGenerator(Options *contract.IdGeneratorOptions) *DefaultIdGenerator {
if Options == nil {
func NewDefaultIdGenerator(options *contract.IdGeneratorOptions) *DefaultIdGenerator {
if options == nil {
panic("dig.Options error.") panic("dig.Options error.")
} }


var minTime = time.Now().AddDate(-50, 0, 0).UnixNano() / 1e6 var minTime = time.Now().AddDate(-50, 0, 0).UnixNano() / 1e6
if minTime == 0 || Options.BaseTime < minTime || Options.BaseTime > time.Now().UnixNano()/1e6 {
if minTime == 0 || options.BaseTime < minTime || options.BaseTime > time.Now().UnixNano()/1e6 {
panic("BaseTime error.") panic("BaseTime error.")
} }


if Options.SeqBitLength+Options.WorkerIdBitLength > 22 {
if options.SeqBitLength+options.WorkerIdBitLength > 22 {
panic("error:WorkerIdBitLength + SeqBitLength <= 22") panic("error:WorkerIdBitLength + SeqBitLength <= 22")
} }


maxWorkerIdNumber := uint16(math.Pow(float64(2), float64(Options.WorkerIdBitLength))) - 1
if Options.WorkerId > maxWorkerIdNumber {
maxWorkerIdNumber := uint16(math.Pow(float64(2), float64(options.WorkerIdBitLength))) - 1
if options.WorkerId > maxWorkerIdNumber {
panic("WorkerId error. (range:[1, "+ string(maxWorkerIdNumber)+ "]") panic("WorkerId error. (range:[1, "+ string(maxWorkerIdNumber)+ "]")
} }


if Options.SeqBitLength < 2 || Options.SeqBitLength > 21 {
if options.SeqBitLength < 2 || options.SeqBitLength > 21 {
panic("SeqBitLength error. (range:[2, 21])") panic("SeqBitLength error. (range:[2, 21])")
} }


maxSeqNumber := uint32(math.Pow(2, float64(Options.SeqBitLength))) - 1
if Options.MaxSeqNumber > maxSeqNumber {
maxSeqNumber := uint32(math.Pow(2, float64(options.SeqBitLength))) - 1
if options.MaxSeqNumber > maxSeqNumber {
panic("MaxSeqNumber error. (range:[1, "+ string(maxSeqNumber)+ "]") panic("MaxSeqNumber error. (range:[1, "+ string(maxSeqNumber)+ "]")
} }


if Options.MinSeqNumber > maxSeqNumber {
if options.MinSeqNumber > maxSeqNumber {
panic("MinSeqNumber error. (range:[1, "+ string(maxSeqNumber)+ "]") panic("MinSeqNumber error. (range:[1, "+ string(maxSeqNumber)+ "]")
} }


var snowWorker contract.ISnowWorker var snowWorker contract.ISnowWorker


switch Options.Method {
switch options.Method {
case 1: case 1:
snowWorker = core.NewSnowWorkerM1(Options)
snowWorker = core.NewSnowWorkerM1(options)
case 2: case 2:
snowWorker = core.NewSnowWorkerM2(Options)
snowWorker = core.NewSnowWorkerM2(options)
default: default:
snowWorker = core.NewSnowWorkerM1(Options)
snowWorker = core.NewSnowWorkerM1(options)
} }


if Options.Method == 1 {
if options.Method == 1 {
time.Sleep(time.Duration(500) * time.Microsecond) time.Sleep(time.Duration(500) * time.Microsecond)
} }
return &DefaultIdGenerator{ return &DefaultIdGenerator{
Options: Options,
Options: options,
SnowWorker: snowWorker, SnowWorker: snowWorker,
} }
} }


Loading…
Cancel
Save