log_query_export_global.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package access_log
  4. import (
  5. "gd_statistics/rpc_apis/gd_management"
  6. "sync"
  7. "time"
  8. "gd_statistics/common.in/cache"
  9. )
  10. var globalApiExportMux sync.RWMutex
  11. var globalProviderExportMux sync.RWMutex
  12. var globalTimeoutInfo map[string]int
  13. var globalShowInfo map[int64]bool
  14. var globalCountCode []gd_management.UserMerchantCountCode
  15. var globalCountCodeMap map[string]gd_management.UserMerchantCountCode
  16. var globalProviderCountInfo map[int64]ProviderCountInfo
  17. func apiExportInfoInit() error {
  18. var err error
  19. globalShowInfo, err = getNotShow()
  20. if err != nil {
  21. globalShowInfo = nil
  22. return err
  23. }
  24. globalTimeoutInfo, err = getTimeoutInfo()
  25. if err != nil {
  26. globalTimeoutInfo = nil
  27. return err
  28. }
  29. globalCountCodeMap, globalCountCode, err = getCountCodeInfoNew()
  30. if err != nil {
  31. globalCountCodeMap = nil
  32. globalCountCode = []gd_management.UserMerchantCountCode{}
  33. return err
  34. }
  35. return nil
  36. }
  37. func providerExportInfoInit() error {
  38. var err error
  39. globalProviderCountInfo, err = getProviderCountInfo()
  40. if err != nil {
  41. globalProviderCountInfo = nil
  42. return err
  43. }
  44. return nil
  45. }
  46. func LogExportInfoInit() {
  47. go func() {
  48. for {
  49. err := apiExportInfoReset("")
  50. if err != nil {
  51. time.Sleep(5 * time.Second)
  52. continue
  53. }
  54. break
  55. }
  56. for {
  57. err := providerExportInfoReset("")
  58. if err != nil {
  59. time.Sleep(5 * time.Second)
  60. continue
  61. }
  62. break
  63. }
  64. }()
  65. }
  66. func apiExportInfoReset(empty string) error {
  67. globalApiExportMux.Lock()
  68. defer globalApiExportMux.Unlock()
  69. globalShowInfo = nil
  70. globalTimeoutInfo = nil
  71. globalCountCodeMap = nil
  72. globalCountCode = []gd_management.UserMerchantCountCode{}
  73. return apiExportInfoInit()
  74. }
  75. func providerExportInfoReset(empty string) error {
  76. globalProviderExportMux.Lock()
  77. defer globalProviderExportMux.Unlock()
  78. globalProviderCountInfo = nil
  79. return providerExportInfoInit()
  80. }
  81. func UpdateLocalCache() {
  82. t := time.NewTicker(10 * time.Minute)
  83. for {
  84. select {
  85. case <-t.C:
  86. apiExportInfoReset("")
  87. providerExportInfoReset("")
  88. }
  89. }
  90. }
  91. var ApiExportInfoNotifyChannel = "api_export_info_notify"
  92. var ProviderExportInfoNotifyChannel = "provider_export_info_notify"
  93. func Watch() {
  94. go func() {
  95. cache.Redis.RegisterFunc(ApiExportInfoNotifyChannel, apiExportInfoReset)
  96. cache.Redis.RegisterFunc(ProviderExportInfoNotifyChannel, providerExportInfoReset)
  97. cache.Redis.SubscribeAndHandle(ApiExportInfoNotifyChannel, ProviderExportInfoNotifyChannel)
  98. }()
  99. }