service.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package block
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/job/main/member/conf"
  7. "go-common/app/job/main/member/dao/block"
  8. "go-common/library/cache"
  9. "go-common/library/log"
  10. "go-common/library/queue/databus"
  11. "github.com/pkg/errors"
  12. )
  13. // Service struct
  14. type Service struct {
  15. dao *block.Dao
  16. conf *conf.Config
  17. cache *cache.Cache
  18. missch chan func()
  19. creditSub *databus.Databus
  20. }
  21. // New init
  22. func New(conf *conf.Config, dao *block.Dao, creditSub *databus.Databus) (s *Service) {
  23. s = &Service{
  24. dao: dao,
  25. conf: conf,
  26. cache: cache.New(1, 10240),
  27. missch: make(chan func(), 10240),
  28. creditSub: creditSub,
  29. }
  30. // 自动解禁检查
  31. if s.conf.BlockProperty.Flag.ExpireCheck {
  32. go s.limitcheckproc()
  33. go s.creditcheckproc()
  34. }
  35. // 小黑屋答题状态订阅
  36. if s.conf.BlockProperty.Flag.CreditSub {
  37. go s.creditsubproc()
  38. }
  39. go s.missproc()
  40. return s
  41. }
  42. // Ping Service
  43. func (s *Service) Ping(c context.Context) (err error) {
  44. return s.dao.Ping(c)
  45. }
  46. // Close Service
  47. func (s *Service) Close() {
  48. s.dao.Close()
  49. }
  50. func (s *Service) limitcheckproc() {
  51. defer func() {
  52. if x := recover(); x != nil {
  53. log.Error("service.limitcheckproc panic(%v)", x)
  54. go s.limitcheckproc()
  55. log.Info("service.limitcheckproc recover")
  56. }
  57. }()
  58. for {
  59. log.Info("limit check start")
  60. s.limitExpireHandler(context.TODO())
  61. log.Info("limit check end")
  62. time.Sleep(time.Duration(s.conf.BlockProperty.LimitExpireCheckTick))
  63. }
  64. }
  65. func (s *Service) creditcheckproc() {
  66. defer func() {
  67. if x := recover(); x != nil {
  68. log.Error("%+v", errors.WithStack(fmt.Errorf("service.creditcheckproc panic(%v)", x)))
  69. go s.creditcheckproc()
  70. log.Info("service.creditcheckproc recover")
  71. }
  72. }()
  73. for {
  74. log.Info("black house check start")
  75. s.creditExpireHandler(context.TODO())
  76. log.Info("black house check end")
  77. time.Sleep(time.Duration(s.conf.BlockProperty.CreditExpireCheckTick))
  78. }
  79. }
  80. func (s *Service) missproc() {
  81. defer func() {
  82. if x := recover(); x != nil {
  83. log.Error("service.missproc panic(%v)", x)
  84. go s.missproc()
  85. log.Info("service.missproc recover")
  86. }
  87. }()
  88. for {
  89. f := <-s.missch
  90. f()
  91. }
  92. }
  93. func (s *Service) mission(f func()) {
  94. select {
  95. case s.missch <- f:
  96. default:
  97. log.Error("s.missch full")
  98. }
  99. }