/head: changeset 93

Optimized acquireconnection to try to return the last connection owned
by this thread
This commit is contained in:
jgoerzen 2002-07-11 04:51:20 +01:00
parent 120076256f
commit 8029c6a25e
2 changed files with 27 additions and 9 deletions

View File

@ -8,8 +8,9 @@ offlineimap (2.0.8) unstable; urgency=low
bug.
* Modified Maildir folder to unlink messages with T flag in
cachemessagelist()
* My own box now syncs in 3 seconds.
-- John Goerzen <jgoerzen@complete.org> Wed, 10 Jul 2002 12:29:30 -0500
-- John Goerzen <jgoerzen@complete.org> Tue, 9 Jul 2002 23:29:30 -0500
offlineimap (2.0.7) unstable; urgency=low

View File

@ -18,6 +18,7 @@
from offlineimap import imaplib, imaputil, threadutil
from threading import *
import thread
class UsefulIMAPMixIn:
def getstate(self):
@ -64,6 +65,7 @@ class IMAPServer:
self.maxconnections = maxconnections
self.availableconnections = []
self.assignedconnections = []
self.lastowner = {}
self.semaphore = BoundedSemaphore(self.maxconnections)
self.connectionlock = Lock()
self.reference = reference
@ -97,9 +99,22 @@ class IMAPServer:
imapobj = None
if len(self.availableconnections): # One is available.
imapobj = self.availableconnections[0]
# Try to find one that previously belonged to this thread
# as an optimization. Start from the back since that's where
# they're popped on.
threadid = thread.get_ident()
imapobj = None
for i in range(len(self.availableconnections) - 1, -1, -1):
tryobj = self.availableconnections[i]
if self.lastowner[tryobj] == threadid:
imapobj = tryobj
del(self.availableconnections[i])
break
if not imapobj:
imapobj = self.availableconnections[0]
del(self.availableconnections[0])
self.assignedconnections.append(imapobj)
del(self.availableconnections[0])
self.lastowner[imapobj] = thread.get_ident()
self.connectionlock.release()
return imapobj
@ -124,6 +139,7 @@ class IMAPServer:
self.connectionlock.acquire()
self.assignedconnections.append(imapobj)
self.lastowner[imapobj] = thread.get_ident()
self.connectionlock.release()
return imapobj
@ -146,6 +162,7 @@ class IMAPServer:
imapobj.logout()
self.assignedconnections = []
self.availableconnections = []
self.lastowner = {}
self.connectionlock.release()
def keepalive(self, timeout, event):
@ -168,14 +185,14 @@ class IMAPServer:
for i in range(numconnections):
imapobj = self.acquireconnection()
imapobjs.append(imapobj)
thread = threadutil.ExitNotifyThread(target = imapobj.noop)
thread.setDaemon(1)
thread.start()
threads.append(thread)
thr = threadutil.ExitNotifyThread(target = imapobj.noop)
thr.setDaemon(1)
thr.start()
threads.append(thr)
for thread in threads:
for thr in threads:
# Make sure all the commands have completed.
thread.join()
thr.join()
for imapobj in imapobjs:
self.releaseconnection(imapobj)