def watch_connections(address, event): # Sleep for 4 seconds to ensure that the Python gRPC server has started time.sleep(4) number_of_failures = 0 timeout_sec = 1 while True: # If there are three failures, shutdown the gRPC server. if number_of_failures == 3: event.set() break channel = grpc.insecure_channel(address) try: channelz_stub = channelz_pb2_grpc.ChannelzStub(channel) # Wait for the channel to be ready. grpc.channel_ready_future(channel).result(timeout=timeout_sec) except grpc.FutureTimeoutError: number_of_failures += 1 continue servers = channelz_stub.GetServers( channelz_pb2.GetServersRequest(start_server_id=0)) sockets = channelz_stub.GetServerSockets( channelz_pb2.GetServerSocketsRequest( server_id=servers.server[0].ref.server_id, start_socket_id=0)) # There should be always more than one socket connected to the server if len(sockets.socket_ref) == 1: number_of_failures += 1 else: number_of_failures = 0 # Sleep for 2 seconds before polling next time time.sleep(2)
def run(addr): with grpc.insecure_channel(addr) as channel: channelz_stub = channelz_pb2_grpc.ChannelzStub(channel) # This RPC pulls server-level metrics, like sent/received messages, # succeeded/failed RPCs. For more info see: # https://github.com/grpc/grpc/blob/master/src/proto/grpc/channelz/channelz.proto response = channelz_stub.GetServers(channelz_pb2.GetServersRequest()) print(f'Info for all servers: {response}')
def test_get_one_server(self): self._pairs = _generate_channel_server_pairs(1) gss_resp = self._channelz_stub.GetServers( channelz_pb2.GetServersRequest(start_server_id=0)) self.assertEqual(len(gss_resp.server), 1) gs_resp = self._channelz_stub.GetServer( channelz_pb2.GetServerRequest( server_id=gss_resp.server[0].ref.server_id)) self.assertEqual(gss_resp.server[0].ref.server_id, gs_resp.server.ref.server_id)
async def bind_channelz(self, channelz_stub): resp = await channelz_stub.GetTopChannels( channelz_pb2.GetTopChannelsRequest(start_channel_id=0)) for channel in resp.channel: if channel.data.target == self.address: self.channel_ref_id = channel.ref.channel_id resp = await channelz_stub.GetServers( channelz_pb2.GetServersRequest(start_server_id=0)) self.server_ref_id = resp.server[-1].ref.server_id
def test_server_listen_sockets(self): self._pairs = _generate_channel_server_pairs(1) gss_resp = self._channelz_stub.GetServers( channelz_pb2.GetServersRequest(start_server_id=0)) self.assertEqual(len(gss_resp.server), 1) self.assertEqual(len(gss_resp.server[0].listen_socket), 1) gs_resp = self._channelz_stub.GetSocket( channelz_pb2.GetSocketRequest( socket_id=gss_resp.server[0].listen_socket[0].socket_id))
def test_server_sockets(self): self._pairs = _generate_channel_server_pairs(1) self._send_successful_unary_unary(0) self._send_failed_unary_unary(0) gs_resp = self._channelz_stub.GetServers( channelz_pb2.GetServersRequest(start_server_id=0)) self.assertEqual(len(gs_resp.server), 1) self.assertEqual(gs_resp.server[0].data.calls_started, 2) self.assertEqual(gs_resp.server[0].data.calls_succeeded, 1) self.assertEqual(gs_resp.server[0].data.calls_failed, 1) gss_resp = self._channelz_stub.GetServerSockets( channelz_pb2.GetServerSocketsRequest( server_id=gs_resp.server[0].ref.server_id, start_socket_id=0))
def test_server_call(self): self._pairs = _generate_channel_server_pairs(1) k_success = 23 k_failed = 29 for i in range(k_success): self._send_successful_unary_unary(0) for i in range(k_failed): self._send_failed_unary_unary(0) resp = self._channelz_stub.GetServers( channelz_pb2.GetServersRequest(start_server_id=0)) self.assertEqual(len(resp.server), 1) self.assertEqual(resp.server[0].data.calls_started, k_success + k_failed) self.assertEqual(resp.server[0].data.calls_succeeded, k_success) self.assertEqual(resp.server[0].data.calls_failed, k_failed)
async def run(addr: str) -> None: async with grpc.aio.insecure_channel(addr) as channel: channelz_stub = channelz_pb2_grpc.ChannelzStub(channel) response = await channelz_stub.GetServers( channelz_pb2.GetServersRequest(start_server_id=0)) print('Info for all servers: %s' % response)
def test_server_basic(self): self._pairs = _generate_channel_server_pairs(1) resp = self._channelz_stub.GetServers( channelz_pb2.GetServersRequest(start_server_id=0)) self.assertEqual(len(resp.server), 1)
async def _get_server_by_ref_id(self, ref_id): """Server id may not be consecutive""" resp = await self._channelz_stub.GetServers( channelz_pb2.GetServersRequest(start_server_id=ref_id)) self.assertEqual(ref_id, resp.server[0].ref.server_id) return resp.server[0]
def run(addr): with grpc.insecure_channel(addr) as channel: channelz_stub = channelz_pb2_grpc.ChannelzStub(channel) response = channelz_stub.GetServers( channelz_pb2.GetServersRequest(start_server_id=0)) print('Info for all servers: %s' % response)