Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #839 from mozilla-services/bug/821
Browse files Browse the repository at this point in the history
feat: Add timeout for internally routed messages.
  • Loading branch information
bbangert authored Mar 14, 2017
2 parents a065467 + 44d0285 commit 2e06521
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 7 deletions.
4 changes: 4 additions & 0 deletions autopush/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ def add_shared_args(parser):
parser.add_argument('--router_write_throughput',
help="DynamoDB router write throughput",
type=int, default=5, env_var="ROUTER_WRITE_THROUGHPUT")
parser.add_argument('--connection_timeout',
help="Seconds to wait for connection timeout",
type=int, default=1, env_var="CONNECTION_TIMEOUT")
parser.add_argument('--max_data', help="Max data segment length in bytes",
default=4096, env_var='MAX_DATA')
parser.add_argument('--env',
Expand Down Expand Up @@ -448,6 +451,7 @@ def make_settings(args, **kwargs):
ami_id=ami_id,
client_certs=client_certs,
msg_limit=args.msg_limit,
connect_timeout=args.connection_timeout,
**kwargs
)

Expand Down
3 changes: 2 additions & 1 deletion autopush/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def __init__(self, response, deferred):
def ignore(cls, response):
"""Class method helper for ignoring the response"""
d = Deferred()
response.deliverBody(cls(response, d))
if response is not None:
response.deliverBody(cls(response, d))
return d

def dataReceived(self, data):
Expand Down
6 changes: 4 additions & 2 deletions autopush/router/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from twisted.internet.defer import (
inlineCallbacks,
returnValue,
CancelledError,
)
from twisted.internet.error import (
ConnectError,
Expand Down Expand Up @@ -81,13 +82,14 @@ def route_notification(self, notification, uaid_data):
try:
result = yield self._send_notification(uaid, node_id,
notification)
except (ConnectError, ConnectionClosed, ResponseFailed) as exc:
except (ConnectError, ConnectionClosed, ResponseFailed,
CancelledError) as exc:
self.metrics.increment("updates.client.host_gone")
yield deferToThread(router.clear_node,
uaid_data).addErrback(self._eat_db_err)
if isinstance(exc, ConnectionRefusedError):
# Occurs if an IP record is now used by some other node
# in AWS.
# in AWS or if the connection timesout.
self.log.debug("Could not route message: {exc}", exc=exc)
if result and result.code == 200:
self.metrics.increment("router.broadcast.hit")
Expand Down
6 changes: 3 additions & 3 deletions autopush/router/webpush.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ def _send_notification(self, uaid, node_id, notification):
payload = notification.serialize()
payload["timestamp"] = int(time.time())
url = node_id + "/push/" + uaid
d = self.ap_settings.agent.request(
request = self.ap_settings.agent.request(
"PUT",
url.encode("utf8"),
bodyProducer=FileBodyProducer(StringIO(json.dumps(payload))),
)
d.addCallback(IgnoreBody.ignore)
return d
request.addCallback(IgnoreBody.ignore)
return request

def _save_notification(self, uaid_data, notification):
"""Saves a notification, returns a deferred.
Expand Down
4 changes: 3 additions & 1 deletion autopush/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def __init__(self,
client_certs=None,
msg_limit=100,
debug=False,
connect_timeout=0.5,
):
"""Initialize the Settings object
Expand All @@ -105,7 +106,8 @@ def __init__(self,
if not debug:
pool._factory = QuietClientFactory

self.agent = Agent(reactor, connectTimeout=5, pool=pool)
self.agent = Agent(reactor, connectTimeout=connect_timeout,
pool=pool)

if not crypto_key:
crypto_key = [Fernet.generate_key()]
Expand Down
1 change: 1 addition & 0 deletions autopush/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ class TestArg:
partner2=["2B:"*31 + "E8",
"3C:"*31 + "D7"])
client_certs = json.dumps(_client_certs)
connection_timeout = 1

def setUp(self):
patchers = [
Expand Down

0 comments on commit 2e06521

Please sign in to comment.