account.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package service
  2. import (
  3. "context"
  4. "math"
  5. "sync"
  6. account "go-common/app/service/main/account/api"
  7. "go-common/library/log"
  8. "go-common/library/sync/errgroup"
  9. )
  10. func (s *Service) accountInfos(c context.Context, mids []int64) (res map[int64]*account.Info, err error) {
  11. var (
  12. g errgroup.Group
  13. mu sync.Mutex
  14. pagesize = 50
  15. ids = make([]int64, 0, len(mids))
  16. midMap = make(map[int64]struct{})
  17. )
  18. res = make(map[int64]*account.Info)
  19. if len(mids) == 0 {
  20. return
  21. }
  22. for _, mid := range mids {
  23. if _, ok := midMap[mid]; !ok {
  24. midMap[mid] = struct{}{}
  25. ids = append(ids, mid)
  26. }
  27. }
  28. total := len(ids)
  29. pageNum := int(math.Ceil(float64(total) / float64(pagesize)))
  30. for i := 0; i < pageNum; i++ {
  31. start := i * pagesize
  32. end := (i + 1) * pagesize
  33. if end > total {
  34. end = total
  35. }
  36. g.Go(func() (err error) {
  37. var (
  38. arg = &account.MidsReq{Mids: ids[start:end]}
  39. reply *account.InfosReply
  40. )
  41. if reply, err = s.accountRPC.Infos3(c, arg); err != nil {
  42. log.Error("accRPC.Infos3(%+v) error(%v)", arg, err)
  43. return
  44. }
  45. for mid, info := range reply.GetInfos() {
  46. mu.Lock()
  47. res[mid] = info
  48. mu.Unlock()
  49. }
  50. return
  51. })
  52. }
  53. err = g.Wait()
  54. return
  55. }