forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrpc.py
More file actions
53 lines (48 loc) · 1.85 KB
/
grpc.py
File metadata and controls
53 lines (48 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import grpc
def create_grpc_channel(
url: str,
enable_ssl: bool = False,
enable_auth: bool = False,
ssl_server_cert_path: str = None,
auth_metadata_plugin: grpc.AuthMetadataPlugin = None,
timeout: int = 3,
) -> grpc.Channel:
"""
Create a gRPC channel
Args:
url: gRPC URL to connect to
enable_ssl: Enable TLS/SSL, optionally provide a server side certificate
enable_auth: Enable user auth
ssl_server_cert_path: (optional) Path to certificate (used with
"enable SSL")
auth_metadata_plugin: Metadata plugin to use to sign requests, only used
with "enable auth" when SSL/TLS is enabled
timeout: Connection timeout to server
Returns: Returns a grpc.Channel
"""
if not url:
raise ValueError("Unable to create gRPC channel. URL has not been defined.")
if enable_ssl or url.endswith(":443"):
# User has provided a public key certificate
if ssl_server_cert_path:
with open(ssl_server_cert_path, "rb",) as f:
credentials = grpc.ssl_channel_credentials(f.read())
# Guess the certificate location
else:
credentials = grpc.ssl_channel_credentials()
# Authentication is enabled, add the metadata plugin in order to sign
# requests
if enable_auth:
credentials = grpc.composite_channel_credentials(
credentials, grpc.metadata_call_credentials(auth_metadata_plugin),
)
channel = grpc.secure_channel(url, credentials=credentials)
else:
channel = grpc.insecure_channel(url)
try:
grpc.channel_ready_future(channel).result(timeout=timeout)
return channel
except grpc.FutureTimeoutError:
raise ConnectionError(
f"Connection timed out while attempting to connect to {url}"
)