khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 gRPC authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package internal |
| 18 | |
| 19 | import ( |
| 20 | "google.golang.org/grpc/attributes" |
| 21 | "google.golang.org/grpc/resolver" |
| 22 | ) |
| 23 | |
| 24 | // handshakeClusterNameKey is the type used as the key to store cluster name in |
| 25 | // the Attributes field of resolver.Address. |
| 26 | type handshakeClusterNameKey struct{} |
| 27 | |
| 28 | // SetXDSHandshakeClusterName returns a copy of addr in which the Attributes field |
| 29 | // is updated with the cluster name. |
| 30 | func SetXDSHandshakeClusterName(addr resolver.Address, clusterName string) resolver.Address { |
khenaidoo | 5cb0d40 | 2021-12-08 14:09:16 -0500 | [diff] [blame^] | 31 | addr.Attributes = addr.Attributes.WithValue(handshakeClusterNameKey{}, clusterName) |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 32 | return addr |
| 33 | } |
| 34 | |
| 35 | // GetXDSHandshakeClusterName returns cluster name stored in attr. |
| 36 | func GetXDSHandshakeClusterName(attr *attributes.Attributes) (string, bool) { |
| 37 | v := attr.Value(handshakeClusterNameKey{}) |
| 38 | name, ok := v.(string) |
| 39 | return name, ok |
| 40 | } |