|
| 1 | +import time |
| 2 | +import collections |
| 3 | +import grpc |
| 4 | +from . import fl_logging as logging |
| 5 | + |
| 6 | + |
| 7 | +class LocalServicerContext(grpc.ServicerContext): |
| 8 | + |
| 9 | + def invocation_metadata(self): |
| 10 | + return () |
| 11 | + |
| 12 | + def peer(self): |
| 13 | + return "local" |
| 14 | + |
| 15 | + def peer_identities(self): |
| 16 | + return None |
| 17 | + |
| 18 | + def peer_identity_key(self): |
| 19 | + return None |
| 20 | + |
| 21 | + def auth_context(self): |
| 22 | + return dict() |
| 23 | + |
| 24 | + def set_compression(self, compression): |
| 25 | + return grpc.Compression.NoCompression |
| 26 | + |
| 27 | + def send_initial_metadata(self, initial_metadata): |
| 28 | + pass |
| 29 | + |
| 30 | + def set_trailing_metadata(self, trailing_metadata): |
| 31 | + pass |
| 32 | + |
| 33 | + def abort(self, code, details): |
| 34 | + pass |
| 35 | + |
| 36 | + def abort_with_status(self, status): |
| 37 | + pass |
| 38 | + |
| 39 | + def set_code(self, code): |
| 40 | + pass |
| 41 | + |
| 42 | + def set_details(self, details): |
| 43 | + pass |
| 44 | + |
| 45 | + def disable_next_message_compression(self): |
| 46 | + pass |
| 47 | + |
| 48 | + def is_active(self): |
| 49 | + return True |
| 50 | + |
| 51 | + def time_remaining(self): |
| 52 | + return None |
| 53 | + |
| 54 | + def cancel(self): |
| 55 | + pass |
| 56 | + |
| 57 | + def add_callback(self, callback): |
| 58 | + pass |
| 59 | + |
| 60 | + |
| 61 | +def call_with_retry(call, max_retry_times=None, retry_interval=1): |
| 62 | + retry_times = 0 |
| 63 | + while True: |
| 64 | + try: |
| 65 | + retry_times += 1 |
| 66 | + return call() |
| 67 | + except grpc.RpcError as e: |
| 68 | + if max_retry_times is None or retry_times < max_retry_times: |
| 69 | + logging.warning( |
| 70 | + "grpc call error, status: %s" |
| 71 | + ", details: %s, wait %ds for retry", e.code(), e.details(), |
| 72 | + retry_interval) |
| 73 | + time.sleep(retry_interval) |
| 74 | + else: |
| 75 | + raise e |
| 76 | + |
| 77 | + |
| 78 | +#def remote_insecure_channel(address, options=None, compression=None): |
| 79 | +# EGRESS_URL = os.getenv('EGRESS_URL', None) |
| 80 | +# EGRESS_HOST = os.environ.get('EGRESS_HOST', None) |
| 81 | +# EGRESS_DOMAIN = os.environ.get('EGRESS_DOMAIN', None) |
| 82 | +# if not EGRESS_URL: |
| 83 | +# return grpc.insecure_channel(address, options, compression) |
| 84 | +# |
| 85 | +# options = list(options) if options else list() |
| 86 | +# default_authority = EGRESS_HOST or address |
| 87 | +# options.append(('grpc.default_authority', default_authority)) |
| 88 | +# channel = grpc.insecure_channel(EGRESS_URL, options, compression) |
| 89 | +# |
| 90 | +# if EGRESS_DOMAIN: |
| 91 | +# address = address + '.' + EGRESS_DOMAIN |
| 92 | +# channel = grpc.intercept_channel( |
| 93 | +# channel, add_metadata_interceptor({'x-host': address})) |
| 94 | +# |
| 95 | +# return channel |
| 96 | +# |
| 97 | +# |
| 98 | +#def add_metadata_interceptor(headers): |
| 99 | +# if not isinstance(headers, dict): |
| 100 | +# raise TypeError("headers must be a dict") |
| 101 | +# headers = list(headers.items()) |
| 102 | +# |
| 103 | +# def add_metadata_fn(client_call_details, request_iterator, |
| 104 | +# request_streaming, response_streaming): |
| 105 | +# metadata = list(client_call_details.metadata or []) |
| 106 | +# metadata.extend(headers) |
| 107 | +# client_call_details = _ClientCallDetails( |
| 108 | +# client_call_details.method, client_call_details.timeout, metadata, |
| 109 | +# client_call_details.credentials) |
| 110 | +# return client_call_details, request_iterator, None |
| 111 | +# |
| 112 | +# return _GenericClientInterceptor(add_metadata_fn) |
| 113 | + |
| 114 | + |
| 115 | +class _ClientCallDetails( |
| 116 | + collections.namedtuple( |
| 117 | + '_ClientCallDetails', |
| 118 | + ('method', 'timeout', 'metadata', 'credentials')), |
| 119 | + grpc.ClientCallDetails): |
| 120 | + pass |
| 121 | + |
| 122 | + |
| 123 | +class _GenericClientInterceptor(grpc.UnaryUnaryClientInterceptor, |
| 124 | + grpc.UnaryStreamClientInterceptor, |
| 125 | + grpc.StreamUnaryClientInterceptor, |
| 126 | + grpc.StreamStreamClientInterceptor): |
| 127 | + |
| 128 | + def __init__(self, interceptor_function): |
| 129 | + self._fn = interceptor_function |
| 130 | + |
| 131 | + def intercept_unary_unary(self, continuation, client_call_details, |
| 132 | + request): |
| 133 | + new_details, new_request_iterator, postprocess = self._fn( |
| 134 | + client_call_details, iter((request, )), False, False) |
| 135 | + response = continuation(new_details, next(new_request_iterator)) |
| 136 | + return postprocess(response) if postprocess else response |
| 137 | + |
| 138 | + def intercept_unary_stream(self, continuation, client_call_details, |
| 139 | + request): |
| 140 | + new_details, new_request_iterator, postprocess = self._fn( |
| 141 | + client_call_details, iter((request, )), False, True) |
| 142 | + response_it = continuation(new_details, next(new_request_iterator)) |
| 143 | + return postprocess(response_it) if postprocess else response_it |
| 144 | + |
| 145 | + def intercept_stream_unary(self, continuation, client_call_details, |
| 146 | + request_iterator): |
| 147 | + new_details, new_request_iterator, postprocess = self._fn( |
| 148 | + client_call_details, request_iterator, True, False) |
| 149 | + response = continuation(new_details, new_request_iterator) |
| 150 | + return postprocess(response) if postprocess else response |
| 151 | + |
| 152 | + def intercept_stream_stream(self, continuation, client_call_details, |
| 153 | + request_iterator): |
| 154 | + new_details, new_request_iterator, postprocess = self._fn( |
| 155 | + client_call_details, request_iterator, True, True) |
| 156 | + response_it = continuation(new_details, new_request_iterator) |
| 157 | + return postprocess(response_it) if postprocess else response_it |
0 commit comments