autopush.websocket
¶
Websocket Protocol handler and HTTP Endpoints for Connection Node
Private HTTP Endpoints¶
These HTTP endpoints are only for communication from endpoint nodes and must not be publicly exposed.
-
PUT
/push/
(uuid: uaid)¶ Send a notification to a connected client with the given uaid.
Status Codes: - 200 OK – Client is connected and delivery will be attempted.
- 404 Not Found – Client is not connected to this node.
- 503 Service Unavailable – Client is connected, but currently busy.
-
PUT
/notif/
(uuid: uaid)¶ Trigger a stored notification check for a connected client.
Status Codes: - 200 OK – Client is connected, and has started checking.
- 202 Accepted – Client is connected but busy, will check notifications when not busy.
- 404 Not Found – Client is not connected to this node.
-
DELETE
/notif/
(uuid: uaid)/
(int: connected_at)¶ Immediately drop a client of this uaid if its connection time matches the connected_at provided.
Websocket Protocol¶
-
class
autopush.websocket.
PushServerProtocol
[source]¶ Main Websocket Connection Protocol
-
parent_class
¶ alias of
autobahn.twisted.websocket.WebSocketServerProtocol
-
classmethod
randrange
(start, stop=None, step=1, _int=<type 'int'>, _maxwidth=9007199254740992L)¶ Choose a random item from range(start, stop[, step]).
This fixes the problem with randint() which includes the endpoint; in Python this is usually not what you want.
-
deferToLater
(when, func, *args, **kwargs)[source]¶ deferToLater helper that tracks defers outstanding
-
force_retry
(func, *args, **kwargs)[source]¶ Forcefully retry a function in a thread until it doesn’t error
Note that this does not use
self.deferToThread
, so this will continue to retry even if the client drops.
Property that uses None if there’s no tags due to a DataDog library bug
-
paused
¶ Indicates if we are paused for output production or not
-
sendClose
(*args, **kwargs)[source]¶ Override to add tracker that ensures the connection is truly torn down
-
nukeConnection
(*args, **kwargs)[source]¶ Aggressive connection shutdown using abortConnection if onClose still hadn’t run by this point
-
processHandshake
(*args, **kwargs)[source]¶ Disable host port checking on nonstandard ports since some clients are buggy and don’t provide it
-
onClose
(*args, **kwargs)[source]¶ autobahn onClose handler for shutting down the connection and any outstanding deferreds related to this connection
-
cleanUp
(wasClean, code, reason)[source]¶ Thorough clean-up method to cancel all remaining deferreds, and send connection metrics in
-
_lookup_node
(results)[source]¶ Looks up the node to send a notify for it to check storage if connected
-
_notify_node
(result)[source]¶ Checks the result of lookup node to send the notify if the client is connected elsewhere now
-
returnError
(messageType, reason, statusCode, close=True, url='http://autopush.readthedocs.io/en/latest/api/websocket.html#private-http-endpoint')[source]¶ Return an error to a client, and optionally shut down the connection safely
-
error_overload
(failure, message_type, disconnect=True)[source]¶ Handle database overloads and errors
If
disconnect
is False, the an overload error is returned and the client is not disconnected.Otherwise, pause producing to cease incoming notifications while we wait a random interval up to 8 seconds before closing down the connection. Most clients wait up to 10 seconds for a command, but this is not a guarantee, so rather than never reply, we still shut the connection down.
Parameters: disconnect – Whether the client should be disconnected or not.
-
error_finish_overload
(message_type)[source]¶ Close the connection down and resume consuming input after the random interval from a db overload
-
_verify_user_record
()[source]¶ Verify a user record is valid
Returns a record that is ready for registering in the database if the user record was found.
Return type: Item
or None
-
_check_other_nodes
(result, url='http://autopush.readthedocs.io/en/latest/api/websocket.html#private-http-endpoint')[source]¶ callback to check other nodes for clients and send them a delete as needed
-
_rotate_message_table
()[source]¶ Function to fire off a message table copy of channels + update the router current_month entry
-
_monthly_transition
()[source]¶ Transition the client to use a new message month
Utilized to migrate a users channels to a new message month and update the router record reflecting the proper month.
This is a blocking function that does not run on the event loop.
-
error_monthly_rotation_overload
(fail)[source]¶ Capture overload on monthly table rotation attempt
If a provision exceeded error hits while attempting monthly table rotation, schedule it all over and re-scan the messages. Normal websocket client flow is returned in the meantime.
-
process_ping
()[source]¶ Ping Handling
Clients in the wild have a bug that lowers their ping interval to 0. It will never increase for them, as there is no way to remedy this without causing the client to use drastically more battery/data-usage we send them a code 4774 close to signify that they should stop until network change.
No other client should ping more than once per minute, or we tell them to go away.
-
finish_register
(endpoint, chid)[source]¶ callback for successful endpoint creation, sends register reply
-
ack_update
(update)[source]¶ Helper function for tracking ack’d updates
Returns either None, if no delete_notification call is needed, or a deferred for the delete_notification call if it was needed.
-
_handle_webpush_update_remove
(result, chid, notif)[source]¶ Handle clearing out the updates_sent
It’s possible the client may leave before this runs, so this is wrapped in a try/except in case the tear-down of self has started.
-
check_missed_notifications
(results, resume=False)[source]¶ Check to see if notifications were missed
-
HTTP Handlers¶
-
class
autopush.websocket.
RouterHandler
(application, request, **kwargs)[source]¶ Router Handler
Handles routing a notification to a connected client from an endpoint.