commit e1fe0b98202d07aaacb14d0dc18bce8080642e67 Author: ilv ilv@users.noreply.github.com Date: Thu Aug 27 15:14:55 2015 -0300
Better debugging and error handling --- gettor/blacklist.py | 65 ++++++++++++++++++++++++--------------------------- gettor/db.py | 7 +++++- 2 files changed, 37 insertions(+), 35 deletions(-)
diff --git a/gettor/blacklist.py b/gettor/blacklist.py index a16c764..635d9a7 100644 --- a/gettor/blacklist.py +++ b/gettor/blacklist.py @@ -120,44 +120,41 @@ class Blacklist(object): :raise: BlacklistError if the user is blacklisted
""" - r = self.db.get_user(user, service) - if r: - # permanently blacklisted - if r['blocked']: - try: + try: + self.log.info("Trying to get info from user") + self.db.connect() + r = self.db.get_user(user, service) + if r: + # permanently blacklisted + if r['blocked']: + self.log.warning("Request from user permanently blocked") self.db.update_user(user, service, r['times']+1, 1) raise BlacklistError("Blocked user") - except db.DBError as e: - raise InternalError("Can't update user (%s)" % str(e)) - # don't be greedy - elif r['times'] >= max_req: - last = datetime.datetime.fromtimestamp( - float(r['last_request']) - ) - next = last + datetime.timedelta(minutes=wait_time) - - if datetime.datetime.now() < next: - # too many requests from the same user - try: + # don't be greedy + elif r['times'] >= max_req: + last = datetime.datetime.fromtimestamp( + float(r['last_request']) + ) + next = last + datetime.timedelta(minutes=wait_time) + + if datetime.datetime.now() < next: + self.log.warning("Too many requests from same user") self.db.update_user(user, service, r['times']+1, 0) raise BlacklistError("Too many requests") - except db.DBError as e: - raise InternalError("Can't update user (%s)" % str(e)) - else: - # fresh user again! - try: + else: + # fresh user again! + self.log.info("Updating counter for existing user") self.db.update_user(user, service, 1, 0) - except db.DBError as e: - raise InternalError("Can't update user (%s)" % str(e)) - else: - # adding up a request for user - try: + else: + # adding up a request for user + self.log.info("Request from existing user") self.db.update_user(user, service, r['times']+1, 0) - except db.DBError as e: - raise InternalError("Can't update user (%s)" % str(e)) - else: - # new request for user - try: + else: + # new request for user + self.log.info("Request from new user") self.db.add_user(user, service, 0) - except db.DBError as e: - raise InternalError("Can't add new user (%s)" % str(e)) + except db.DBError as e: + self.log.error("Something failed!") + raise InternalError("Error with database (%s)" % str(e)) + except BlacklistError as e: + raise BlacklistError(e) diff --git a/gettor/db.py b/gettor/db.py index 0b971af..7a595a3 100644 --- a/gettor/db.py +++ b/gettor/db.py @@ -44,8 +44,13 @@ class DB(object): :param: dbname (string) the path of the database.
""" + self.dbname = dbname + + + def connect(self): + """ """ try: - self.con = sqlite3.connect(dbname) + self.con = sqlite3.connect(self.dbname) self.con.row_factory = sqlite3.Row except sqlite3.Error as e: raise DBError("%s" % str(e))
tor-commits@lists.torproject.org