import os import grpc from csst_dfs_commons.models.errors import CSSTFatalException class ServiceProxy: def __init__(self): self.gateway = os.getenv("CSST_DFS_GATEWAY",'172.31.248.218:30880') def channel(self): options = (('grpc.max_send_message_length', 1024 * 1024 * 1024), ('grpc.max_receive_message_length', 1024 * 1024 * 1024)) # channel = grpc.insecure_channel(self.gateway, options = options, compression = grpc.Compression.Gzip) channel = grpc.insecure_channel(self.gateway, options = options) try: grpc.channel_ready_future(channel).result(timeout=10) except grpc.FutureTimeoutError: raise CSSTFatalException('Error connecting to server {}'.format(self.gateway)) else: return channel def grpc_channel(func): def wrapper(*args, **kwargs): with get_grpc_channel() as c: args[0].stub = args[0].stub_class(c) return func(*args, **kwargs) return wrapper def get_grpc_channel(): options = (('grpc.max_send_message_length', 1024 * 1024 * 1024), ('grpc.max_receive_message_length', 1024 * 1024 * 1024)) gateway = os.getenv("CSST_DFS_GATEWAY",'172.31.248.218:30880') # channel = grpc.insecure_channel(self.gateway, options = options, compression = grpc.Compression.Gzip) channel = grpc.insecure_channel(gateway, options = options) try: grpc.channel_ready_future(channel).result(timeout=10) except grpc.FutureTimeoutError: raise CSSTFatalException('Error connecting to server {}'.format(gateway)) else: return channel