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

Commit

Permalink
fix: call defer methods with callables (#737)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbangert authored Nov 17, 2016
1 parent a38ee75 commit aff8904
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion autopush/tests/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1987,7 +1987,7 @@ def test_notif_finished_with_too_many_messages(self):
d = Deferred()

def check(*args, **kwargs):
ok_(self.proto.ap_settings.router.drop_user.called)
ok_(self.proto.force_retry.called)
ok_(self.send_mock.called)
d.callback(True)

Expand Down
15 changes: 11 additions & 4 deletions autopush/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
from twisted.web._newclient import ResponseFailed
from twisted.web.resource import Resource
from typing import ( # noqa
Any,
Callable,
Dict,
List,
Optional,
Expand All @@ -79,6 +81,7 @@
from autopush.exceptions import MessageOverloadException
from autopush.noseplugin import track_object
from autopush.protocol import IgnoreBody
from autopush.settings import AutopushSettings # noqa
from autopush.utils import (
parse_user_agent,
validate_uaid,
Expand Down Expand Up @@ -273,8 +276,11 @@ class PushServerProtocol(WebSocketServerProtocol, policies.TimeoutMixin):
_log_exc = True
sent_notification_count = 0

ap_settings = None # type: AutopushSettings

# Defer helpers
def deferToThread(self, func, *args, **kwargs):
# type (Callable[..., Any], *Any, **Any) -> Deferred
"""deferToThread helper that tracks defers outstanding"""
d = deferToThread(func, *args, **kwargs)
self.ps._callbacks.append(d)
Expand All @@ -287,6 +293,7 @@ def f(result):
return d

def deferToLater(self, when, func, *args, **kwargs):
# type: (float, Callable[..., Any], *Any, **Any) -> Deferred
"""deferToLater helper that tracks defers outstanding"""
def cancel(d):
d._cancelled = True
Expand Down Expand Up @@ -317,6 +324,7 @@ def trap_connection_err(self, fail):
fail.trap(ConnectError, ConnectionClosed, ResponseFailed)

def force_retry(self, func, *args, **kwargs):
# type: (Callable[..., Any], *Any, **Any) -> Deferred
"""Forcefully retry a function in a thread until it doesn't error
Note that this does not use ``self.deferToThread``, so this will
Expand Down Expand Up @@ -917,7 +925,7 @@ def error_notification_overload(self, fail):
def error_message_overload(self, fail):
"""errBack for handling excessive messages per UAID"""
fail.trap(MessageOverloadException)
self.force_retry(self.ap_settings.router.drop_user(self.ps.uaid))
self.force_retry(self.ap_settings.router.drop_user, self.ps.uaid)
self.sendClose()

def finish_notifications(self, notifs):
Expand Down Expand Up @@ -960,8 +968,7 @@ def finish_notifications(self, notifs):
d.addErrback(self.trap_cancel)
elif self.ps.reset_uaid:
# Told to reset the user?
self.force_retry(
self.ap_settings.router.drop_user(self.ps.uaid))
self.force_retry(self.ap_settings.router.drop_user, self.ps.uaid)
self.sendClose()

def finish_webpush_notifications(self, result):
Expand Down Expand Up @@ -996,7 +1003,7 @@ def finish_webpush_notifications(self, result):
# Told to reset the user?
if self.ps.reset_uaid:
self.force_retry(
self.ap_settings.router.drop_user(self.ps.uaid))
self.ap_settings.router.drop_user, self.ps.uaid)
self.sendClose()

# Not told to check for notifications, do we need to now rotate
Expand Down

0 comments on commit aff8904

Please sign in to comment.