state.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. *
  3. * Copyright 2020 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 state declares grpclb types to be set by resolvers wishing to pass
  19. // information to grpclb via resolver.State Attributes.
  20. package state
  21. import (
  22. "google.golang.org/grpc/resolver"
  23. )
  24. // keyType is the key to use for storing State in Attributes.
  25. type keyType string
  26. const key = keyType("grpc.grpclb.state")
  27. // State contains gRPCLB-relevant data passed from the name resolver.
  28. type State struct {
  29. // BalancerAddresses contains the remote load balancer address(es). If
  30. // set, overrides any resolver-provided addresses with Type of GRPCLB.
  31. BalancerAddresses []resolver.Address
  32. }
  33. // Set returns a copy of the provided state with attributes containing s. s's
  34. // data should not be mutated after calling Set.
  35. func Set(state resolver.State, s *State) resolver.State {
  36. state.Attributes = state.Attributes.WithValue(key, s)
  37. return state
  38. }
  39. // Get returns the grpclb State in the resolver.State, or nil if not present.
  40. // The returned data should not be mutated.
  41. func Get(state resolver.State) *State {
  42. s, _ := state.Attributes.Value(key).(*State)
  43. return s
  44. }