diff --git a/.gitignore b/.gitignore index 1e8572c..acb6a62 100644 --- a/.gitignore +++ b/.gitignore @@ -252,6 +252,7 @@ paket-files/ # JetBrains Rider .idea/ *.sln.iml +#target/ # macOS diff --git a/C/README.md b/C/README.md index 4b7ea56..d3af0b1 100644 --- a/C/README.md +++ b/C/README.md @@ -1,8 +1,8 @@ # idgenerator -## 编译说明 +## 缂栬瘧璇存槑 -1.默认是 Linux 环境,用 CMake。 +1.榛樿鏄 Linux 鐜锛岀敤 CMake銆 -2.如果是 Windows 环境,要用 Cygwin 或 MinGW。 +2.濡傛灉鏄 Windows 鐜锛岃鐢 Cygwin 鎴 MinGW銆 diff --git a/Go/README.md b/Go/README.md index bde43ae..b1c605a 100644 --- a/Go/README.md +++ b/Go/README.md @@ -1,35 +1,43 @@ -# idgenerator - -## - -Go集成专项工程入口:https://gitee.com/yitter/idgenerator-go - -后文内容以 Go 专项工程为准。 - -## Go环境 - -1.SDK,go1.16 - -2.启用 Go-Modules - -``` -go env -w GO111MODULE=on -go env -w GOPROXY=https://goproxy.cn,https://goproxy.io,direct -``` - - -## Go代码示例 -``` -var yid = idgen.YitIdHelper{} -fmt.Println(yid.NextId()) - -// 方法二:自定义参数 -var options = contract.NewIdGeneratorOptions(1) -//options.WorkerIdBitLength = 6 -//options.SeqBitLength = 6 -//options.TopOverCostCount = 2000 -//options.BaseTime = time.Date(2020, 2, 20, 2, 20, 2, 20, time.UTC).UnixNano() / 1e6 -yid.SetIdGenerator(options) - -``` - +# 鉂勶笍 idenerator-go + +## 浠嬬粛 +椤圭洰鏇村浠嬬粛鍙傜収锛歨ttps://github.com/yitter/idgenerator + +## Go鐜 + +1.SDK锛実o1.14 + +2.鍚敤 Go-Modules + +``` +go env -w GO111MODULE=on +go env -w GOPROXY=https://goproxy.cn,https://goproxy.io,direct +``` + +3. 瀹夎鏂瑰紡 +``` + go get -u -v github.com/yitter/idgenerator-go +``` +鎴 go.mod 涓坊鍔犲紩鐢 +``` +require github.com/yitter/idgenerator-go v1.2.0 +``` + +## Go浠g爜绀轰緥 +``` + +// 瀹氫箟鍙傛暟 +var options = idgen.NewIdGeneratorOptions(1) +options.WorkerId = 1 +options.WorkerIdBitLength = 6 +options.SeqBitLength = 6 +// ... +idgen.SetIdGenerator(options) + +// 璋冪敤鏂规硶鐢熸垚Id +var id = idgen.NextId() + +``` + +## 浠g爜璐$尞鑰(鎸夋椂闂撮『搴) +guoyahao | amuluowin | houseme diff --git a/Go/source/go.mod b/Go/source/go.mod index c1c9b3c..6a111a2 100644 --- a/Go/source/go.mod +++ b/Go/source/go.mod @@ -1,6 +1,6 @@ module yitidgen -go 1.16 +go 1.14 require ( github.com/go-redis/redis v6.15.9+incompatible diff --git a/Go/source/idgen/DefaultIdGenerator.go b/Go/source/idgen/DefaultIdGenerator.go index 5176980..a185edd 100644 --- a/Go/source/idgen/DefaultIdGenerator.go +++ b/Go/source/idgen/DefaultIdGenerator.go @@ -7,18 +7,17 @@ package idgen import ( + "strconv" "time" - "yitidgen/contract" - "yitidgen/core" ) type DefaultIdGenerator struct { - Options *contract.IdGeneratorOptions - SnowWorker contract.ISnowWorker - IdGeneratorException contract.IdGeneratorException + Options *IdGeneratorOptions + SnowWorker ISnowWorker + IdGeneratorException IdGeneratorException } -func NewDefaultIdGenerator(options *contract.IdGeneratorOptions) *DefaultIdGenerator { +func NewDefaultIdGenerator(options *IdGeneratorOptions) *DefaultIdGenerator { if options == nil { panic("dig.Options error.") } @@ -32,9 +31,9 @@ func NewDefaultIdGenerator(options *contract.IdGeneratorOptions) *DefaultIdGener panic("error锛歐orkerIdBitLength + SeqBitLength <= 22") } - maxWorkerIdNumber := uint16(1< maxWorkerIdNumber { - panic("WorkerId error. (range:[1, " + string(maxWorkerIdNumber) + "]") + maxWorkerIDNumber := uint16(1< maxWorkerIDNumber { + panic("WorkerId error. (range:[1, " + strconv.FormatUint(uint64(maxWorkerIDNumber), 10) + "]") } if options.SeqBitLength < 2 || options.SeqBitLength > 21 { @@ -43,22 +42,23 @@ func NewDefaultIdGenerator(options *contract.IdGeneratorOptions) *DefaultIdGener maxSeqNumber := uint32(1< maxSeqNumber { - panic("MaxSeqNumber error. (range:[1, " + string(maxSeqNumber) + "]") + panic("MaxSeqNumber error. (range:[1, " + strconv.FormatUint(uint64(maxSeqNumber), 10) + "]") } if options.MinSeqNumber > maxSeqNumber { - panic("MinSeqNumber error. (range:[1, " + string(maxSeqNumber) + "]") + panic("MinSeqNumber error. (range:[1, " + strconv.FormatUint(uint64(maxSeqNumber), 10) + "]") } - var snowWorker contract.ISnowWorker + + var snowWorker ISnowWorker switch options.Method { case 1: - snowWorker = core.NewSnowWorkerM1(options) + snowWorker = NewSnowWorkerM1(options) case 2: - snowWorker = core.NewSnowWorkerM2(options) + snowWorker = NewSnowWorkerM2(options) default: - snowWorker = core.NewSnowWorkerM1(options) + snowWorker = NewSnowWorkerM1(options) } if options.Method == 1 { diff --git a/Go/source/contract/IIdGenerator.go b/Go/source/idgen/IIdGenerator.go similarity index 92% rename from Go/source/contract/IIdGenerator.go rename to Go/source/idgen/IIdGenerator.go index 48c346f..eab4e8d 100644 --- a/Go/source/contract/IIdGenerator.go +++ b/Go/source/idgen/IIdGenerator.go @@ -4,7 +4,7 @@ * 浠g爜淇锛歽itter * 寮婧愬湴鍧锛歨ttps://gitee.com/yitter/idgenerator */ -package contract +package idgen type IIdGenerator interface { NewLong() uint64 diff --git a/Go/source/contract/ISnowWorker.go b/Go/source/idgen/ISnowWorker.go similarity index 92% rename from Go/source/contract/ISnowWorker.go rename to Go/source/idgen/ISnowWorker.go index 5a98ad9..1e15cc2 100644 --- a/Go/source/contract/ISnowWorker.go +++ b/Go/source/idgen/ISnowWorker.go @@ -4,7 +4,7 @@ * 浠g爜淇锛歽itter * 寮婧愬湴鍧锛歨ttps://gitee.com/yitter/idgenerator */ -package contract +package idgen type ISnowWorker interface { NextId() uint64 diff --git a/Go/source/contract/IdGeneratorException.go b/Go/source/idgen/IdGeneratorException.go similarity index 95% rename from Go/source/contract/IdGeneratorException.go rename to Go/source/idgen/IdGeneratorException.go index 9b3cf05..a3aa378 100644 --- a/Go/source/contract/IdGeneratorException.go +++ b/Go/source/idgen/IdGeneratorException.go @@ -4,7 +4,7 @@ * 浠g爜淇锛歽itter * 寮婧愬湴鍧锛歨ttps://gitee.com/yitter/idgenerator */ -package contract +package idgen import "fmt" diff --git a/Go/source/contract/IdGeneratorOptions.go b/Go/source/idgen/IdGeneratorOptions.go similarity index 98% rename from Go/source/contract/IdGeneratorOptions.go rename to Go/source/idgen/IdGeneratorOptions.go index bd2afc2..b76ba17 100644 --- a/Go/source/contract/IdGeneratorOptions.go +++ b/Go/source/idgen/IdGeneratorOptions.go @@ -4,7 +4,7 @@ * 浠g爜淇锛歽itter * 寮婧愬湴鍧锛歨ttps://gitee.com/yitter/idgenerator */ -package contract +package idgen type IdGeneratorOptions struct { Method uint16 // 闆姳璁$畻鏂规硶,锛1-婕傜Щ绠楁硶|2-浼犵粺绠楁硶锛夛紝榛樿1 diff --git a/Go/source/contract/OverCostActionArg.go b/Go/source/idgen/OverCostActionArg.go similarity index 97% rename from Go/source/contract/OverCostActionArg.go rename to Go/source/idgen/OverCostActionArg.go index e8bbeb1..82714e1 100644 --- a/Go/source/contract/OverCostActionArg.go +++ b/Go/source/idgen/OverCostActionArg.go @@ -4,7 +4,7 @@ * 浠g爜淇锛歽itter * 寮婧愬湴鍧锛歨ttps://gitee.com/yitter/idgenerator */ -package contract +package idgen type OverCostActionArg struct { ActionType int32 diff --git a/Go/source/core/snowWorkerM1.go b/Go/source/idgen/SnowWorkerM1.go similarity index 93% rename from Go/source/core/snowWorkerM1.go rename to Go/source/idgen/SnowWorkerM1.go index b5fc7db..36fd533 100644 --- a/Go/source/core/snowWorkerM1.go +++ b/Go/source/idgen/SnowWorkerM1.go @@ -4,14 +4,14 @@ * 浠g爜淇锛歽itter * 寮婧愬湴鍧锛歨ttps://gitee.com/yitter/idgenerator */ -package core +package idgen import ( "sync" "time" - "yitidgen/contract" ) +// SnowWorkerM1 . type SnowWorkerM1 struct { BaseTime int64 //鍩虹鏃堕棿 WorkerId uint16 //鏈哄櫒鐮 @@ -32,7 +32,8 @@ type SnowWorkerM1 struct { sync.Mutex } -func NewSnowWorkerM1(options *contract.IdGeneratorOptions) contract.ISnowWorker { +// NewSnowWorkerM1 . +func NewSnowWorkerM1(options *IdGeneratorOptions) ISnowWorker { var workerIdBitLength byte var seqBitLength byte var maxSeqNumber uint32 @@ -63,8 +64,10 @@ func NewSnowWorkerM1(options *contract.IdGeneratorOptions) contract.ISnowWorker } else { baseTime = 1582136402000 } + timestampShift := (byte)(options.WorkerIdBitLength + options.SeqBitLength) currentSeqNumber := options.MinSeqNumber + return &SnowWorkerM1{ BaseTime: baseTime, WorkerId: workerId, @@ -77,7 +80,8 @@ func NewSnowWorkerM1(options *contract.IdGeneratorOptions) contract.ISnowWorker _CurrentSeqNumber: currentSeqNumber} } -func (m1 *SnowWorkerM1) DoGenIdAction(arg *contract.OverCostActionArg) { +// DoGenIDAction . +func (m1 *SnowWorkerM1) DoGenIdAction(arg *OverCostActionArg) { } @@ -128,10 +132,12 @@ func (m1 *SnowWorkerM1) NextOverCostId() uint64 { return m1.CalcId(m1._LastTimeTick) } + m1._GenCountInOneTerm++ return m1.CalcId(m1._LastTimeTick) } +// NextNormalID . func (m1 *SnowWorkerM1) NextNormalId() uint64 { currentTimeTick := m1.GetCurrentTimeTick() if currentTimeTick < m1._LastTimeTick { @@ -145,7 +151,8 @@ func (m1 *SnowWorkerM1) NextNormalId() uint64 { } m1.BeginTurnBackAction(m1._TurnBackTimeTick) } - time.Sleep(time.Duration(10) * time.Millisecond) + + time.Sleep(time.Duration(1) * time.Millisecond) return m1.CalcTurnBackId(m1._TurnBackTimeTick) } // 鏃堕棿杩藉钩鏃讹紝_TurnBackTimeTick娓呴浂 @@ -169,26 +176,31 @@ func (m1 *SnowWorkerM1) NextNormalId() uint64 { return m1.CalcId(m1._LastTimeTick) } + return m1.CalcId(m1._LastTimeTick) } +// CalcID . func (m1 *SnowWorkerM1) CalcId(useTimeTick int64) uint64 { result := uint64(useTimeTick< /* for ptrdiff_t below */ - -#ifndef GO_CGO_EXPORT_PROLOGUE_H -#define GO_CGO_EXPORT_PROLOGUE_H - -#ifndef GO_CGO_GOSTRING_TYPEDEF -typedef struct { const char *p; ptrdiff_t n; } _GoString_; -#endif - -#endif - -/* Start of preamble from import "C" comments. */ - - - - -/* End of preamble from import "C" comments. */ - - -/* Start of boilerplate cgo prologue. */ -#line 1 "cgo-gcc-export-header-prolog" - -#ifndef GO_CGO_PROLOGUE_H -#define GO_CGO_PROLOGUE_H - -typedef signed char GoInt8; -typedef unsigned char GoUint8; -typedef short GoInt16; -typedef unsigned short GoUint16; -typedef int GoInt32; -typedef unsigned int GoUint32; -typedef long long GoInt64; -typedef unsigned long long GoUint64; -typedef GoInt64 GoInt; -typedef GoUint64 GoUint; -typedef __SIZE_TYPE__ GoUintptr; -typedef float GoFloat32; -typedef double GoFloat64; -typedef float _Complex GoComplex64; -typedef double _Complex GoComplex128; - -/* - static assertion to make sure the file is being used on architecture - at least with matching size of GoInt. -*/ -typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1]; - -#ifndef GO_CGO_GOSTRING_TYPEDEF -typedef _GoString_ GoString; -#endif -typedef void *GoMap; -typedef void *GoChan; -typedef struct { void *t; void *v; } GoInterface; -typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; - -#endif - -/* End of boilerplate cgo prologue. */ - -#ifdef __cplusplus -extern "C" { -#endif - -extern __declspec(dllexport) GoUint64 NextId(); - -#ifdef __cplusplus -} -#endif diff --git a/Go/source/target/yitidgen.dll b/Go/source/target/yitidgen.dll deleted file mode 100644 index 2f42ebe..0000000 Binary files a/Go/source/target/yitidgen.dll and /dev/null differ diff --git a/Go/source/target/yitidgen.h b/Go/source/target/yitidgen.h deleted file mode 100644 index 8eb543c..0000000 --- a/Go/source/target/yitidgen.h +++ /dev/null @@ -1,75 +0,0 @@ -/* Code generated by cmd/cgo; DO NOT EDIT. */ - -/* package command-line-arguments */ - - -#line 1 "cgo-builtin-export-prolog" - -#include /* for ptrdiff_t below */ - -#ifndef GO_CGO_EXPORT_PROLOGUE_H -#define GO_CGO_EXPORT_PROLOGUE_H - -#ifndef GO_CGO_GOSTRING_TYPEDEF -typedef struct { const char *p; ptrdiff_t n; } _GoString_; -#endif - -#endif - -/* Start of preamble from import "C" comments. */ - - - - -/* End of preamble from import "C" comments. */ - - -/* Start of boilerplate cgo prologue. */ -#line 1 "cgo-gcc-export-header-prolog" - -#ifndef GO_CGO_PROLOGUE_H -#define GO_CGO_PROLOGUE_H - -typedef signed char GoInt8; -typedef unsigned char GoUint8; -typedef short GoInt16; -typedef unsigned short GoUint16; -typedef int GoInt32; -typedef unsigned int GoUint32; -typedef long long GoInt64; -typedef unsigned long long GoUint64; -typedef GoInt64 GoInt; -typedef GoUint64 GoUint; -typedef __SIZE_TYPE__ GoUintptr; -typedef float GoFloat32; -typedef double GoFloat64; -typedef float _Complex GoComplex64; -typedef double _Complex GoComplex128; - -/* - static assertion to make sure the file is being used on architecture - at least with matching size of GoInt. -*/ -typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1]; - -#ifndef GO_CGO_GOSTRING_TYPEDEF -typedef _GoString_ GoString; -#endif -typedef void *GoMap; -typedef void *GoChan; -typedef struct { void *t; void *v; } GoInterface; -typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; - -#endif - -/* End of boilerplate cgo prologue. */ - -#ifdef __cplusplus -extern "C" { -#endif - -extern __declspec(dllexport) GoUint64 NextId(); - -#ifdef __cplusplus -} -#endif diff --git a/Go/source/target/yitidgengo.dll b/Go/source/target/yitidgengo.dll deleted file mode 100644 index d9468a4..0000000 Binary files a/Go/source/target/yitidgengo.dll and /dev/null differ diff --git a/Go/source/target/yitidgengo.h b/Go/source/target/yitidgengo.h deleted file mode 100644 index 950edd6..0000000 --- a/Go/source/target/yitidgengo.h +++ /dev/null @@ -1,83 +0,0 @@ -/* Code generated by cmd/cgo; DO NOT EDIT. */ - -/* package command-line-arguments */ - - -#line 1 "cgo-builtin-export-prolog" - -#include /* for ptrdiff_t below */ - -#ifndef GO_CGO_EXPORT_PROLOGUE_H -#define GO_CGO_EXPORT_PROLOGUE_H - -#ifndef GO_CGO_GOSTRING_TYPEDEF -typedef struct { const char *p; ptrdiff_t n; } _GoString_; -#endif - -#endif - -/* Start of preamble from import "C" comments. */ - - - - -/* End of preamble from import "C" comments. */ - - -/* Start of boilerplate cgo prologue. */ -#line 1 "cgo-gcc-export-header-prolog" - -#ifndef GO_CGO_PROLOGUE_H -#define GO_CGO_PROLOGUE_H - -typedef signed char GoInt8; -typedef unsigned char GoUint8; -typedef short GoInt16; -typedef unsigned short GoUint16; -typedef int GoInt32; -typedef unsigned int GoUint32; -typedef long long GoInt64; -typedef unsigned long long GoUint64; -typedef GoInt64 GoInt; -typedef GoUint64 GoUint; -typedef __SIZE_TYPE__ GoUintptr; -typedef float GoFloat32; -typedef double GoFloat64; -typedef float _Complex GoComplex64; -typedef double _Complex GoComplex128; - -/* - static assertion to make sure the file is being used on architecture - at least with matching size of GoInt. -*/ -typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1]; - -#ifndef GO_CGO_GOSTRING_TYPEDEF -typedef _GoString_ GoString; -#endif -typedef void *GoMap; -typedef void *GoChan; -typedef struct { void *t; void *v; } GoInterface; -typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; - -#endif - -/* End of boilerplate cgo prologue. */ - -#ifdef __cplusplus -extern "C" { -#endif - - -// 娉ㄥ唽涓涓柊鐨刉orkerId -extern __declspec(dllexport) GoInt RegisterWorkerId(char* ip, GoInt port, char* password, GoInt maxWorkerId); - -// 娉ㄩ攢WorkerId -extern __declspec(dllexport) void UnRegisterWorkerId(); - -// 妫鏌ユ湰鍦癢orkerId鏄惁鏈夋晥锛0-鏈夋晥锛屽叾瀹-鏃犳晥锛 -extern __declspec(dllexport) GoInt ValidateLocalWorkerId(GoInt workerId); - -#ifdef __cplusplus -} -#endif diff --git a/README.md b/README.md index 0bd3255..0059e7f 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ## 杞浇鍙婄増鏉冨0鏄 - 鏈汉浠庢湭鍦ㄥ崥瀹㈠洯涔嬪鐨勭綉绔欙紝鍙戣〃杩囧叧浜庢湰绠楁硶鐨勯暱鏂囷紝鍏跺畠缃戠珯涓婂瓨鍦ㄧ殑浠嬬粛鏂囩珷锛屽潎灞炰粬浜烘嫹璐濅箣浣溿 + 鏈汉浠庢湭鍦ㄥ崥瀹㈠洯涔嬪鐨勭綉绔欙紝鍙戣〃杩囨湰绠楁硶闀挎枃锛屽叾瀹冪綉绔欐墍鐜版枃绔狅紝鍧囧睘浠栦汉鎷疯礉涔嬩綔銆 鎵鏈夋嫹璐濅箣浣滐紝鍧囬』淇濈暀椤圭洰寮婧愰摼鎺ワ紝鍚﹀垯绂佹杞浇銆