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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. id := idgen.NextId()
  55. fmt.Println(id)
  56. }
  57. var end = time.Now().UnixNano() / 1e3
  58. fmt.Println(end - begin)
  59. time.Sleep(time.Duration(1000) * time.Millisecond)
  60. }
  61. } else {
  62. workerIdList := regworkerid.RegisterMany("localhost", 6379, "", 4, 3)
  63. for _, value := range workerIdList {
  64. fmt.Println("注册的WorkerId:", value)
  65. }
  66. //var workerId = regworkerid.RegisterOne("localhost", 6379, "", 4)
  67. //fmt.Println("注册的WorkerId:", workerId)
  68. fmt.Println("end")
  69. time.Sleep(time.Duration(300) * time.Second)
  70. }
  71. }
  72. // windows:
  73. // go build -o ./target/yitidgengo.dll -buildmode=c-shared main.go
  74. // linux init: go install -buildmode=shared -linkshared std
  75. // go build -o ./target/yitidgengo.so -buildmode=c-shared main.go