@@ -8,7 +8,7 @@ class ErrorStrategy:
88 """
99 Base class of strategies for determining how SSEClient should handle a stream error or the
1010 end of a stream.
11-
11+
1212 The parameter that SSEClient passes to :meth:`apply()` is either ``None`` if the server ended
1313 the stream normally, or an exception. If it is an exception, it could be an I/O exception
1414 (failure to connect, broken connection, etc.), or one of the error types defined in this
@@ -25,7 +25,7 @@ class ErrorStrategy:
2525 With either option, it is still always possible to explicitly reconnect the stream by calling
2626 :meth:`.SSEClient.start()` again, or simply by trying to read from :attr:`.SSEClient.events`
2727 or :attr:`.SSEClient.all` again.
28-
28+
2929 Subclasses should be immutable. To implement strategies that behave differently on consecutive
3030 retries, the strategy should return a new instance of its own class as the second return value
3131 from ``apply``, rather than modifying the state of the existing instance. This makes it easy
@@ -74,7 +74,7 @@ def continue_with_max_attempts(max_attempts: int) -> ErrorStrategy:
7474 :param max_attempts: the maximum number of consecutive retries
7575 """
7676 return _MaxAttemptsErrorStrategy (max_attempts , 0 )
77-
77+
7878 @staticmethod
7979 def continue_with_time_limit (max_time : float ) -> ErrorStrategy :
8080 """
@@ -90,7 +90,7 @@ def from_lambda(fn: Callable[[Optional[Exception]], Tuple[bool, Optional[ErrorSt
9090 """
9191 Convenience method for creating an ErrorStrategy whose ``apply`` method is equivalent to
9292 the given lambda.
93-
93+
9494 The one difference is that the second return value is an ``Optional[ErrorStrategy]`` which
9595 can be None to mean "no change", since the lambda cannot reference the strategy's ``self``.
9696 """
@@ -100,26 +100,28 @@ def from_lambda(fn: Callable[[Optional[Exception]], Tuple[bool, Optional[ErrorSt
100100class _LambdaErrorStrategy (ErrorStrategy ):
101101 def __init__ (self , fn : Callable [[Optional [Exception ]], Tuple [bool , Optional [ErrorStrategy ]]]):
102102 self .__fn = fn
103-
103+
104104 def apply (self , exception : Optional [Exception ]) -> Tuple [bool , ErrorStrategy ]:
105105 should_raise , maybe_next = self .__fn (exception )
106106 return (should_raise , maybe_next or self )
107107
108+
108109class _MaxAttemptsErrorStrategy (ErrorStrategy ):
109110 def __init__ (self , max_attempts : int , counter : int ):
110111 self .__max_attempts = max_attempts
111112 self .__counter = counter
112-
113+
113114 def apply (self , exception : Optional [Exception ]) -> Tuple [bool , ErrorStrategy ]:
114115 if self .__counter >= self .__max_attempts :
115116 return (ErrorStrategy .FAIL , self )
116117 return (ErrorStrategy .CONTINUE , _MaxAttemptsErrorStrategy (self .__max_attempts , self .__counter + 1 ))
117118
119+
118120class _TimeLimitErrorStrategy (ErrorStrategy ):
119121 def __init__ (self , max_time : float , start_time : float ):
120122 self .__max_time = max_time
121123 self .__start_time = start_time
122-
124+
123125 def apply (self , exception : Optional [Exception ]) -> Tuple [bool , ErrorStrategy ]:
124126 if self .__start_time == 0 :
125127 return (ErrorStrategy .CONTINUE , _TimeLimitErrorStrategy (self .__max_time , time .time ()))
0 commit comments