async def start_server(args): boxid = args.boxid ssl_context = get_ssl_context(args.ssl) # server etcd agent srv_names = list(srv_dict.keys()) curr_box = get_box() curr_box.ssl_prefix = args.ssl await curr_box.start(boxid, srv_names) app = web.Application() app.router.add_post('/jsonrpc/2.0/api', handle) app.router.add_route('*', '/jsonrpc/2.0/ws', handle_ws) app.router.add_get('/metrics', handle_metrics) app.router.add_get('/metrics.json', handle_metrics_json) app.router.add_get('/', index) host, port = curr_box.bind.split(':') if not localbox_ip(host): host = '0.0.0.0' logger.warn('box {} launched as {}'.format(curr_box.boxid, curr_box.bind)) handler = app.make_handler() loop = asyncio.get_event_loop() srv = await loop.create_server(handler, host, port, ssl=ssl_context) return srv, handler
async def start_proxy_server(self, args:Namespace) -> None: boxid = args.boxid ssl_context = get_ssl_context(args.ssl) # server etcd agent curr_box = get_box() curr_box.ssl_prefix = args.ssl srv_names = args.srv_name await curr_box.start(boxid, srv_names) app = web.Application() app.router.add_post('/jsonrpc/2.0/api', self.proxy_requests) host, port = curr_box.bind.split(':') if not localbox_ip(host): host = '0.0.0.0' logger.warn('box {} launched as {}'.format( curr_box.boxid, curr_box.bind)) handler = app.make_handler() loop = asyncio.get_event_loop() srv = await loop.create_server( handler, host, int(port), ssl=ssl_context)
async def call_method(self, method_ref: MethodRef, srv_name:str) -> Dict[str, Any]: start_time = time.time() stats_name = '/{}/{}'.format( srv_name, self.req.method) stats.rpc_request_count.incr(stats_name) res: Any = await method_ref.fn(self, *self.req.params) resp: Dict[str, Any] = {'result': res, 'id': self.req.req_id, 'jsonrpc': '2.0'} end_time = time.time() if end_time - start_time > 1.0: logging.warn( 'long bbox method execution, ' 'method %s, box %s, used %s seconds', self.req.full_method, get_box().boxid, end_time - start_time) stats.slow_rpc_request_count.incr(stats_name) return resp
async def handle_metrics_json(request): resp = await collect_metrics() box = get_box() for name, labels, v in resp['lines']: labels['box'] = box.boxid return web.json_response(resp)
def shutdown(self) -> None: loop = asyncio.get_event_loop() for h in self.mod_handlers: h.shutdown() loop.run_until_complete(get_box().deregister())
def shutdown(self): loop = asyncio.get_event_loop() loop.run_until_complete(get_box().deregister())
async def echostr(request, msg): curr_box = get_box() if not isinstance(msg, str): raise ServiceError('invalid arg', 'msg is not str') return 'echo {} from {} {}'.format(msg, curr_box.bind, curr_box.boxid)
def shutdown(self): loop = asyncio.get_event_loop() if self.mod_handler: self.mod_handler.shutdown() loop.run_until_complete(get_box().deregister())