def __init__(self, hostname, ip, ports, pid, address, server_type=PHP, meta=None, get_req_stat=None, get_interval_stat=None, maxPending=-1, timeout=10): super().__init__(address, meta, maxPending) self.hostname = hostname self.ip = ip self.pid = pid self.is_ok = False self.timeout = timeout self.pingid = GrpcAgent.PINGID GrpcAgent.PINGID += 1 self.exit_cv = threading.Condition() self.stub = Service_pb2_grpc.AgentStub(self.channel) self.cmd_sub = Service_pb2_grpc.ProfilerCommandServiceStub( self.channel) self.agentinfo = PAgentInfo(hostname=hostname, ip=ip, ports=ports, pid=pid, endTimestamp=-1, serviceType=server_type) self.ping_meta = meta.append(('socketid', str(GrpcAgent.PINGID))) self.profile_meta = meta self.task_running = False assert get_req_stat self.get_req_stat = get_req_stat
def _send_thread_count(self, requestId): channel = grpc.insecure_channel(self.address) stub = Service_pb2_grpc.ProfilerCommandServiceStub(channel) def generator_cmd(): i = 0 while True: try: cmd_response = PCmdStreamResponse(responseId=requestId, sequenceId=i) cmd_response.message.value = 'hello' threadCountRes = PCmdActiveThreadCountRes( commonStreamResponse=cmd_response) threadCountRes.histogramSchemaType = 2 for stat in self.get_req_stat(): threadCountRes.activeThreadCount.append(stat) threadCountRes.timeStamp = int(time.time()) i += 1 yield threadCountRes with self.exit_cv: if self.exit_cv.wait(1): break except Exception as e: TCLogger.warning("catch exception %s", e) break try: stub.CommandStreamActiveThreadCount(generator_cmd(), metadata=self.profile_meta) TCLogger.debug("send req state requestId: %d done", requestId) except Exception as e: TCLogger.error( "CommandStreamActiveThreadCount, catch exception %s", e)
def _send_thread_count(self, requestId): channel = grpc.insecure_channel(self.address) stub = Service_pb2_grpc.ProfilerCommandServiceStub(channel) def generator_cmd(): i = 0 while self.task_running: try: cmd_response = PCmdStreamResponse(responseId=requestId, sequenceId=i) cmd_response.message.value = 'hello' threadCountRes = PCmdActiveThreadCountRes( commonStreamResponse=cmd_response) threadCountRes.histogramSchemaType = 2 for stat in self.get_req_stat(): threadCountRes.activeThreadCount.append(stat) threadCountRes.timeStamp = int(time.time()) i += 1 yield threadCountRes ## it's a templated task, don't use exit_cv time.sleep(1) except Exception as e: TCLogger.warning("catch exception %s", e) break try: TCLogger.debug("new a thread for activeThreadCound %d", requestId) stub.CommandStreamActiveThreadCount(generator_cmd(), metadata=self.profile_meta) TCLogger.debug("send activeThreadCound requestId: %d is done", requestId) channel.close() except Exception as e: TCLogger.error( "CommandStreamActiveThreadCount, catch exception %s", e)
def _connect(self): TCLogger.info("span channel connect %s with meta:%s", self.address, self.meta) self.channel = grpc.intercept_channel( grpc.insecure_channel(self.address), GrpcClient.InterceptorAddHeader(self.meta)) self.span_stub = Service_pb2_grpc.SpanStub(self.channel)
def run(self): channel = grpc.insecure_channel('localhost:50051') stub = Service_pb2_grpc.ServiceStub(channel) request = Service_pb2.CurrencySubscription(requested=[Service_pb2.PLN, Service_pb2.USD, Service_pb2.EUR]) for response in stub.subscribeCurrency(request): currency = Bank.Currency.valueOf(response.currency) currency_rates[currency] = response.rate print(currency, response.rate)
def __init__(self, address, meta, queue): super().__init__(address, meta) self.span_stub = Service_pb2_grpc.SpanStub(self.channel) self.exit_cv = Condition() self.send_span_count = 0 self.is_ok = False self.task_thead = Thread(target=self.getAndSendSpan) self.task_running = False self.queue = queue
def __init__(self, address, meta=None): super().__init__(address, meta, -1) self.meta_stub = Service_pb2_grpc.MetadataStub(self.channel) self.must_snd_meta_now = False self.id = 0 self.sql_table = {} self.api_table = {} self.string_table = {} self.recover_timer = GTimer()