Repository navigation
Expand file tree
/
Copy pathmanager.py
More file actions
250 lines (202 loc) · 8.9 KB
/
Copy pathmanager.py
File metadata and controls
250 lines (202 loc) · 8.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
"""Push subsystem manager class and helpers."""
import logging
from threading import Timer
from splitio.api import APIException
from splitio.util.time import get_current_epoch_time_ms
from splitio.push.splitsse import SplitSSEClient
from splitio.push.parser import parse_incoming_event, EventParsingException, EventType, \
MessageType
from splitio.push.processor import MessageProcessor
from splitio.push.status_tracker import PushStatusTracker, Status
from splitio.models.telemetry import StreamingEventTypes
_TOKEN_REFRESH_GRACE_PERIOD = 10 * 60 # 10 minutes
_LOGGER = logging.getLogger(__name__)
class PushManager(object): # pylint:disable=too-many-instance-attributes
"""Push notifications susbsytem manager."""
def __init__(self, auth_api, synchronizer, feedback_loop, sdk_metadata, telemetry_runtime_producer, sse_url=None, client_key=None):
"""
Class constructor.
:param auth_api: sdk-auth-service api client
:type auth_api: splitio.api.auth.AuthAPI
:param synchronizer: split data synchronizer facade
:type synchronizer: splitio.sync.synchronizer.Synchronizer
:param feedback_loop: queue where push status updates are published.
:type feedback_loop: queue.Queue
:param sdk_metadata: SDK version & machine name & IP.
:type sdk_metadata: splitio.client.util.SdkMetadata
:param sse_url: streaming base url.
:type sse_url: str
:param client_key: client key.
:type client_key: str
"""
self._auth_api = auth_api
self._feedback_loop = feedback_loop
self._processor = MessageProcessor(synchronizer, telemetry_runtime_producer)
self._status_tracker = PushStatusTracker(telemetry_runtime_producer)
self._event_handlers = {
EventType.MESSAGE: self._handle_message,
EventType.ERROR: self._handle_error
}
self._message_handlers = {
MessageType.UPDATE: self._handle_update,
MessageType.CONTROL: self._handle_control,
MessageType.OCCUPANCY: self._handle_occupancy
}
kwargs = {} if sse_url is None else {'base_url': sse_url}
self._sse_client = SplitSSEClient(self._event_handler, sdk_metadata, self._handle_connection_ready,
self._handle_connection_end, client_key, **kwargs)
self._running = False
self._next_refresh = Timer(0, lambda: 0)
self._telemetry_runtime_producer = telemetry_runtime_producer
def update_workers_status(self, enabled):
"""
Enable/Disable push update workers.
:param enabled: if True, enable workers. If False, disable them.
:type enabled: bool
"""
self._processor.update_workers_status(enabled)
def start(self):
"""Start a new connection if not already running."""
if self._running:
_LOGGER.warning('Push manager already has a connection running. Ignoring')
return
self._trigger_connection_flow()
def stop(self, blocking=False):
"""
Stop the current ongoing connection.
:param blocking: whether to wait for the connection to be successfully closed or not
:type blocking: bool
"""
if not self._running:
_LOGGER.warning('Push manager does not have an open SSE connection. Ignoring')
return
self._running = False
self._processor.update_workers_status(False)
self._status_tracker.notify_sse_shutdown_expected()
self._next_refresh.cancel()
self._sse_client.stop(blocking)
def _event_handler(self, event):
"""
Process an incoming event.
:param event: Incoming event
:type event: splitio.push.sse.SSEEvent
"""
try:
parsed = parse_incoming_event(event)
except EventParsingException:
_LOGGER.error('error parsing event of type %s', event.event_type)
_LOGGER.debug(str(event), exc_info=True)
return
try:
handle = self._event_handlers[parsed.event_type]
except KeyError:
_LOGGER.error('no handler for message of type %s', parsed.event_type)
_LOGGER.debug(str(event), exc_info=True)
return
try:
handle(parsed)
except Exception: # pylint:disable=broad-except
_LOGGER.error('something went wrong when processing message of type %s',
parsed.event_type)
_LOGGER.debug(str(parsed), exc_info=True)
def _token_refresh(self):
"""Refresh auth token."""
_LOGGER.info("retriggering authentication flow.")
self.stop(True)
self._trigger_connection_flow()
def _trigger_connection_flow(self):
"""Authenticate and start a connection."""
try:
token = self._auth_api.authenticate()
except APIException:
_LOGGER.error('error performing sse auth request.')
_LOGGER.debug('stack trace: ', exc_info=True)
self._feedback_loop.put(Status.PUSH_RETRYABLE_ERROR)
return
if token is None or not token.push_enabled:
self._feedback_loop.put(Status.PUSH_NONRETRYABLE_ERROR)
return
self._telemetry_runtime_producer.record_token_refreshes()
_LOGGER.debug("auth token fetched. connecting to streaming.")
self._status_tracker.reset()
if self._sse_client.start(token):
_LOGGER.debug("connected to streaming, scheduling next refresh")
self._setup_next_token_refresh(token)
self._running = True
self._telemetry_runtime_producer.record_streaming_event((StreamingEventTypes.CONNECTION_ESTABLISHED, 0, get_current_epoch_time_ms()))
def _setup_next_token_refresh(self, token):
"""
Schedule next token refresh.
:param token: Last fetched token.
:type token: splitio.models.token.Token
"""
if self._next_refresh is not None:
self._next_refresh.cancel()
self._next_refresh = Timer((token.exp - token.iat) - _TOKEN_REFRESH_GRACE_PERIOD,
self._token_refresh)
self._next_refresh.setName('TokenRefresh')
self._next_refresh.start()
self._telemetry_runtime_producer.record_streaming_event((StreamingEventTypes.TOKEN_REFRESH, 1000 * token.exp, get_current_epoch_time_ms()))
def _handle_message(self, event):
"""
Handle incoming update message.
:param event: Incoming Update message
:type event: splitio.push.sse.parser.Update
"""
try:
handle = self._message_handlers[event.message_type]
except KeyError:
_LOGGER.error('no handler for message of type %s', event.message_type)
_LOGGER.debug(str(event), exc_info=True)
return
handle(event)
def _handle_update(self, event):
"""
Handle incoming update message.
:param event: Incoming Update message
:type event: splitio.push.sse.parser.Update
"""
_LOGGER.debug('handling update event: %s', str(event))
self._processor.handle(event)
def _handle_control(self, event):
"""
Handle incoming control message.
:param event: Incoming control message.
:type event: splitio.push.sse.parser.ControlMessage
"""
_LOGGER.debug('handling control event: %s', str(event))
feedback = self._status_tracker.handle_control_message(event)
if feedback is not None:
self._feedback_loop.put(feedback)
def _handle_occupancy(self, event):
"""
Handle incoming notification message.
:param event: Incoming occupancy message.
:type event: splitio.push.sse.parser.Occupancy
"""
_LOGGER.debug('handling occupancy event: %s', str(event))
feedback = self._status_tracker.handle_occupancy(event)
if feedback is not None:
self._feedback_loop.put(feedback)
def _handle_error(self, event):
"""
Handle incoming error message.
:param event: Incoming ably error
:type event: splitio.push.sse.parser.AblyError
"""
_LOGGER.debug('handling ably error event: %s', str(event))
feedback = self._status_tracker.handle_ably_error(event)
if feedback is not None:
self._feedback_loop.put(feedback)
def _handle_connection_ready(self):
"""Handle a successful connection to SSE."""
self._feedback_loop.put(Status.PUSH_SUBSYSTEM_UP)
_LOGGER.info('sse initial event received. enabling')
def _handle_connection_end(self):
"""
Handle a connection ending.
If the connection shutdown was not requested, trigger a restart.
"""
feedback = self._status_tracker.handle_disconnect()
if feedback is not None:
self._feedback_loop.put(feedback)