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