@@ -51,84 +51,46 @@ def __init__(self, auth_api, synchronizer, feedback_loop, sse_url=None):
5151 MessageType .OCCUPANCY : self ._handle_occupancy
5252 }
5353
54- self ._sse_client = SplitSSEClient (self ._event_handler ) if sse_url is None \
55- else SplitSSEClient (self ._event_handler , sse_url )
54+ kwargs = {} if sse_url is None else {'base_url' : sse_url }
55+ self ._sse_client = SplitSSEClient (self ._event_handler , self ._handle_connection_ready ,
56+ self ._handle_connection_end , ** kwargs )
5657 self ._running = False
5758 self ._next_refresh = Timer (0 , lambda : 0 )
5859
59- def _handle_message (self , event ):
60- """
61- Handle incoming update message.
62-
63- :param event: Incoming Update message
64- :type event: splitio.push.sse.parser.Update
65- """
66- try :
67- handle = self ._message_handlers [event .message_type ]
68- except KeyError :
69- _LOGGER .error ('no handler for message of type %s' , event .message_type )
70- _LOGGER .debug (str (event ), exc_info = True )
71- return
72-
73- handle (event )
74-
75- def _handle_update (self , event ):
76- """
77- Handle incoming update message.
78-
79- :param event: Incoming Update message
80- :type event: splitio.push.sse.parser.Update
81- """
82- _LOGGER .debug ('handling update event: %s' , str (event ))
83- self ._processor .handle (event )
84-
85- def _handle_control (self , event ):
86- """
87- Handle incoming control message.
88-
89- :param event: Incoming control message.
90- :type event: splitio.push.sse.parser.ControlMessage
60+ def update_workers_status (self , enabled ):
9161 """
92- _LOGGER .debug ('handling control event: %s' , str (event ))
93- feedback = self ._status_tracker .handle_control_message (event )
94- if feedback is not None :
95- self ._feedback_loop .put (feedback )
62+ Enable/Disable push update workers.
9663
97- def _handle_occupancy (self , event ):
64+ :param enabled: if True, enable workers. If False, disable them.
65+ :type enabled: bool
9866 """
99- Handle incoming notification message.
67+ self . _processor . update_workers_status ( enabled )
10068
101- :param event: Incoming occupancy message.
102- :type event: splitio.push.sse.parser.Occupancy
103- """
104- _LOGGER .debug ('handling occupancy event: %s' , str (event ))
105- feedback = self ._status_tracker .handle_occupancy (event )
106- if feedback is not None :
107- self ._feedback_loop .put (feedback )
10869
109- def _handle_connection_end (self , shutdown_requested ):
110- """
111- Handle a connection ending.
70+ def start (self ):
71+ """Start a new connection if not already running."""
72+ if self ._running :
73+ _LOGGER .warning ('Push manager already has a connection running. Ignoring' )
74+ return
11275
113- If the connection shutdown was not requested, trigger a restart.
76+ self . _trigger_connection_flow ()
11477
115- :param shutdown_requested: whether the shutdown was requested or unexpected.
116- :type shutdown_requested: True
78+ def stop (self , blocking = False ):
11779 """
118- if not shutdown_requested :
119- self ._feedback_loop .put (Status .PUSH_RETRYABLE_ERROR )
80+ Stop the current ongoing connection.
12081
121- def _handle_error (self , event ):
82+ :param blocking: whether to wait for the connection to be successfully closed or not
83+ :type blocking: bool
12284 """
123- Handle incoming error message.
85+ if not self ._running :
86+ _LOGGER .warning ('Push manager does not have an open SSE connection. Ignoring' )
87+ return
12488
125- :param event: Incoming ably error
126- :type event: splitio.push.sse.parser.AblyError
127- """
128- _LOGGER .debug ('handling ably error event: %s' , str (event ))
129- feedback = self ._status_tracker .handle_ably_error (event )
130- if feedback is not None :
131- self ._feedback_loop .put (feedback )
89+ self ._running = False
90+ self ._processor .update_workers_status (False )
91+ self ._status_tracker .notify_sse_shutdown_expected ()
92+ self ._next_refresh .cancel ()
93+ self ._sse_client .stop (blocking )
13294
13395 def _event_handler (self , event ):
13496 """
@@ -178,14 +140,12 @@ def _trigger_connection_flow(self):
178140 self ._feedback_loop .put (Status .PUSH_NONRETRYABLE_ERROR )
179141 return
180142
143+ _LOGGER .debug ("auth token fetched. connecting to streaming." )
181144 self ._status_tracker .reset ()
182- if self ._sse_client .start (token ):
145+ if self ._sse_client .start (token ):
146+ _LOGGER .debug ("connected to streaming, scheduling next refresh" )
183147 self ._setup_next_token_refresh (token )
184148 self ._running = True
185- self ._feedback_loop .put (Status .PUSH_SUBSYSTEM_UP )
186- return
187-
188- self ._feedback_loop .put (Status .PUSH_RETRYABLE_ERROR )
189149
190150 def _setup_next_token_refresh (self , token ):
191151 """
@@ -201,36 +161,79 @@ def _setup_next_token_refresh(self, token):
201161 self ._next_refresh .setName ('TokenRefresh' )
202162 self ._next_refresh .start ()
203163
204- def update_workers_status (self , enabled ):
164+ def _handle_message (self , event ):
205165 """
206- Enable/Disable push update workers .
166+ Handle incoming update message .
207167
208- :param enabled: if True, enable workers. If False, disable them.
209- :type enabled: bool
168+ :param event: Incoming Update message
169+ :type event: splitio.push.sse.parser.Update
210170 """
211- self ._processor .update_workers_status (enabled )
171+ try :
172+ handle = self ._message_handlers [event .message_type ]
173+ except KeyError :
174+ _LOGGER .error ('no handler for message of type %s' , event .message_type )
175+ _LOGGER .debug (str (event ), exc_info = True )
176+ return
212177
178+ handle (event )
213179
214- def start (self ):
215- """Start a new connection if not already running."""
216- if self ._running :
217- _LOGGER .warning ('Push manager already has a connection running. Ignoring' )
218- return
180+ def _handle_update (self , event ):
181+ """
182+ Handle incoming update message.
219183
220- self ._trigger_connection_flow ()
184+ :param event: Incoming Update message
185+ :type event: splitio.push.sse.parser.Update
186+ """
187+ _LOGGER .debug ('handling update event: %s' , str (event ))
188+ self ._processor .handle (event )
221189
222- def stop (self , blocking = False ):
190+ def _handle_control (self , event ):
223191 """
224- Stop the current ongoing connection .
192+ Handle incoming control message .
225193
226- :param blocking: whether to wait for the connection to be successfully closed or not
227- :type blocking: bool
194+ :param event: Incoming control message.
195+ :type event: splitio.push.sse.parser.ControlMessage
228196 """
229- if not self ._running :
230- _LOGGER .warning ('Push manager does not have an open SSE connection. Ignoring' )
231- return
197+ _LOGGER .debug ('handling control event: %s' , str (event ))
198+ feedback = self ._status_tracker .handle_control_message (event )
199+ if feedback is not None :
200+ self ._feedback_loop .put (feedback )
232201
233- self ._processor .update_workers_status (False )
234- self ._status_tracker .notify_sse_shutdown_expected ()
235- self ._next_refresh .cancel ()
236- self ._sse_client .stop (blocking )
202+ def _handle_occupancy (self , event ):
203+ """
204+ Handle incoming notification message.
205+
206+ :param event: Incoming occupancy message.
207+ :type event: splitio.push.sse.parser.Occupancy
208+ """
209+ _LOGGER .debug ('handling occupancy event: %s' , str (event ))
210+ feedback = self ._status_tracker .handle_occupancy (event )
211+ if feedback is not None :
212+ self ._feedback_loop .put (feedback )
213+
214+ def _handle_error (self , event ):
215+ """
216+ Handle incoming error message.
217+
218+ :param event: Incoming ably error
219+ :type event: splitio.push.sse.parser.AblyError
220+ """
221+ _LOGGER .debug ('handling ably error event: %s' , str (event ))
222+ feedback = self ._status_tracker .handle_ably_error (event )
223+ if feedback is not None :
224+ self ._feedback_loop .put (feedback )
225+
226+ def _handle_connection_ready (self ):
227+ """Handle a successful connection to SSE."""
228+ self ._feedback_loop .put (Status .PUSH_SUBSYSTEM_UP )
229+ _LOGGER .info ('sse initial event received. enabling' )
230+
231+ def _handle_connection_end (self ):
232+ """
233+ Handle a connection ending.
234+
235+ If the connection shutdown was not requested, trigger a restart.
236+ """
237+ feedback = self ._status_tracker .handle_disconnect ()
238+ if feedback is not None :
239+ self ._feedback_loop .put (feedback )
0 commit comments