dao.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/live/push-live/conf"
  6. "go-common/library/cache/redis"
  7. "go-common/library/database/hbase.v2"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/log"
  10. xhttp "go-common/library/net/http/blademaster"
  11. "go-common/library/stat/prom"
  12. )
  13. // Dao dao
  14. type Dao struct {
  15. c *conf.Config
  16. db *xsql.DB
  17. httpClient *xhttp.Client
  18. relationHBase *hbase.Client
  19. relationHBaseReadTimeout time.Duration
  20. blackListHBase *hbase.Client
  21. blackListHBaseReadTimeout time.Duration
  22. }
  23. // Prometheus
  24. var (
  25. errorsCount = prom.BusinessErrCount
  26. infosCount = prom.BusinessInfoCount
  27. )
  28. // New init mysql db
  29. func New(c *conf.Config) (dao *Dao) {
  30. dao = &Dao{
  31. c: c,
  32. db: xsql.NewMySQL(c.MySQL),
  33. relationHBase: hbase.NewClient(c.HBase.Config),
  34. relationHBaseReadTimeout: time.Duration(c.HBase.ReadTimeout),
  35. httpClient: xhttp.NewClient(c.HTTPClient),
  36. blackListHBase: hbase.NewClient(c.BlackListHBase.Config),
  37. blackListHBaseReadTimeout: time.Duration(c.BlackListHBase.ReadTimeout),
  38. }
  39. return
  40. }
  41. // RedisOption return redis options
  42. func (d *Dao) RedisOption() []redis.DialOption {
  43. cnop := redis.DialConnectTimeout(time.Duration(d.c.Redis.PushInterval.DialTimeout))
  44. rdop := redis.DialReadTimeout(time.Duration(d.c.Redis.PushInterval.ReadTimeout))
  45. wrop := redis.DialWriteTimeout(time.Duration(d.c.Redis.PushInterval.WriteTimeout))
  46. return []redis.DialOption{cnop, rdop, wrop}
  47. }
  48. // Close close the resource.
  49. func (d *Dao) Close() (err error) {
  50. if err = d.relationHBase.Close(); err != nil {
  51. log.Error("[dao.dao|Close] d.relationHBase.Close() error(%v)", err)
  52. PromError("hbase:close")
  53. }
  54. if err = d.db.Close(); err != nil {
  55. log.Error("[dao.dao|Close] d.db.Close() error(%v)", err)
  56. PromError("db:close")
  57. }
  58. return
  59. }
  60. // PromError prom error
  61. func PromError(name string) {
  62. errorsCount.Incr(name)
  63. }
  64. // PromInfo add prom info
  65. func PromInfo(name string) {
  66. infosCount.Incr(name)
  67. }
  68. //PromInfoAdd add prom info by value
  69. func PromInfoAdd(name string, value int64) {
  70. infosCount.Add(name, value)
  71. }
  72. // Ping dao ping
  73. func (d *Dao) Ping(c context.Context) (err error) {
  74. if err = d.db.Ping(c); err != nil {
  75. PromError("mysql:Ping")
  76. log.Error("[dao.dao|Ping] d.db.Ping error(%v)", err)
  77. return
  78. }
  79. if err = d.relationHBase.Ping(c); err != nil {
  80. PromError("hbase:Ping")
  81. log.Error("[dao.dao|Ping] d.relationHBase.Ping error(%v)", err)
  82. return
  83. }
  84. return
  85. }