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.5 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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package main
  2. import (
  3. "C"
  4. "fmt"
  5. "time"
  6. "yitidgen/idgen"
  7. "yitidgen/regworkerid"
  8. )
  9. //export SetOptions
  10. func SetOptions(workerId uint16) {
  11. var options = idgen.NewIdGeneratorOptions(workerId)
  12. idgen.SetIdGenerator(options)
  13. }
  14. //export NextId
  15. func NextId() int64 {
  16. return idgen.NextId()
  17. }
  18. // 注册一个 WorkerId,会先注销所有本机已注册的记录
  19. //export RegisterOne
  20. func RegisterOne(ip *C.char, port int32, password *C.char, maxWorkerId int32) int32 {
  21. return regworkerid.RegisterOne(C.GoString(ip), port, C.GoString(password), maxWorkerId)
  22. }
  23. // 注册多个 WorkerId,会先注销所有本机已注册的记录
  24. ///export RegisterMany
  25. func RegisterMany(ip *C.char, port int32, password *C.char, maxWorkerId int32, totalCount int32) []int32 {
  26. //values := regworkerid.RegisterMany(C.GoString(ip), port, C.GoString(password), maxWorkerId, totalCount)
  27. //return (*C.int)(unsafe.Pointer(&values))
  28. return regworkerid.RegisterMany(C.GoString(ip), port, C.GoString(password), maxWorkerId, totalCount);
  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:
  72. // go build -o ./target/yitidgengo.dll -buildmode=c-shared main.go
  73. // linux init: go install -buildmode=shared -linkshared std
  74. // go build -o ./target/yitidgengo.so -buildmode=c-shared main.go