dao.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package email
  2. import (
  3. "crypto/tls"
  4. "go-common/app/job/main/videoup-report/conf"
  5. "go-common/app/job/main/videoup-report/model/email"
  6. "go-common/library/cache/redis"
  7. gomail "gopkg.in/gomail.v2"
  8. )
  9. // Dao is redis dao.
  10. type Dao struct {
  11. c *conf.Config
  12. redis *redis.Pool
  13. email *gomail.Dialer
  14. FansAddr map[int16][]string
  15. emailAddr map[string][]string
  16. PrivateAddr map[string][]string
  17. //fast behavior detector
  18. detector *email.FastDetector
  19. //快速通道token
  20. fastChan chan int
  21. //邮件发送api的频率token,发送邮件5s后插入
  22. controlChan chan int64
  23. }
  24. // New is new redis dao.
  25. func New(c *conf.Config) (d *Dao) {
  26. emailAddr := make(map[string][]string)
  27. for _, v := range c.Mail.Addr {
  28. emailAddr[v.Type] = v.Addr
  29. }
  30. privateMail := make(map[string][]string)
  31. for _, v := range c.Mail.PrivateAddr {
  32. privateMail[v.Type] = v.Addr
  33. }
  34. d = &Dao{
  35. c: c,
  36. redis: redis.NewPool(c.Redis.Mail),
  37. email: gomail.NewDialer(c.Mail.Host, c.Mail.Port, c.Mail.Username, c.Mail.Password),
  38. emailAddr: emailAddr,
  39. PrivateAddr: privateMail,
  40. detector: email.NewFastDetector(c.Mail.SpeedThreshold, c.Mail.OverspeedThreshold),
  41. fastChan: make(chan int, 10240),
  42. controlChan: make(chan int64, 1),
  43. }
  44. d.email.TLSConfig = &tls.Config{
  45. InsecureSkipVerify: true,
  46. }
  47. d.fastChan <- 1
  48. d.controlChan <- 1
  49. return d
  50. }
  51. //Close close
  52. func (d *Dao) Close() (err error) {
  53. if d.redis != nil {
  54. err = d.redis.Close()
  55. }
  56. return
  57. }
  58. //FastChan get fast channel
  59. func (d *Dao) FastChan() <-chan int {
  60. return d.fastChan
  61. }