service.py 1.63 KB
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
ephem    
Wei Shoulin committed
3
from csst_dfs_commons.models.errors import CSSTFatalException
Wei Shoulin's avatar
Wei Shoulin committed
4

Shoulin Wei's avatar
init  
Shoulin Wei committed
5
class ServiceProxy:
Wei Shoulin's avatar
Wei Shoulin committed
6
7
    def __init__(self):
        self.gateway = os.getenv("CSST_DFS_GATEWAY",'172.31.248.218:30880')
Shoulin Wei's avatar
init  
Shoulin Wei committed
8

Wei Shoulin's avatar
ephem    
Wei Shoulin committed
9
    def channel(self):
Wei Shoulin's avatar
Wei Shoulin committed
10
11
        options = (('grpc.max_send_message_length', 1024 * 1024 * 1024),
                    ('grpc.max_receive_message_length', 1024 * 1024 * 1024))     
Wei Shoulin's avatar
Wei Shoulin committed
12
13
        # channel = grpc.insecure_channel(self.gateway, options = options, compression = grpc.Compression.Gzip)
        channel = grpc.insecure_channel(self.gateway, options = options)
Wei Shoulin's avatar
ephem    
Wei Shoulin committed
14
15
16
17
18
        try:
            grpc.channel_ready_future(channel).result(timeout=10)
        except grpc.FutureTimeoutError:
            raise CSSTFatalException('Error connecting to server {}'.format(self.gateway))
        else:
Wei Shoulin's avatar
Wei Shoulin committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
            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