Skip to content

Commit

Permalink
fix bug lost appid
Browse files Browse the repository at this point in the history
  • Loading branch information
xxnet committed Dec 19, 2015
1 parent 4315b51 commit 95b4c94
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions gae_proxy/local/appids_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
Expand All @@ -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()

0 comments on commit 95b4c94

Please sign in to comment.