Selaa lähdekoodia

Throttle POP pulling based on time and space (not the physics kind)

version-14
Anand Doshi 11 vuotta sitten
vanhempi
commit
8a3be21704
2 muutettua tiedostoa jossa 17 lisäystä ja 6 poistoa
  1. +2
    -2
      conf/conf.py
  2. +15
    -4
      webnotes/utils/email_lib/receive.py

+ 2
- 2
conf/conf.py Näytä tiedosto

@@ -18,8 +18,8 @@ max_file_size = 1000000
# max email size in bytes
max_email_size = 0

# total pop session timeout in seconds
pop_timeout = 0
# max total email pulling time in seconds
max_email_time = 0

# generate schema (.txt files)
developer_mode = 0


+ 15
- 4
webnotes/utils/email_lib/receive.py Näytä tiedosto

@@ -7,9 +7,11 @@ import poplib
import webnotes
from webnotes.utils import extract_email_id, convert_utc_to_user_timezone, now, cint
from webnotes.utils.scheduler import log
import time

class EmailSizeExceededError(webnotes.ValidationError): pass
class TotalSizeExceededError(webnotes.ValidationError): pass
class TotalTimeExceededError(webnotes.ValidationError): pass

class IncomingMail:
"""
@@ -157,6 +159,10 @@ class POP3Mailbox:
# WARNING: Hard coded max no. of messages to be popped
if num > 20: num = 20
# time limits
self.start_time = time.time()
self.max_email_time = cint(webnotes.local.conf.get("max_email_time"))
# size limits
self.total_size = 0
self.max_email_size = cint(webnotes.local.conf.get("max_email_size"))
@@ -169,7 +175,7 @@ class POP3Mailbox:
try:
self.retrieve_message(pop_meta, i+1)
except TotalSizeExceededError:
except (TotalSizeExceededError, TotalTimeExceededError):
break
# WARNING: Mark as read - message number 101 onwards from the pop list
@@ -185,7 +191,7 @@ class POP3Mailbox:
def retrieve_message(self, pop_meta, msg_num):
incoming_mail = None
try:
self.validate_size(pop_meta)
self.validate_pop(pop_meta)
msg = self.pop.retr(msg_num)

incoming_mail = IncomingMail(b'\n'.join(msg[1]))
@@ -193,7 +199,7 @@ class POP3Mailbox:
self.process_message(incoming_mail)
webnotes.conn.commit()
except TotalSizeExceededError:
except (TotalSizeExceededError, TotalTimeExceededError):
# propagate this error to break the loop
raise
@@ -207,7 +213,12 @@ class POP3Mailbox:
else:
self.pop.dele(msg_num)
def validate_size(self, pop_meta):
def validate_pop(self, pop_meta):
# throttle based on time restriction
if self.max_email_time and (time.time() - self.start_time) > self.max_email_time:
raise TotalTimeExceededError
# throttle based on email size
if not self.max_email_size:
return


Ladataan…
Peruuta
Tallenna