service.py 980 Bytes
Newer Older
Wei Shoulin's avatar
Wei Shoulin committed
1
import os
Shoulin Wei's avatar
init  
Shoulin Wei committed
2
import grpc
Wei Shoulin's avatar
C9    
Wei Shoulin committed
3
from csst_dfs_proto.db import db_pb2, db_pb2_grpc
Wei Shoulin's avatar
ephem    
Wei Shoulin committed
4
from csst_dfs_commons.models.errors import CSSTFatalException
Wei Shoulin's avatar
Wei Shoulin committed
5
6
7
8

def grpc_channel(func):
    def wrapper(*args, **kwargs):
        with get_grpc_channel() as c:
Wei Shoulin's avatar
C9    
Wei Shoulin committed
9
            args[0].stub = db_pb2_grpc.DBSrvStub(c)
Wei Shoulin's avatar
Wei Shoulin committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
            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:
Wei Shoulin's avatar
C9    
Wei Shoulin committed
24
        return channel