Skip to content

Commit

Permalink
ENH Sequence IMAP operations
Browse files Browse the repository at this point in the history
Previously, we would open a new connection for each operation.
Eventually this would lead to my IMAP server complaining: "too many
simultaneous connections". This new version also reuses connections
(with a connection close timeout).
  • Loading branch information
luispedro committed Jan 9, 2013
1 parent fe47fdd commit 9c3c35f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
40 changes: 40 additions & 0 deletions gui/resources.py
@@ -0,0 +1,40 @@
# Copyright (C) 2013 Luis Pedro Coelho <luis@luispedro.org>
# This file is part of rbit mail.
from __future__ import print_function

from contextlib import contextmanager

class imap_manager(object):
def __init__(self):
from gevent.coros import Semaphore
self.client = None
self.sem = Semaphore(1)
self.count = 0

def close(self, current):
import gevent
gevent.sleep(360)
self.sem.acquire()
if self.count == current:
self.client.close()
self.client = None
imap_sem.release()

@contextmanager
def get(self):
import gevent
self.count += 1
self.sem.acquire()
self.count += 1
if self.client is None:
from rbit import config
from rbit import backend
from rbit import imap
cfg = config.Config('config', backend.create_session)
self.client = imap.IMAPClient.from_config(cfg)
yield self.client
self.sem.release()
gevent.spawn(lambda : self.close(self.count))



26 changes: 16 additions & 10 deletions gui/tasks.py
@@ -1,4 +1,4 @@
# Copyright (C) 2012 Luis Pedro Coelho <luis@luispedro.org>
# Copyright (C) 2012-2013 Luis Pedro Coelho <luis@luispedro.org>
# This file is part of rbit mail.

from __future__ import print_function
Expand All @@ -7,6 +7,17 @@

from rbit import signals

_imap_manager = None
def get_imap():
# By importing only inside the function, we avoid having to import at
# programme start up
from resources import imap_manager
global _imap_manager
if _imap_manager is None:
_imap_manager = imap_manager()
return _imap_manager.get()


def run_from_queue(group, q):
from gevent import monkey
# thread needs to be left alone as Qt handles those
Expand Down Expand Up @@ -121,15 +132,10 @@ def __init__(self, parent, message, target):
self.target = target

def _perform(self):
from rbit import config
from rbit import backend
from rbit import imap
cfg = config.Config('config', backend.create_session)
client = imap.IMAPClient.from_config(cfg)
client.select_folder(self.folder)
client.move_messages([self.uid], self.target)
client.expunge()
client.close()
with get_imap() as client:
client.select_folder(self.folder)
client.move_messages([self.uid], self.target)
client.expunge()

class TrashMessage(MoveMessage):
def __init__(self, parent, message):
Expand Down

0 comments on commit 9c3c35f

Please sign in to comment.