-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2be9db3
commit 67e3799
Showing
3 changed files
with
64 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* | ||
* Copyright 2024 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
// Package attributes contains functions for getting and setting attributes. | ||
package attributes | ||
|
||
import ( | ||
"net/url" | ||
|
||
"google.golang.org/grpc/resolver" | ||
) | ||
|
||
type keyType string | ||
|
||
const userAndConnectAddrKey = keyType("grpc.resolver.delegatingresolver.userAndConnectAddr") | ||
|
||
type attr struct { | ||
user *url.Userinfo | ||
addr string | ||
} | ||
|
||
// SetUserAndConnectAddr returns a copy of the provided resolver.Address with | ||
// attributes containing address to be sent in connect request to proxy and the | ||
// user info. It's data should not be mutated after calling SetConnectAddr. | ||
func SetUserAndConnectAddr(resAddr resolver.Address, user *url.Userinfo, addr string) resolver.Address { | ||
resAddr.Attributes = resAddr.Attributes.WithValue(userAndConnectAddrKey, attr{user: user, addr: addr}) | ||
return resAddr | ||
} | ||
|
||
// ProxyConnectAddr returns the proxy connect address in resolver.Address, or nil | ||
// if not present. The returned data should not be mutated. | ||
func ProxyConnectAddr(addr resolver.Address) string { | ||
return addr.Attributes.Value(userAndConnectAddrKey).(attr).addr | ||
} | ||
|
||
// User returns the user info in the resolver.Address, or nil if not present. | ||
// The returned data should not be mutated. | ||
func User(addr resolver.Address) *url.Userinfo { | ||
return addr.Attributes.Value(userAndConnectAddrKey).(attr).user | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters