balancer.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package rpc
  2. import (
  3. "context"
  4. "reflect"
  5. "sync/atomic"
  6. "go-common/library/log"
  7. "go-common/library/sync/errgroup"
  8. )
  9. const (
  10. _clientsPool = 3
  11. )
  12. // balancer interface.
  13. type balancer interface {
  14. Boardcast(context.Context, string, interface{}, interface{}) error
  15. Call(context.Context, string, interface{}, interface{}) error
  16. }
  17. // wrr get avaliable rpc client by wrr strategy.
  18. type wrr struct {
  19. pool []*Client
  20. weight int64
  21. server int64
  22. idx int64
  23. }
  24. // Boardcast broad cast to all Client.
  25. // NOTE: reply must be ptr.
  26. func (r *wrr) Boardcast(ctx context.Context, serviceMethod string, args interface{}, reply interface{}) (err error) {
  27. if r.weight == 0 {
  28. log.Error("wrr get() error weight:%d server:%d idx:%d", len(r.pool), r.server, r.idx)
  29. return ErrNoClient
  30. }
  31. rtp := reflect.TypeOf(reply).Elem()
  32. g, ctx := errgroup.WithContext(ctx)
  33. for i := 0; i < int(r.server); i++ {
  34. j := i
  35. g.Go(func() error {
  36. nrp := reflect.New(rtp).Interface()
  37. return r.pool[j].Call(ctx, serviceMethod, args, nrp)
  38. })
  39. }
  40. return g.Wait()
  41. }
  42. func (r *wrr) Call(ctx context.Context, serviceMethod string, args interface{}, reply interface{}) (err error) {
  43. if r.weight == 0 {
  44. log.Error("wrr get() error weight:%d server:%d idx:%d", len(r.pool), r.server, r.idx)
  45. return ErrNoClient
  46. }
  47. v := atomic.AddInt64(&r.idx, 1)
  48. for i := int64(0); i < r.server; i++ {
  49. cli := r.pool[int((v+i)%r.weight)]
  50. if err = cli.Call(ctx, serviceMethod, args, reply); err != ErrNoClient {
  51. return
  52. }
  53. }
  54. return ErrNoClient
  55. }
  56. type key interface {
  57. Key() int64
  58. }
  59. type sharding struct {
  60. pool []*Client
  61. weight int64
  62. server int64
  63. idx int64
  64. }
  65. // Boardcast broad cast to all clients.
  66. // NOTE: reply must be ptr.
  67. func (r *sharding) Boardcast(ctx context.Context, serviceMethod string, args interface{}, reply interface{}) (err error) {
  68. if r.weight == 0 {
  69. log.Error("wrr get() error weight:%d server:%d idx:%d", len(r.pool), r.server, r.idx)
  70. return ErrNoClient
  71. }
  72. rtp := reflect.TypeOf(reply).Elem()
  73. g, ctx := errgroup.WithContext(ctx)
  74. for i := 0; i < int(r.server); i++ {
  75. j := i
  76. g.Go(func() error {
  77. nrp := reflect.New(rtp).Interface()
  78. return r.pool[j].Call(ctx, serviceMethod, args, nrp)
  79. })
  80. }
  81. return g.Wait()
  82. }
  83. func (r *sharding) Call(ctx context.Context, serviceMethod string, args interface{}, reply interface{}) (err error) {
  84. if r.weight == 0 {
  85. log.Error("wrr get() error weight:%d server:%d idx:%d", len(r.pool), r.server, r.idx)
  86. return ErrNoClient
  87. }
  88. if k, ok := args.(key); ok {
  89. if err = r.pool[int(k.Key()%r.server)].Call(ctx, serviceMethod, args, reply); err != ErrNoClient {
  90. return
  91. }
  92. }
  93. return ErrNoClient
  94. }