You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.go 2.3 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package main
  2. import (
  3. "C"
  4. "fmt"
  5. "time"
  6. "unsafe"
  7. "yitidgen/idgen"
  8. "yitidgen/regworkerid"
  9. )
  10. //export SetOptions
  11. func SetOptions(workerId uint16) {
  12. var options = idgen.NewIdGeneratorOptions(workerId)
  13. idgen.SetIdGenerator(options)
  14. }
  15. //export NextId
  16. func NextId() int64 {
  17. return idgen.NextId()
  18. }
  19. // 注册一个 WorkerId,会先注销所有本机已注册的记录
  20. //export RegisterOne
  21. func RegisterOne(ip *C.char, port int32, password *C.char, maxWorkerId int32) int32 {
  22. return regworkerid.RegisterOne(C.GoString(ip), port, C.GoString(password), maxWorkerId)
  23. }
  24. // 注册多个 WorkerId,会先注销所有本机已注册的记录
  25. //export RegisterMany
  26. func RegisterMany(ip *C.char, port int32, password *C.char, maxWorkerId int32, totalCount int32) *C.int {
  27. values := regworkerid.RegisterMany(C.GoString(ip), port, C.GoString(password), maxWorkerId, totalCount)
  28. return (*C.int)(unsafe.Pointer(&values))
  29. }
  30. // 注销本机已注册的 WorkerId
  31. //export UnRegister
  32. func UnRegister() {
  33. regworkerid.UnRegister()
  34. }
  35. // 检查本地WorkerId是否有效(0-有效,其它-无效)
  36. //export Validate
  37. func Validate(workerId int32) int32 {
  38. return regworkerid.Validate(workerId)
  39. }
  40. func main() {
  41. const testGenId = true // 测试设置
  42. if testGenId {
  43. // 自定义参数
  44. var options = idgen.NewIdGeneratorOptions(1)
  45. options.WorkerIdBitLength = 6
  46. options.SeqBitLength = 6
  47. options.BaseTime = time.Date(2020, 2, 20, 2, 20, 2, 20, time.UTC).UnixNano() / 1e6
  48. idgen.SetIdGenerator(options)
  49. var genCount = 50000
  50. for {
  51. var begin = time.Now().UnixNano() / 1e3
  52. for i := 0; i < genCount; i++ {
  53. // 生成ID
  54. idgen.NextId()
  55. }
  56. var end = time.Now().UnixNano() / 1e3
  57. fmt.Println(end - begin)
  58. time.Sleep(time.Duration(1000) * time.Millisecond)
  59. }
  60. } else {
  61. workerIdList := regworkerid.RegisterMany("localhost", 6379, "", 4, 3)
  62. for _, value := range workerIdList {
  63. fmt.Println("注册的WorkerId:", value)
  64. }
  65. //var workerId = regworkerid.RegisterOne("localhost", 6379, "", 4)
  66. //fmt.Println("注册的WorkerId:", workerId)
  67. fmt.Println("end")
  68. time.Sleep(time.Duration(300) * time.Second)
  69. }
  70. }
  71. // windows: go build -o ./target/yitidgengo.dll -buildmode=c-shared main.go
  72. //// go install -buildmode=shared -linkshared std
  73. //linux: go build -o ./target/yitidgengo.so -buildmode=c-shared main.go

雪花算法中非常好用的数字ID生成器