Skip to content

Commit

Permalink
Use accounts randomly
Browse files Browse the repository at this point in the history
  • Loading branch information
jayme-github committed Aug 25, 2014
1 parent 2a319d5 commit 5cb3253
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions newznabbalancer/database.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import sqlite3
import os
import datetime

import logging


Expand Down Expand Up @@ -32,7 +31,7 @@ def _fallback(self, atype):
(atype, datetime.datetime.now()))
self.db.commit()

self.cur.execute('SELECT apikey, url FROM accounts WHERE isfallback = 1')
self.cur.execute('SELECT apikey, url FROM accounts WHERE isfallback = 1 ORDER BY RANDOM() LIMIT 1')
account = self.cur.fetchone()
if not account:
self.logger.warning('No fallback account defined, request will fail.')
Expand Down Expand Up @@ -78,11 +77,12 @@ def get_account(self, atype):
field = 'next'+atype
now = datetime.datetime.now()
baseSQL = 'SELECT apikey, url FROM accounts WHERE isfallback = 0 AND '
endSQL = ' ORDER BY RANDOM() LIMIT 1'
if atype == 'grab':
# A grab consumes a hit and a grab...
self.cur.execute(baseSQL + '(nextgrab IS NULL OR nextgrab < ?) AND (nexthit IS NULL OR nexthit < ?)', (now, now))
self.cur.execute(baseSQL + '(nextgrab IS NULL OR nextgrab < ?) AND (nexthit IS NULL OR nexthit < ?)' + endSQL, (now, now))
else:
self.cur.execute(baseSQL + '%s IS NULL OR %s < ? LIMIT 1' % (field, field), (now, ))
self.cur.execute(baseSQL + '%s IS NULL OR %s < ?' % (field, field) + endSQL, (now, ))
account = self.cur.fetchone()
if not account:
return self._fallback(atype)
Expand Down

0 comments on commit 5cb3253

Please sign in to comment.