queue.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package liu
  2. import (
  3. "sync"
  4. "time"
  5. "bet24.com/log"
  6. "bet24.com/servers/common"
  7. "bet24.com/servers/payment/db"
  8. )
  9. var withdrawQueues *withdrawQueue
  10. // 请求队列
  11. type withdrawQueue struct {
  12. queues []*withdrawInfo
  13. lock *sync.RWMutex
  14. }
  15. // 提现信息
  16. type withdrawInfo struct {
  17. OrderId string // 订单号
  18. ReqTimes int // 请求次数
  19. ts int // 时间戳
  20. }
  21. func Run() {
  22. withdrawQueues = new(withdrawQueue)
  23. withdrawQueues.lock = &sync.RWMutex{}
  24. // 加载数据
  25. withdrawQueues.loadQueue()
  26. // 去检查
  27. withdrawQueues.check()
  28. log.Debug("liu.queue withdrawQueues run...")
  29. }
  30. // 加载数据
  31. func (wq *withdrawQueue) loadQueue() {
  32. obj := db.NewLiuWithdrawQueue()
  33. obj.DoAction()
  34. var list []*withdrawInfo
  35. for _, v := range obj.Out.List {
  36. list = append(list, &withdrawInfo{
  37. OrderId: v,
  38. ReqTimes: 0,
  39. ts: 0,
  40. })
  41. }
  42. wq.lock.Lock()
  43. defer wq.lock.Unlock()
  44. wq.queues = list
  45. }
  46. // 检查数据
  47. func (wq *withdrawQueue) check() {
  48. ticker := time.NewTicker(1 * time.Second)
  49. go func(t *time.Ticker) {
  50. for {
  51. select {
  52. case <-t.C:
  53. // 查询提现订单并请求
  54. wq.search()
  55. }
  56. }
  57. }(ticker)
  58. }
  59. // 查询提现订单并请求
  60. func (wq *withdrawQueue) search() {
  61. for i := 0; i < len(wq.queues); i++ {
  62. // 还没到时间
  63. if wq.queues[i].ts > common.GetTimeStamp() {
  64. continue
  65. }
  66. log.Debug("liu.queue search idx=[%d] ts=%d ==> %+v", i, common.GetTimeStamp(), wq.queues[i])
  67. // 失败,查询次数+1
  68. if ok := withdrawSearch(wq.queues[i].OrderId); !ok {
  69. wq.queues[i].ts = common.GetTimeStamp() + getSeconds(wq.queues[i].ReqTimes)
  70. wq.queues[i].ReqTimes++
  71. continue
  72. }
  73. // 成功,从队列里删除
  74. wq.lock.Lock()
  75. wq.queues = append(wq.queues[:i], wq.queues[i+1:]...)
  76. wq.lock.Unlock()
  77. }
  78. }
  79. // 添加数据(压数据)
  80. func (wq *withdrawQueue) push(orderId string) {
  81. info := &withdrawInfo{
  82. OrderId: orderId,
  83. ReqTimes: 0,
  84. ts: common.GetTimeStamp(),
  85. }
  86. wq.lock.Lock()
  87. defer wq.lock.Unlock()
  88. wq.queues = append(wq.queues, info)
  89. }
  90. func Dump(param1, param2 string) {
  91. switch param1 {
  92. case "sys":
  93. withdrawQueues.dumpOrder(param2)
  94. default:
  95. log.Debug("withdrawQueues.Dump unhandled %s:%s", param1, param2)
  96. }
  97. }
  98. func (wq *withdrawQueue) dumpOrder(param string) {
  99. log.Debug("-------------------------------")
  100. log.Debug("withdrawQueue.dumpOrder %s %d", param, common.GetTimeStamp())
  101. defer func() {
  102. log.Debug("+++++++++++++++++++++++++++++++")
  103. log.Debug("")
  104. }()
  105. for k, v := range wq.queues {
  106. if param != "" && param == v.OrderId {
  107. log.Debug("idx[%d] ==> %+v", k, v)
  108. return
  109. }
  110. log.Debug("idx[%d] ==> %+v", k, v)
  111. }
  112. return
  113. }