testlocker.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package main
  2. import (
  3. "strconv"
  4. "sync"
  5. "time"
  6. "git.wecise.com/wecise/odbserver/odb"
  7. "gitee.com/wecisecode/util/logger"
  8. )
  9. // CGO_ENABLED=1 go run --ldflags '-linkmode external -extldflags "-static"' testlocker.go
  10. // #cgo LDFLAGS: -L/opt/odbserver/sqlite -lsqlite -ldl
  11. // #include "/opt/odbserver/sqlite/sqlite.h"
  12. // #include <stdint.h>
  13. // #include <stdlib.h>
  14. // extern int64_t uhaha_seed;
  15. // extern int64_t uhaha_ts;
  16. // void uhaha_begin_reader();
  17. // void uhaha_end_reader();
  18. func main() {
  19. g, err := odb.New(&odb.Option{Cache: odb.CacheAll, Keyspace: "matrix", DisableInitialHostLookup: true}) // For aliyun
  20. //g, err := odb.New(&odb.Option{Cache:odb.CacheAll})
  21. if err != nil {
  22. logger.Fatal(err)
  23. }
  24. locker := odb.NewLocker(g, 5)
  25. if err := locker.Lock(str); err == nil {
  26. defer locker.UnLock()
  27. time.Sleep(time.Duration(200) * time.Millisecond)
  28. logger.Infof("lock %s ok .", str)
  29. } else {
  30. logger.Error("lock err .", err)
  31. }
  32. var wg sync.WaitGroup
  33. for i := 0; i < 1000; i++ {
  34. wg.Add(1)
  35. go func(str string) {
  36. defer wg.Done()
  37. locker := odb.NewLocker(g, 5)
  38. if err := locker.Lock(str); err == nil {
  39. defer locker.UnLock()
  40. time.Sleep(time.Duration(200) * time.Millisecond)
  41. logger.Infof("lock %s ok .", str)
  42. } else {
  43. logger.Error("lock err .", err)
  44. }
  45. }(strconv.Itoa(i))
  46. }
  47. for i := 0; i < 1000; i++ {
  48. wg.Add(1)
  49. go func(str string) {
  50. defer wg.Done()
  51. locker := odb.NewLocker(g, 5)
  52. if err := locker.Lock(str); err == nil {
  53. defer locker.UnLock()
  54. time.Sleep(time.Duration(200) * time.Millisecond)
  55. logger.Infof("lock %s ok .", str)
  56. } else {
  57. logger.Error("lock err .", err)
  58. }
  59. }(strconv.Itoa(i))
  60. }
  61. wg.Wait()
  62. }
  63. /*
  64. [
  65. {
  66. "graph": {
  67. "nodes": [
  68. {
  69. "_icon": "linux",
  70. "class": "/matrix/entity/linux",
  71. "id": "linux:h2"
  72. },
  73. {
  74. "_icon": "cassandra",
  75. "class": "/matrix/entity/cassandra",
  76. "id": "cassandra:c1"
  77. },
  78. {
  79. "_icon": "biz",
  80. "class": "/matrix/entity/biz",
  81. "id": "biz:b1"
  82. },
  83. {
  84. "_icon": "linux",
  85. "class": "/matrix/entity/linux",
  86. "id": "linux:h1"
  87. }
  88. ],
  89. "edges": [
  90. {
  91. "class": "contain",
  92. "id": "biz:b1-linux:h1",
  93. "source": "biz:b1",
  94. "target": "linux:h1"
  95. },
  96. {
  97. "class": "contain",
  98. "id": "biz:b1-linux:h2",
  99. "source": "biz:b1",
  100. "target": "linux:h2"
  101. },
  102. {
  103. "class": "contain",
  104. "id": "linux:h1-cassandra:c1",
  105. "source": "linux:h1",
  106. "target": "cassandra:c1"
  107. }
  108. ],
  109. "paths": null,
  110. "pathtags": null
  111. }
  112. }
  113. ]
  114. */