Skip to content

Commit

Permalink
Removed lock around SES connection
Browse files Browse the repository at this point in the history
  • Loading branch information
hmarr committed Mar 6, 2011
1 parent 5fd4fb1 commit ab70ac4
Showing 1 changed file with 26 additions and 29 deletions.
55 changes: 26 additions & 29 deletions django_ses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def __init__(self, fail_silently=False, *args, **kwargs):
SESConnection.DefaultHost)

self.connection = None
self._lock = threading.RLock()

def open(self):
"""Create a connection to the AWS API server. This can be reused for
Expand Down Expand Up @@ -60,33 +59,31 @@ def send_messages(self, email_messages):
if not email_messages:
return

self._lock.acquire()
try:
new_conn_created = self.open()
if not self.connection:
# Failed silently
return

num_sent = 0
for message in email_messages:
try:
response = self.connection.send_raw_email(
source=message.from_email,
destinations=message.recipients(),
raw_message=message.message().as_string(),
)
send_result = response['SendRawEmailResponse']['SendRawEmailResult']
message.extra_headers['Message-Id'] = send_result['MessageId']
num_sent += 1
except SESConnection.ResponseError:
if not self.fail_silently:
raise
pass

if new_conn_created:
self.close()

finally:
self._lock.release()
new_conn_created = self.open()
if not self.connection:
# Failed silently
return

num_sent = 0
for message in email_messages:
try:
response = self.connection.send_raw_email(
source=message.from_email,
destinations=message.recipients(),
raw_message=message.message().as_string(),
)
send_response = response['SendRawEmailResponse']
send_result = send_response['SendRawEmailResult']
message.extra_headers['Message-Id'] = send_result['MessageId']

num_sent += 1
except SESConnection.ResponseError:
if not self.fail_silently:
raise
pass

if new_conn_created:
self.close()

return num_sent

0 comments on commit ab70ac4

Please sign in to comment.