Use python locking to avoid busy wait.
This commit is contained in:
+7
-1
@@ -2,10 +2,13 @@ import time
|
|||||||
import logging
|
import logging
|
||||||
import urllib
|
import urllib
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
import thread
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
log = logging.getLogger('milter.greylist')
|
log = logging.getLogger('milter.greylist')
|
||||||
|
|
||||||
|
_db_lock = thread.allocate_lock()
|
||||||
|
|
||||||
class Greylist(object):
|
class Greylist(object):
|
||||||
|
|
||||||
def __init__(self,dbname,grey_time=10,grey_expire=4,grey_retain=36):
|
def __init__(self,dbname,grey_time=10,grey_expire=4,grey_retain=36):
|
||||||
@@ -35,6 +38,7 @@ class Greylist(object):
|
|||||||
|
|
||||||
def check(self,ip,sender,recipient,timeinc=0):
|
def check(self,ip,sender,recipient,timeinc=0):
|
||||||
"Return number of allowed messages for greylist triple."
|
"Return number of allowed messages for greylist triple."
|
||||||
|
_db_lock.acquire()
|
||||||
cur = self.conn.execute('begin immediate')
|
cur = self.conn.execute('begin immediate')
|
||||||
try:
|
try:
|
||||||
cur.execute('''select firstseen,lastseen,cnt,umis from greylist where
|
cur.execute('''select firstseen,lastseen,cnt,umis from greylist where
|
||||||
@@ -73,7 +77,9 @@ class Greylist(object):
|
|||||||
where ip=? and sender=? and recipient=?''',
|
where ip=? and sender=? and recipient=?''',
|
||||||
(now,now,0,None,ip,sender,recipient))
|
(now,now,0,None,ip,sender,recipient))
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
finally: cur.close()
|
finally:
|
||||||
|
cur.close()
|
||||||
|
_db_lock.release()
|
||||||
return cnt
|
return cnt
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user