def compare_sections( self, new_sections: Section) -> Tuple[Set[ConfigItem], Set[ConfigItem]]: new_vset: Set[ConfigItem] = set() vset: Set[ConfigItem] = set() for sec, section in sorted(new_sections.items()): for key, value in sorted(section.items()): value = json_to_str(value) new_vset.add((sec, key, value)) for sec, section in sorted(self.sections.items()): for key, value in sorted(section.items()): value = json_to_str(value) vset.add((sec, key, value)) will_delete = vset - new_vset will_add = new_vset - vset new_2set = set((sec, key) for (sec, key, value) in will_add) will_delete = set((sec, key, value) for (sec, key, value) in will_delete if (sec, key) not in new_2set) return will_delete, will_add
async def run(self, args): srv, method = args.srv_method.split('::') ps = [guess_json(p) for p in args.param] if args.dispatch_policy == 'random': bbox_client.pool.policy = bbox_client.pool.RANDOM try: await get_cluster().start() for i in range(args.ntimes): r = await bbox_client.pool.request(srv, method, *ps, retry=args.retry) if args.pp: print(json_pp(r)) else: print(json_to_str(r)) if (args.stack and r.get('error') and isinstance(r['error'], dict) and r['error'].get('stack')): print('\nerror stack:') print(r['error']['stack']) if i >= args.ntimes - 1: break await asyncio.sleep(args.interval) finally: c = get_cluster() c.cont = False await asyncio.sleep(0.1) c.close()
def box_info(self, extbind: str=None) -> str: extbind = extbind or self.extbind return json_to_str({ 'bind': extbind, 'start_time': datetime.now(tzlocal()), 'ssl': self.ssl_prefix, 'boxid': self.boxid, 'services': self.srv_names})
async def set_config(self, sec, key, value) -> None: assert sec and key assert '/' not in sec assert '/' not in key shared_cfg = get_sharedconfig() etcd_key = f'configs/{sec}/{key}' old_value = shared_cfg.get(sec, key) value_json = json_to_str(value) if old_value: old_value_json = json_to_str(old_value) await self.etcd_client.write(etcd_key, value_json, prevValue=old_value_json) else: await self.etcd_client.write(etcd_key, value_json, prevExist=False) shared_cfg.set(sec, key, value)
async def set_config(self, sec, key, value): assert sec and key assert '/' not in sec assert '/' not in key shared_cfg = get_sharedconfig() etcd_key = self.path('configs/{}/{}'.format(sec, key)) old_value = shared_cfg.get(sec, key) value_json = json_to_str(value) if old_value: old_value_json = json_to_str(old_value) await self.write(etcd_key, value_json, prevValue=old_value_json) else: await self.write(etcd_key, value_json, prevExist=False) shared_cfg.set(sec, key, value)
def box_info(self, extbind=None): extbind = extbind or self.extbind return json_to_str({ 'bind': extbind, 'ssl': self.ssl_prefix, 'boxid': self.boxid, 'services': self.srv_names })
async def request_obj(self, req: Request, timeout:float=DEFAULT_TIMEOUT_SECS) -> Dict[str, Any]: tmp_req_id = uuid.uuid4().hex payload = req.as_json() timeout = int(timeout) payload['id'] = tmp_req_id payload['timeout'] = timeout redis_pool = await self.get_redis_pool() await redis_pool.execute('LPUSH', self.req_key, json_to_str(payload)) # wait for response res_key = 'tunnel.res.{}'.format(tmp_req_id) # TODO: timeout r = await redis_pool.execute( 'BRPOP', res_key, timeout) if r is not None: key, res = r res = json.loads(res) res['id'] = req.req_id return res else: raise asyncio.TimeoutError from None
async def respond(self, res: Dict[str, Any], req:Request, timeout:float) -> None: redis_pool = await self.get_redis_pool() if req.req_id: res_key = 'tunnel.res.{}'.format(req.req_id) await redis_pool.execute('LPUSH', res_key, json_to_str(res)) await redis_pool.execute('EXPIRE', res_key, timeout)