Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
2 changed files
with
56 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters