service.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package telecom
  2. import (
  3. "fmt"
  4. "go-common/app/interface/main/app-wall/conf"
  5. seqDao "go-common/app/interface/main/app-wall/dao/seq"
  6. telecomDao "go-common/app/interface/main/app-wall/dao/telecom"
  7. "go-common/library/log"
  8. "go-common/library/stat/prom"
  9. )
  10. const (
  11. _initIPlimitKey = "iplimit_%v_%v"
  12. _telecomKey = "telecom"
  13. )
  14. type Service struct {
  15. c *conf.Config
  16. dao *telecomDao.Dao
  17. seqdao *seqDao.Dao
  18. flowPercentage int
  19. smsTemplate string
  20. smsMsgTemplate string
  21. smsFlowTemplate string
  22. smsOrderTemplateOK string
  23. operationIPlimit map[string]struct{}
  24. telecomArea map[string]struct{}
  25. // prom
  26. pHit *prom.Prom
  27. pMiss *prom.Prom
  28. }
  29. func New(c *conf.Config) (s *Service) {
  30. s = &Service{
  31. c: c,
  32. dao: telecomDao.New(c),
  33. seqdao: seqDao.New(c),
  34. flowPercentage: c.Telecom.FlowPercentage,
  35. smsTemplate: c.Telecom.SMSTemplate,
  36. smsMsgTemplate: c.Telecom.SMSMsgTemplate,
  37. smsFlowTemplate: c.Telecom.SMSFlowTemplate,
  38. smsOrderTemplateOK: c.Telecom.SMSOrderTemplateOK,
  39. operationIPlimit: map[string]struct{}{},
  40. telecomArea: map[string]struct{}{},
  41. // prom
  42. pHit: prom.CacheHit,
  43. pMiss: prom.CacheMiss,
  44. }
  45. go s.loadIPlimit(c)
  46. go s.loadTelecomArea(c)
  47. return
  48. }
  49. func (s *Service) loadIPlimit(c *conf.Config) {
  50. hosts := make(map[string]struct{}, len(c.IPLimit.Addrs))
  51. for k, v := range c.IPLimit.Addrs {
  52. for _, ipStr := range v {
  53. key := fmt.Sprintf(_initIPlimitKey, k, ipStr)
  54. if _, ok := hosts[key]; !ok {
  55. hosts[key] = struct{}{}
  56. }
  57. }
  58. }
  59. s.operationIPlimit = hosts
  60. log.Info("loadTelecomIPCache success")
  61. }
  62. func (s *Service) loadTelecomArea(c *conf.Config) {
  63. areas := make(map[string]struct{}, len(c.Telecom.Area))
  64. for _, v := range c.Telecom.Area {
  65. for _, area := range v {
  66. if _, ok := areas[area]; !ok {
  67. areas[area] = struct{}{}
  68. }
  69. }
  70. }
  71. s.telecomArea = areas
  72. log.Info("loadTelecomArea success")
  73. }