Skip to content

Commit

Permalink
auto commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouzj committed Mar 16, 2023
1 parent 9502447 commit 5b44aa6
Show file tree
Hide file tree
Showing 25 changed files with 184 additions and 1,956 deletions.
1 change: 1 addition & 0 deletions .dccache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"c:/project/_project1/Yitter.IdGenerator/GitHub.IdGen.go/main.go":[1952,1678950385570.279,"27308dd87e048d51a3b1ef0da09046f0dcbbcd30a326ebc199c34da73989a8bf"],"c:/project/_project1/Yitter.IdGenerator/GitHub.IdGen.go/reg.go":[2638,1678950429101.9556,"31a018d8d099a9c68a806c80a46d5125a466a3bef09dbe5aa0ad59273d7fcd06"],"c:/project/_project1/Yitter.IdGenerator/GitHub.IdGen.go/idgen/DefaultIdGenerator.go":[2625,1678948712618.3582,"95670f254be1b92f737df2f390117b9fc6de200e476507acd1396b8c02553e8f"],"c:/project/_project1/Yitter.IdGenerator/GitHub.IdGen.go/idgen/IdGeneratorException.go":[404,1657434368000,"ee0ab19bdfb44d03284fcce23a619e9c6f628c5585e75bc6f865ea62a0e03676"],"c:/project/_project1/Yitter.IdGenerator/GitHub.IdGen.go/idgen/IdGeneratorOptions.go":[1601,1657434368000,"301827da63c9765ca3cbd34cb966beb218a5434f4cc0ec0d43a4c1cc06cb5f4d"],"c:/project/_project1/Yitter.IdGenerator/GitHub.IdGen.go/idgen/IIdGenerator.go":[222,1657434368000,"9891fd54b9a4beb8d9d10eb252eb24a227ec14ce9400ad40bbca69b4c245f229"],"c:/project/_project1/Yitter.IdGenerator/GitHub.IdGen.go/idgen/ISnowWorker.go":[219,1657434368000,"02895d7a8e045dc8e40ca7a84e587ab1b5866e43c4a7d997a5092033660619b1"],"c:/project/_project1/Yitter.IdGenerator/GitHub.IdGen.go/idgen/OverCostActionArg.go":[769,1657434368000,"34751899a8bb722ce838098760301ee267fa43d2e03dd025ae66cf669887eea1"],"c:/project/_project1/Yitter.IdGenerator/GitHub.IdGen.go/idgen/SnowWorkerM1.go":[6300,1657434368000,"caaf30edf6e78ee5bdfa443d828786831f38124108f05348406685298021515a"],"c:/project/_project1/Yitter.IdGenerator/GitHub.IdGen.go/idgen/SnowWorkerM2.go":[1078,1657434368000,"969e1191a68a654d32a91b37b0b1a71e23e550c58344da3a836a0c65845ce8a9"],"c:/project/_project1/Yitter.IdGenerator/GitHub.IdGen.go/idgen/YitIdHelper.go":[799,1657434368000,"ab5150d7fc3269974127c9c65b1dd5aa6856eebcf97f250fd7badd5e33f8db72"],"c:/project/_project1/Yitter.IdGenerator/GitHub.IdGen.go/regworkerid/reghelper.go":[11229,1678949394759.0046,"06f236bdd917aa2884c2141e02398c29bf84bf0bd6266ada93bc6116711b23d1"]}
9 changes: 4 additions & 5 deletions idgen/SnowWorkerM1.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,12 @@ func NewSnowWorkerM1(options *IdGeneratorOptions) ISnowWorker {
// 6.MinSeqNumber
var minSeqNumber = options.MinSeqNumber

// 7.TopOverCostCount
// 7.Others
var topOverCostCount = options.TopOverCostCount
// if topOverCostCount == 0 {
// topOverCostCount = 2000
// }
if topOverCostCount == 0 {
topOverCostCount = 2000
}

// 8.Others
timestampShift := (byte)(workerIdBitLength + seqBitLength)
currentSeqNumber := minSeqNumber

Expand Down
29 changes: 14 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,30 @@ func main() {
idgen.SetIdGenerator(options)

var genCount = 500000
for j := 0; j < 100000; j++ {
for {
var begin = time.Now().UnixNano() / 1e6
for i := 0; i < genCount; i++ {
// 生成ID
idgen.NextId()
// fmt.Println(id)
}
var end = time.Now().UnixNano() / 1e6

fmt.Println("耗时:", (end - begin), "ms")
time.Sleep(time.Duration(1000) * time.Millisecond)
for j := 0; j < 10; j++ {
var begin = time.Now().UnixNano() / 1e6
for i := 0; i < genCount; i++ {
// 生成ID
idgen.NextId()
// fmt.Println(id)
}
var end = time.Now().UnixNano() / 1e6

fmt.Println("耗时:", (end - begin), "ms")
time.Sleep(time.Duration(1000) * time.Millisecond)
}
} else {
// ip := "localhost"
ipChar := C.CString("localhost")
ipAddr := C.CString("localhost:6379")
passChar := C.CString("")
sentinelMasterName := C.CString("")

workerIdList := RegisterMany(ipChar, 6379, passChar, 4, 3, 0)
workerIdList := RegisterMany(ipAddr, passChar, 4, sentinelMasterName, 3, 10, 5, 15)
for _, value := range workerIdList {
fmt.Println("注册的WorkerId:", value)
}

id := RegisterOne(ipChar, 6379, passChar, 4, 0)
id := RegisterOne(ipAddr, passChar, 4, sentinelMasterName, 3, 10, 15)
fmt.Println("注册的WorkerId:", id)

// C.free(unsafe.Pointer(ipChar))
Expand Down
56 changes: 43 additions & 13 deletions reg.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,58 @@ import (
)
import "github.com/yitter/idgenerator-go/regworkerid"

// RegisterOne 注册一个 WorkerId,会先注销所有本机已注册的记录
// address: Redis连接地址,单机模式示例:127.0.0.1:6379,哨兵/集群模式示例:127.0.0.1:26380,127.0.0.1:26381,127.0.0.1:26382
// password: Redis连接密码
// db: Redis指定存储库,示例:1
// sentinelMasterName: Redis 哨兵模式下的服务名称,示例:mymaster,非哨兵模式传入空字符串即可
// maxWorkerId: WorkerId 最大值,示例:63
// minWorkerId: WorkerId 最小值,示例:30
// lifeTimeSeconds: WorkerId缓存时长(秒,3的倍数)
//export RegisterOne
// 注册一个 WorkerId,会先注销所有本机已注册的记录
func RegisterOne(ip *C.char, port int32, password *C.char, maxWorkerId int32, database int) int32 {
return regworkerid.RegisterOne(C.GoString(ip), port, C.GoString(password), maxWorkerId, database)
func RegisterOne(address *C.char, password *C.char, db int, sentinelMasterName *C.char, minWorkerId int32, maxWorkerId int32, lifeTimeSeconds int32) int32 {
return regworkerid.RegisterOne(regworkerid.RegisterConf{
Address: C.GoString(address),
Password: C.GoString(password),
DB: db,
MasterName: C.GoString(sentinelMasterName),
MinWorkerId: minWorkerId,
MaxWorkerId: maxWorkerId,
LifeTimeSeconds: lifeTimeSeconds,
})
}

// RegisterMany 注册多个 WorkerId,会先注销所有本机已注册的记录
// address: Redis连接地址,单机模式示例:127.0.0.1:6379,哨兵/集群模式示例:127.0.0.1:26380,127.0.0.1:26381,127.0.0.1:26382
// password: Redis连接密码
// db: Redis指定存储库,示例:1
// sentinelMasterName: Redis 哨兵模式下的服务名称,示例:mymaster,非哨兵模式传入空字符串即可
// maxWorkerId: WorkerId 最大值,示例:63
// minWorkerId: WorkerId 最小值,示例:30
// totalCount: 获取N个WorkerId,示例:5
// lifeTimeSeconds: WorkerId缓存时长(秒,3的倍数)
//export RegisterMany
func RegisterMany(address *C.char, password *C.char, db int, sentinelMasterName *C.char, minWorkerId int32, maxWorkerId int32, totalCount int32, lifeTimeSeconds int32) []int32 {
return regworkerid.RegisterMany(regworkerid.RegisterConf{
Address: C.GoString(address),
Password: C.GoString(password),
DB: db,
MasterName: C.GoString(sentinelMasterName),
MinWorkerId: minWorkerId,
MaxWorkerId: maxWorkerId,
TotalCount: totalCount,
LifeTimeSeconds: lifeTimeSeconds,
})
}

// UnRegister 注销本机已注册的 WorkerId
//export UnRegister
// 注销本机已注册的 WorkerId
func UnRegister() {
regworkerid.UnRegister()
}

// Validate 检查本地WorkerId是否有效(0-有效,其它-无效)
//export Validate
// 检查本地WorkerId是否有效(0-有效,其它-无效)
func Validate(workerId int32) int32 {
return regworkerid.Validate(workerId)
}

// RegisterMany
// 注册多个 WorkerId,会先注销所有本机已注册的记录
func RegisterMany(ip *C.char, port int32, password *C.char, maxWorkerId, totalCount int32, database int) []int32 {
// return (*C.int)(unsafe.Pointer(&values))
//return regworkerid.RegisterMany(ip, port, password, maxWorkerId, totalCount, database)
return regworkerid.RegisterMany(C.GoString(ip), port, C.GoString(password), maxWorkerId, totalCount, database)
}
Loading

0 comments on commit 5b44aa6

Please sign in to comment.