nop.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. *
  3. * Copyright 2023 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 nop implements a balancer with all of its balancer operations as
  19. // no-ops, other than returning a Transient Failure Picker on a Client Conn
  20. // update.
  21. package nop
  22. import (
  23. "google.golang.org/grpc/balancer"
  24. "google.golang.org/grpc/balancer/base"
  25. "google.golang.org/grpc/connectivity"
  26. )
  27. // bal is a balancer with all of its balancer operations as no-ops, other than
  28. // returning a Transient Failure Picker on a Client Conn update.
  29. type bal struct {
  30. cc balancer.ClientConn
  31. err error
  32. }
  33. // NewBalancer returns a no-op balancer.
  34. func NewBalancer(cc balancer.ClientConn, err error) balancer.Balancer {
  35. return &bal{
  36. cc: cc,
  37. err: err,
  38. }
  39. }
  40. // UpdateClientConnState updates the bal's Client Conn with an Error Picker
  41. // and a Connectivity State of TRANSIENT_FAILURE.
  42. func (b *bal) UpdateClientConnState(_ balancer.ClientConnState) error {
  43. b.cc.UpdateState(balancer.State{
  44. Picker: base.NewErrPicker(b.err),
  45. ConnectivityState: connectivity.TransientFailure,
  46. })
  47. return nil
  48. }
  49. // ResolverError is a no-op.
  50. func (b *bal) ResolverError(_ error) {}
  51. // UpdateSubConnState is a no-op.
  52. func (b *bal) UpdateSubConnState(_ balancer.SubConn, _ balancer.SubConnState) {}
  53. // Close is a no-op.
  54. func (b *bal) Close() {}