account.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package dao
  2. import (
  3. "context"
  4. "sync"
  5. accountApi "go-common/app/service/main/account/api"
  6. account "go-common/app/service/main/account/model"
  7. "go-common/library/log"
  8. "go-common/library/sync/errgroup"
  9. )
  10. const (
  11. _perCall = 50
  12. )
  13. func chain(ids ...[]int64) []int64 {
  14. res := make([]int64, 0, len(ids))
  15. for _, l := range ids {
  16. res = append(res, l...)
  17. }
  18. return res
  19. }
  20. func uniq(ids ...[]int64) []int64 {
  21. hm := make(map[int64]struct{})
  22. for _, i := range chain(ids...) {
  23. hm[i] = struct{}{}
  24. }
  25. res := make([]int64, 0, len(ids))
  26. for i := range hm {
  27. res = append(res, i)
  28. }
  29. return res
  30. }
  31. // RPCInfos rpc info get by muti mid .
  32. func (d *Dao) RPCInfos(c context.Context, mids []int64) (res map[int64]*account.Info, err error) {
  33. var (
  34. g errgroup.Group
  35. l sync.RWMutex
  36. )
  37. mids = uniq(mids)
  38. total := len(mids)
  39. pageNum := total / _perCall
  40. if total%_perCall != 0 {
  41. pageNum++
  42. }
  43. res = make(map[int64]*account.Info, total)
  44. for i := 0; i < pageNum; i++ {
  45. start := i * _perCall
  46. end := (i + 1) * _perCall
  47. if end > total {
  48. end = total
  49. }
  50. g.Go(func() (err error) {
  51. midsReq := &accountApi.MidsReq{Mids: mids[start:end]}
  52. infosReply, err := d.accountClient.Infos3(c, midsReq)
  53. if err != nil {
  54. log.Error("d.accountClient.Infos3(%+v) error(%v)", midsReq, err)
  55. err = nil
  56. return
  57. }
  58. for mid, info := range infosReply.Infos {
  59. l.Lock()
  60. res[mid] = info
  61. l.Unlock()
  62. }
  63. return
  64. })
  65. }
  66. if err = g.Wait(); err != nil {
  67. log.Error("g.Wait error(%v)", err)
  68. }
  69. return
  70. }