dao.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/service/main/vip/conf"
  6. eleclient "go-common/app/service/main/vip/dao/ele-api-client"
  7. mailclient "go-common/app/service/main/vip/dao/mail-api-client"
  8. "go-common/library/cache/memcache"
  9. "go-common/library/cache/redis"
  10. "go-common/library/database/sql"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/stat/prom"
  13. "go-common/library/sync/pipeline/fanout"
  14. )
  15. // Dao struct info of Dao.
  16. type Dao struct {
  17. // mysql
  18. db *sql.DB
  19. olddb *sql.DB
  20. // http
  21. client *bm.Client
  22. // ele api http client
  23. eleclient *eleclient.EleClient
  24. // mail api http client
  25. mailclient *mailclient.Client
  26. // conf
  27. c *conf.Config
  28. msgURI string
  29. payURI string
  30. payCloseURL string
  31. vipURI string
  32. passportDetail string
  33. mc *memcache.Pool
  34. mcExpire int32
  35. errProm *prom.Prom
  36. loginOutURL string
  37. //redis pool
  38. redis *redis.Pool
  39. // cache async save
  40. cache *fanout.Fanout
  41. }
  42. // New new a Dao and return.
  43. func New(c *conf.Config) (d *Dao) {
  44. d = &Dao{
  45. // conf
  46. c: c,
  47. // db
  48. db: sql.NewMySQL(c.Mysql),
  49. olddb: sql.NewMySQL(c.OldMysql),
  50. // http client.
  51. client: bm.NewClient(c.HTTPClient),
  52. msgURI: c.MsgURI + _SendUserNotify,
  53. payURI: c.PayURI + _PayAPIAdd,
  54. payCloseURL: c.Property.PayCoURL + _payClose,
  55. passportDetail: c.Property.PassportURL + _passportDetail,
  56. vipURI: c.VipURI + _CleanCache,
  57. mc: memcache.NewPool(c.Memcache.Config),
  58. mcExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
  59. errProm: prom.BusinessErrCount,
  60. loginOutURL: c.Property.PassportURL + _loginout,
  61. redis: redis.NewPool(c.Redis.Config),
  62. // cache chan
  63. cache: fanout.New("cache", fanout.Worker(10), fanout.Buffer(10240)),
  64. }
  65. // ele
  66. d.eleclient = eleclient.NewEleClient(c.ELEConf, d.client)
  67. d.mailclient = mailclient.NewClient(d.client)
  68. return
  69. }
  70. // Ping ping health of db.
  71. func (d *Dao) Ping(c context.Context) (err error) {
  72. if err = d.pingMC(c); err != nil {
  73. return
  74. }
  75. if err = d.olddb.Ping(c); err != nil {
  76. return
  77. }
  78. return d.db.Ping(c)
  79. }
  80. // Close close connections of mc, redis, db.
  81. func (d *Dao) Close() {
  82. if d.db != nil {
  83. d.db.Close()
  84. }
  85. if d.olddb != nil {
  86. d.olddb.Close()
  87. }
  88. if d.mc != nil {
  89. d.mc.Close()
  90. }
  91. }
  92. //StartTx start tx
  93. func (d *Dao) StartTx(c context.Context) (tx *sql.Tx, err error) {
  94. if d.db != nil {
  95. tx, err = d.db.Begin(c)
  96. }
  97. return
  98. }
  99. //OldStartTx old start tx
  100. func (d *Dao) OldStartTx(c context.Context) (tx *sql.Tx, err error) {
  101. if d.db != nil {
  102. tx, err = d.olddb.Begin(c)
  103. }
  104. return
  105. }