From 95b4c94a5c3f4da2c8d993e3f522b07e302c3908 Mon Sep 17 00:00:00 2001 From: "Michael.X" Date: Sat, 19 Dec 2015 15:58:15 +0800 Subject: [PATCH] fix bug lost appid --- gae_proxy/local/appids_manager.py | 47 +++++++++++++++++++------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/gae_proxy/local/appids_manager.py b/gae_proxy/local/appids_manager.py index 47e459df26..8c485955b8 100644 --- a/gae_proxy/local/appids_manager.py +++ b/gae_proxy/local/appids_manager.py @@ -3,23 +3,47 @@ import random import threading +import time + from config import config from proxy import xlog + class APPID_manager(object): lock = threading.Lock() def __init__(self): - self.reset_appid() + if len(config.GAE_APPIDS) == 0: + xlog.error("No usable appid left, add new appid to continue use GAEProxy") + return + + self.lock.acquire() + try: + self.working_appid_list = list(config.GAE_APPIDS) + self.not_exist_appids = [] + self.out_of_quota_appids = [] + finally: + self.lock.release() + + self.last_reset_time = 0 def get_appid(self): if len(self.working_appid_list) == 0: - xlog.error("No usable appid left, add new appid to continue use GAEProxy") - return None - else: - return random.choice(self.working_appid_list) + if time.time() - self.last_reset_time < 60: + xlog.warn("all appid out of quota, need 1 min to reset") + return None + else: + xlog.warn("reset appid") + self.lock.acquire() + self.working_appid_list = list(config.GAE_APPIDS) + self.out_of_quota_appids = [] + self.lock.release() + self.last_reset_time = time.time() + + return random.choice(self.working_appid_list) def report_out_of_quota(self, appid): + xlog.warn("report_out_of_quota:%s", appid) self.lock.acquire() try: if appid not in self.out_of_quota_appids: @@ -30,10 +54,6 @@ def report_out_of_quota(self, appid): finally: self.lock.release() - if len(self.working_appid_list) == 0: - self.working_appid_list = config.GAE_APPIDS - self.out_of_quota_appids = [] - def report_not_exist(self, appid): xlog.warn("APPID_manager, report_not_exist %s", appid) self.lock.acquire() @@ -55,14 +75,5 @@ def appid_exist(self, appids): return True return False - def reset_appid(self): - #xlog.debug("reset_appid") - self.lock.acquire() - try: - self.working_appid_list = config.GAE_APPIDS - self.not_exist_appids = [] - self.out_of_quota_appids = [] - finally: - self.lock.release() appid_manager = APPID_manager()