dao.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/admin/main/videoup-task/conf"
  6. account "go-common/app/service/main/account/api"
  7. "go-common/app/service/main/account/model"
  8. "go-common/library/cache/redis"
  9. "go-common/library/database/hbase.v2"
  10. "go-common/library/database/sql"
  11. "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/stat/prom"
  14. )
  15. //Dao dao.
  16. type Dao struct {
  17. c *conf.Config
  18. arcDB *sql.DB
  19. arcReadDB *sql.DB
  20. mngDB *sql.DB
  21. upGroupURL string
  22. hbase *hbase.Client
  23. redis *redis.Pool
  24. hclient *bm.Client
  25. //grpc
  26. acc account.AccountClient
  27. }
  28. var errCount = prom.BusinessErrCount
  29. //New new dao
  30. func New(config *conf.Config) (d *Dao) {
  31. d = &Dao{
  32. c: config,
  33. arcDB: sql.NewMySQL(config.DB.Archive),
  34. arcReadDB: sql.NewMySQL(config.DB.ArchiveRead),
  35. mngDB: sql.NewMySQL(config.DB.Manager),
  36. upGroupURL: config.Host.API + "/x/internal/uper/special/get",
  37. hbase: hbase.NewClient(config.HBase.Config),
  38. redis: redis.NewPool(config.Redis.Weight.Config),
  39. hclient: bm.NewClient(config.HTTPClient),
  40. }
  41. var err error
  42. if d.acc, err = account.NewClient(config.GRPC.AccRPC); err != nil {
  43. panic(err)
  44. }
  45. return
  46. }
  47. //BeginTran begin transaction
  48. func (d *Dao) BeginTran(ctx context.Context) (tx *sql.Tx, err error) {
  49. if tx, err = d.arcDB.Begin(ctx); err != nil {
  50. PromeErr("arcdb: begintran", "BeginTran d.arcDB.Begin error(%v)", err)
  51. }
  52. return
  53. }
  54. //Close close
  55. func (d *Dao) Close() {
  56. if d.arcDB != nil {
  57. d.arcDB.Close()
  58. }
  59. if d.arcReadDB != nil {
  60. d.arcReadDB.Close()
  61. }
  62. if d.mngDB != nil {
  63. d.mngDB.Close()
  64. }
  65. }
  66. //Ping ping
  67. func (d *Dao) Ping(ctx context.Context) (err error) {
  68. if d.arcDB != nil {
  69. if err = d.arcDB.Ping(ctx); err != nil {
  70. PromeErr("arcdb: ping", "d.arcDB.Ping error(%v)", err)
  71. return
  72. }
  73. }
  74. if d.arcReadDB != nil {
  75. if err = d.arcReadDB.Ping(ctx); err != nil {
  76. PromeErr("arcReaddb: ping", "d.arcReadDB.Ping error(%v)", err)
  77. return
  78. }
  79. }
  80. if d.mngDB != nil {
  81. if err = d.mngDB.Ping(ctx); err != nil {
  82. PromeErr("mngdb: ping", "d.mngDB.Ping error(%v)", err)
  83. return
  84. }
  85. }
  86. return
  87. }
  88. //AccountInfos get multi mids' accountinfo
  89. func (d *Dao) AccountInfos(ctx context.Context, mids []int64) (info map[int64]*model.Info, err error) {
  90. tctx, cancel := context.WithTimeout(ctx, time.Millisecond*500)
  91. defer cancel()
  92. var infosreply *account.InfosReply
  93. if infosreply, err = d.acc.Infos3(tctx, &account.MidsReq{Mids: mids}); err != nil {
  94. PromeErr("account_infos", "AccountInfos d.acc.Infos3 error(%v) mid(%d)", err, mids)
  95. }
  96. if infosreply != nil {
  97. info = infosreply.Infos
  98. }
  99. return
  100. }
  101. //PromeErr prome & log err
  102. func PromeErr(name string, format string, args ...interface{}) {
  103. errCount.Incr(name)
  104. log.Error(format, args...)
  105. }