def __init__(self): self.hosts = [ "xxnet2.herokuapp.com", "xxnet3.herokuapp.com", "xxnet4.herokuapp.com", "xxnet5.herokuapp.com" ] self.host = str(random.choice(self.hosts)) self.dispatcher = http_dispatcher.HttpsDispatcher( self.host, self.log_debug_data) self.last_success_time = time.time() self.last_fail_time = 0 self.continue_fail_num = 0 self.success_num = 0 self.fail_num = 0 self.rtts = collections.deque([(0, time.time())]) self.rtts_lock = threading.Lock() self.traffics = collections.deque() self.traffics_lock = threading.Lock() self.recent_sent = 0 self.recent_received = 0 self.total_sent = 0 self.total_received = 0 threading.Thread(target=self.debug_data_clearup_thread).start()
def request(self, method, host, path="/", headers={}, data="", timeout=120): if host not in self.dispatchs: self.dispatchs[host] = http_dispatcher.HttpsDispatcher(host, self.log_debug_data) self.last_host = host dispatcher = self.dispatchs[host] response = dispatcher.request(method, host, path, dict(headers), data, timeout=timeout) status = response.status if status not in [200, 405]: # xlog.warn("front request %s %s%s fail, status:%d", method, host, path, status) self.fail_num += 1 self.continue_fail_num += 1 self.last_fail_time = time.time() else: self.success_num += 1 self.continue_fail_num = 0 content = response.task.read_all() if status == 200: xlog.debug("%s %s%s status:%d trace:%s", method, response.worker.ssl_sock.host, path, status, response.task.get_trace()) else: xlog.warn("%s %s%s status:%d trace:%s", method, response.worker.ssl_sock.host, path, status, response.task.get_trace()) return content, status, response
def worker_num(self): host = self.last_host if host not in self.dispatchs: self.dispatchs[host] = http_dispatcher.HttpsDispatcher(host, self.log_debug_data) dispatcher = self.dispatchs[host] return len(dispatcher.workers)
def request(self, method, host, path="/", header={}, data="", timeout=120): if host not in self.dispatchs: self.dispatchs[host] = http_dispatcher.HttpsDispatcher(host) dispatcher = self.dispatchs[host] start_time = time.time() while time.time() - start_time < timeout: try: response = dispatcher.request(method, host, path, header, data, timeout=timeout) status = response.status if status not in [200, 405]: xlog.warn("front request %s %s%s fail, status:%d", method, host, path, status) continue content = response.task.read_all() xlog.debug("%s %s%s trace:%s", method, response.ssl_sock.host, path, response.task.get_trace()) return content, status, response except Exception as e: xlog.warn("front request %s %s%s fail:%r", method, host, path, e) continue return "", 500, {}
def get_score(self, host): if host not in self.dispatchs: self.dispatchs[host] = http_dispatcher.HttpsDispatcher(host) dispatcher = self.dispatchs[host] worker = dispatcher.get_worker(nowait=True) if not worker: return None return worker.get_score()
def __init__(self): self.hosts = ["xxnet3.herokuapp.com"] self.host = str(random.choice(self.hosts)) self.dispatcher = http_dispatcher.HttpsDispatcher(self.host) self.last_success_time = time.time() self.last_fail_time = 0 self.continue_fail_num = 0 self.success_num = 0 self.fail_num = 0
def request(self, method, host, path="/", headers={}, data="", timeout=120): self.processed_reqs += 1 if host not in self.dispatchs: self.dispatchs[host] = http_dispatcher.HttpsDispatcher(host) self.last_host = host dispatcher = self.dispatchs[host] start_time = time.time() while time.time() - start_time < timeout: try: response = dispatcher.request(method, host, path, headers, data, timeout=timeout) status = response.status content = response.task.read_all() if status not in [200]: xlog.warn("front request %s %s%s fail, status:%d trace:%s", method, host, path, status, response.task.get_trace()) self.last_fail_time = time.time() self.continue_fail_num += 1 self.fail_num += 1 continue xlog.debug("%s %s%s trace:%s", method, response.ssl_sock.host, path, response.task.get_trace()) self.last_success_time = time.time() self.continue_fail_num = 0 self.success_num += 1 return content, status, response except Exception as e: self.last_fail_time = time.time() self.continue_fail_num += 1 self.fail_num += 1 xlog.warn("front request %s %s%s fail:%r", method, host, path, e) continue return "", 500, {}
def get_score(self, host=None): now = time.time() if now - self.last_fail_time < 60 and \ self.continue_fail_num > 10: return None if host is None: host = self.last_host if host not in self.dispatchs: self.dispatchs[host] = http_dispatcher.HttpsDispatcher(host, self.log_debug_data) dispatcher = self.dispatchs[host] worker = dispatcher.get_worker(nowait=True) if not worker: return None return worker.get_score()
def request(self, method, host, path, headers, body): if host not in self.dispatchs: self.dispatchs[host] = http_dispatcher.HttpsDispatcher(host) dispatcher = self.dispatchs[host] return dispatcher.request(method, host, path, headers, body)