def test_api(self): print('# Initialize test API') a = Api('test-api') a.use_port(GRPC_PORT) self.assertEqual(a.status, Status.UNKNOWN, 'New API failed: wrong status') print('# Register services') example.add_ServiceServicer_to_server(ExampleServer(), a.server) print('# Start API') a.start() self.assertEqual(a.status, Status.SERVING, 'Start API failed: wrong status') print('# Dial API') with grpc.insecure_channel(f'localhost:{GRPC_PORT}') as conn: print('# Initialize health client') self.health_client = health_pb2_grpc.HealthStub(conn) self.__call_health_check() print('# Initialize main client') self.client = example.ServiceStub(conn) self.__call_simple_request() self.__call_server_stream() self.__call_client_stream() self.__call_bidirectional_stream() print('# Stop API') a.stop() self.assertEqual(a.status, Status.NOT_SERVING, 'Stop API failed: wrong status')
def send_health_check_sync( target: str, timeout=100.0, tls=False, root_certificates: Optional[str] = None, ) -> health_pb2.HealthCheckResponse: """ Sends a request synchronously to the target via grpc :param target: where to send the request to, like 127.0.0.1:8080 :param timeout: timeout for the send :param tls: if True, use tls encryption for the grpc channel :param root_certificates: the path to the root certificates for tls, only used if tls is True :returns: the response health check """ for i in range(3): try: with GrpcConnectionPool.get_grpc_channel( target, tls=tls, root_certificates=root_certificates, ) as channel: health_check_req = health_pb2.HealthCheckRequest() health_check_req.service = '' stub = health_pb2_grpc.HealthStub(channel) return stub.Check(health_check_req, timeout=timeout) except grpc.RpcError as e: if e.code() != grpc.StatusCode.UNAVAILABLE or i == 2: raise
def __init__(self, host="localhost", port="50051", src='', targ='', osrc='', otarg=''): port = str(port) self.addr = "{!s}:{!s}".format(host, port) self.src = src self.targ = targ self.osrc = osrc self.otarg = otarg self.stream = StreamingClient(host=host, port=port) if bool(src) != bool(targ): raise ValueError( 'src->targ mapping specified, but one end is None: {}->{}'. format(src, targ)) if bool(osrc) != bool(otarg): raise ValueError( 'osrc->otarg mapping specified, but one end is None: {}->{}'. format(osrc, otarg)) channel = grpc.insecure_channel(self.addr) super(MediforClient, self).__init__(channel) self.health_stub = health_pb2_grpc.HealthStub(channel)
def __init__(self, host=HOST_NAME, port=HOST_PORT, proto_http_port=HTTP_PROTO_PORT, proto_path=PROTO_PATH, update_pb_class=PROTO_UPDATE): """ Initialize channel and stub objects. Params: :param: host: string, default='localhost' :param: port: string, default='5000' (default of the SSM server service) :param: proto_http_port: string, default='8001' (default of the HTTP server) :param: proto_path: string, default='/static/proto/opac.proto' (default path to proto file) """ if update_pb_class: utils.generate_pb_files(host, proto_http_port, proto_path) reload(opac_pb2_grpc) options = [('grpc.max_receive_message_length', MAX_RECEIVE_MESSAGE_LENGTH), ('grpc.max_send_message_length', MAX_SEND_MESSAGE_LENGTH)] self.channel = grpc.insecure_channel('{0}:{1}'.format(host, port), options) self.stubAsset = opac_pb2_grpc.AssetServiceStub(self.channel) self.stubBucket = opac_pb2_grpc.BucketServiceStub(self.channel) self.stubHealth = health_pb2_grpc.HealthStub(self.channel)
def __init__(self, host="localhost", port="50051"): port = str(port) self.addr = "{!s}:{!s}".format(host, port) channel = grpc.insecure_channel(self.addr) super(StreamingClient, self).__init__(channel) self.health_stub = health_pb2_grpc.HealthStub(channel)
def main(): hostname = "merger-grpc:50051" if USE_SSL: config = LocalDockerSSLConfig() channel = grpc.secure_channel(hostname, config.ssl_channel_credentials) else: channel = grpc.insecure_channel(hostname) print("Doing health check") health_stub = health_pb2_grpc.HealthStub(channel) response = health_stub.Check( health_pb2.HealthCheckRequest(service="Merger")) print(response) # Grabs the collections protobubs from the files that indexing_job.py wrote to ./data collections_files = [ "collection_1_data_source.pbtxt", "collection_2_data_source.pbtxt" ] collections = [] for file in collections_files: with open(os.path.join(data_dir, file), "r") as f: collection = text_format.Parse(f.read(), collections_pb2.Collection()) collections.append(collection) print("Adding collections to cluster") stub = servicers_pb2_grpc.MergerStub(channel) response = stub.CollectionsAdd( collections_pb2.CollectionsAddRequest(collections=collections)) print(response)
def create_health_stub(host, port): try: channel = grpc.insecure_channel("{}:{}".format(host, port)) stub = heartb_pb2_grpc.HealthStub(channel) return stub except Exception as e: log.error(e)
def health_check(self): try: health_stub = health_pb2_grpc.HealthStub(self.channel) request = health_pb2.HealthCheckRequest() return health_stub.Check(request) except grpc.RpcError as e: logging.error("Failed health check due to error: {}".format(e)) return None
def _service_status(self, url, secure=True): try: channel = open_grpc_channel(endpoint=url) stub = heartb_pb2_grpc.HealthStub(channel) response = stub.Check(heartb_pb2.HealthCheckRequest(service=""), timeout=10) if response != None and response.status == 1: return True return False except Exception as e: return False
async def setUp(self): self._servicer = health.aio.HealthServicer() await self._servicer.set(_SERVING_SERVICE, health_pb2.HealthCheckResponse.SERVING) await self._servicer.set(_UNKNOWN_SERVICE, health_pb2.HealthCheckResponse.UNKNOWN) await self._servicer.set(_NOT_SERVING_SERVICE, health_pb2.HealthCheckResponse.NOT_SERVING) self._server = aio.server() port = self._server.add_insecure_port('[::]:0') health_pb2_grpc.add_HealthServicer_to_server(self._servicer, self._server) await self._server.start() self._channel = aio.insecure_channel('localhost:%d' % port) self._stub = health_pb2_grpc.HealthStub(self._channel)
def setUp(self): servicer = health.HealthServicer() servicer.set('', health_pb2.HealthCheckResponse.SERVING) servicer.set('grpc.test.TestServiceServing', health_pb2.HealthCheckResponse.SERVING) servicer.set('grpc.test.TestServiceUnknown', health_pb2.HealthCheckResponse.UNKNOWN) servicer.set('grpc.test.TestServiceNotServing', health_pb2.HealthCheckResponse.NOT_SERVING) self._server = test_common.test_server() port = self._server.add_insecure_port('[::]:0') health_pb2_grpc.add_HealthServicer_to_server(servicer, self._server) self._server.start() channel = grpc.insecure_channel('localhost:%d' % port) self._stub = health_pb2_grpc.HealthStub(channel)
def __init__(self, eqaddr='localhost:37706'): """Create an EntroQ client (over gRPC). Args: eqaddr: Hostport of the address of an EntroQ gRPC service. """ self.addr = eqaddr self.claimant_id = str(uuid.uuid4()) # TODO: allow secure channels. self.channel = grpc.insecure_channel(self.addr) self.stub = entroq_pb2_grpc.EntroQStub(self.channel) self.health_stub = health_pb2_grpc.HealthStub(self.channel) # Call the server, see what time it thinks it is, calculate rough skew. now = int(time.time() * 1000) self.time_skew = self.time() - now
def _get_service_status(self, url, secure=True): try: if secure: channel = grpc.secure_channel(url, grpc.ssl_channel_credentials()) else: channel = grpc.insecure_channel(url) stub = heartb_pb2_grpc.HealthStub(channel) response = stub.Check(heartb_pb2.HealthCheckRequest(service=""), timeout=SRVC_STATUS_GRPC_TIMEOUT) if response is not None and response.status == 1: logger.info(response.status) return 1 return 0 except Exception as e: logger.info(f"error in making grpc call::url: {url}, |error: {e}") return 0
def _check_service_status(self, url, secure=True): try: if secure: channel = grpc.secure_channel(url, grpc.ssl_channel_credentials()) else: channel = grpc.insecure_channel(url) stub = heartb_pb2_grpc.HealthStub(channel) response = stub.Check(heartb_pb2.HealthCheckRequest(service=""), timeout=SRVC_STATUS_GRPC_TIMEOUT) if response != None and response.status == 1: print(response.status) return 1 return 0 except Exception as e: print("error in making grpc call::url: ", url, "|err: ", e) return 0
def create_channel(address): config = _config.Config() enable_proxy = config.get('grpc.enable_proxy', False) channel = grpc.insecure_channel( address, options=[('grpc.enable_http_proxy', enable_proxy), ('grpc.max_send_message_length', config.get('grpc.max_send_message_length')), ('grpc.max_receive_message_length', config.get('grpc.max_receive_message_length'))]) health = health_pb2_grpc.HealthStub(channel) hcr = health.Check( health_pb2.HealthCheckRequest(service=constants.EVENTS_SERVICE_NAME)) if hcr.status != health_pb2.HealthCheckResponse.SERVING: raise ValueError('Failed to connect to events service. Status:') return channel
def start_server(self, non_blocking=False, thread_pool=None): self._thread_pool = thread_pool self._servicer = health.HealthServicer( experimental_non_blocking=non_blocking, experimental_thread_pool=thread_pool) self._servicer.set(_SERVING_SERVICE, health_pb2.HealthCheckResponse.SERVING) self._servicer.set(_UNKNOWN_SERVICE, health_pb2.HealthCheckResponse.UNKNOWN) self._servicer.set(_NOT_SERVING_SERVICE, health_pb2.HealthCheckResponse.NOT_SERVING) self._server = test_common.test_server() port = self._server.add_insecure_port('[::]:0') health_pb2_grpc.add_HealthServicer_to_server( self._servicer, self._server) self._server.start() self._channel = grpc.insecure_channel('localhost:%d' % port) self._stub = health_pb2_grpc.HealthStub(self._channel)
def test_get_service_status(self, secure=True): url = "https://bh.singularitynet.io:7252" url = "138.197.215.173:5001" try: if secure: channel = grpc.secure_channel(url, grpc.ssl_channel_credentials()) else: channel = grpc.insecure_channel(url) stub = heartb_pb2_grpc.HealthStub(channel) response = stub.Check(heartb_pb2.HealthCheckRequest(service=""), timeout=10) if response is not None and response.status == 1: # logger.info(response.status) return 1 return 0 except Exception as e: # logger.info(f"error in making grpc call::url: {url}, |error: {e}") return 0
def __init__(self, *, address: Optional[str] = None, stub: Optional[events_pb2_grpc.EventsStub] = None): if stub is None: if address is None: discovery = _discovery.Discovery(Config()) address = discovery.discover_events_service('v1') channel = grpc.insecure_channel(address) health = health_pb2_grpc.HealthStub(channel) hcr = health.Check( health_pb2.HealthCheckRequest(service=EVENTS_SERVICE_NAME)) if hcr.status != health_pb2.HealthCheckResponse.SERVING: raise ValueError( 'Failed to connect to events service. Status:') self._channel = channel self.stub = events_pb2_grpc.EventsStub(channel) else: self.stub = stub self._is_open = True
def health_check(self): health_stub = health_pb2_grpc.HealthStub(self.channel) request = health_pb2.HealthCheckRequest() return health_stub.Check(request)