roundrobin.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. *
  3. * Copyright 2017 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // Package roundrobin defines a roundrobin balancer. Roundrobin balancer is
  19. // installed as one of the default balancers in gRPC, users don't need to
  20. // explicitly install this balancer.
  21. package roundrobin
  22. import (
  23. "context"
  24. "sync"
  25. "google.golang.org/grpc/balancer"
  26. "google.golang.org/grpc/balancer/base"
  27. "google.golang.org/grpc/grpclog"
  28. "google.golang.org/grpc/internal/grpcrand"
  29. "google.golang.org/grpc/resolver"
  30. )
  31. // Name is the name of round_robin balancer.
  32. const Name = "round_robin"
  33. // newBuilder creates a new roundrobin balancer builder.
  34. func newBuilder() balancer.Builder {
  35. return base.NewBalancerBuilderWithConfig(Name, &rrPickerBuilder{}, base.Config{HealthCheck: true})
  36. }
  37. func init() {
  38. balancer.Register(newBuilder())
  39. }
  40. type rrPickerBuilder struct{}
  41. func (*rrPickerBuilder) Build(readySCs map[resolver.Address]balancer.SubConn) balancer.Picker {
  42. grpclog.Infof("roundrobinPicker: newPicker called with readySCs: %v", readySCs)
  43. if len(readySCs) == 0 {
  44. return base.NewErrPicker(balancer.ErrNoSubConnAvailable)
  45. }
  46. var scs []balancer.SubConn
  47. for _, sc := range readySCs {
  48. scs = append(scs, sc)
  49. }
  50. return &rrPicker{
  51. subConns: scs,
  52. // Start at a random index, as the same RR balancer rebuilds a new
  53. // picker when SubConn states change, and we don't want to apply excess
  54. // load to the first server in the list.
  55. next: grpcrand.Intn(len(scs)),
  56. }
  57. }
  58. type rrPicker struct {
  59. // subConns is the snapshot of the roundrobin balancer when this picker was
  60. // created. The slice is immutable. Each Get() will do a round robin
  61. // selection from it and return the selected SubConn.
  62. subConns []balancer.SubConn
  63. mu sync.Mutex
  64. next int
  65. }
  66. func (p *rrPicker) Pick(ctx context.Context, opts balancer.PickOptions) (balancer.SubConn, func(balancer.DoneInfo), error) {
  67. p.mu.Lock()
  68. sc := p.subConns[p.next]
  69. p.next = (p.next + 1) % len(p.subConns)
  70. p.mu.Unlock()
  71. return sc, nil, nil
  72. }