@@ -158,12 +158,6 @@ async def azip(
158158 pass
159159
160160
161- def _retry_for_status (status : int ) -> bool :
162- if status == 429 :
163- return True
164- return False
165-
166-
167161async def async_streaming_bulk (
168162 client : AsyncElasticsearch ,
169163 actions : Union [Iterable [_TYPE_BULK_ACTION ], AsyncIterable [_TYPE_BULK_ACTION ]],
@@ -173,13 +167,13 @@ async def async_streaming_bulk(
173167 expand_action_callback : Callable [
174168 [_TYPE_BULK_ACTION ], _TYPE_BULK_ACTION_HEADER_AND_BODY
175169 ] = expand_action ,
176- retry_for_status_callback : Callable [[int ], bool ] = _retry_for_status ,
177170 raise_on_exception : bool = True ,
178171 max_retries : int = 0 ,
179172 initial_backoff : float = 2 ,
180173 max_backoff : float = 600 ,
181174 yield_ok : bool = True ,
182175 ignore_status : Union [int , Collection [int ]] = (),
176+ retry_on_status : Union [int , Collection [int ]] = (429 ,),
183177 * args : Any ,
184178 ** kwargs : Any ,
185179) -> AsyncIterable [Tuple [bool , Dict [str , Any ]]]:
@@ -191,7 +185,7 @@ async def async_streaming_bulk(
191185 entire input is consumed and sent.
192186
193187 If you specify ``max_retries`` it will also retry any documents that were
194- rejected with a ``429`` status code. Use ``retry_for_status_callback `` to
188+ rejected with a ``429`` status code. Use ``retry_on_status `` to
195189 configure which status codes will be retried. To do this it will wait
196190 (**by calling time.sleep which will block**) for ``initial_backoff`` seconds
197191 and then, every subsequent rejection for the same chunk, for double the time
@@ -208,12 +202,11 @@ async def async_streaming_bulk(
208202 :arg expand_action_callback: callback executed on each action passed in,
209203 should return a tuple containing the action line and the data line
210204 (`None` if data line should be omitted).
211- :arg retry_for_status_callback: callback executed on each item's status,
212- should return a True if the status require a retry and False if not.
205+ :arg retry_on_status: HTTP status code that will trigger a retry.
213206 (if `None` is specified only status 429 will retry).
214207 :arg max_retries: maximum number of times a document will be retried when
215- retry_for_status_callback (defaulting to ``429``) is received,
216- set to 0 (default) for no retries on retry_for_status_callback
208+ retry_on_status (defaulting to ``429``) is received,
209+ set to 0 (default) for no retries
217210 :arg initial_backoff: number of seconds we should wait before the first
218211 retry. Any subsequent retries will be powers of ``initial_backoff *
219212 2**retry_number``
@@ -225,6 +218,9 @@ async def async_streaming_bulk(
225218 client = client .options ()
226219 client ._client_meta = (("h" , "bp" ),)
227220
221+ if isinstance (retry_on_status , int ):
222+ retry_on_status = (retry_on_status ,)
223+
228224 async def map_actions () -> AsyncIterable [_TYPE_BULK_ACTION_HEADER_AND_BODY ]:
229225 async for item in aiter (actions ):
230226 yield expand_action_callback (item )
@@ -277,10 +273,10 @@ async def map_actions() -> AsyncIterable[_TYPE_BULK_ACTION_HEADER_AND_BODY]:
277273 if not ok :
278274 action , info = info .popitem ()
279275 # retry if retries enabled, we are not in the last attempt,
280- # and retry_for_status_callback is true (defaulting to 429)
276+ # and status not in retry_on_status (defaulting to 429)
281277 if (
282278 max_retries
283- and retry_for_status_callback ( info ["status" ])
279+ and info ["status" ] in retry_on_status
284280 and (attempt + 1 ) <= max_retries
285281 ):
286282 # _process_bulk_chunk expects strings so we need to
@@ -293,11 +289,9 @@ async def map_actions() -> AsyncIterable[_TYPE_BULK_ACTION_HEADER_AND_BODY]:
293289 yield ok , info
294290
295291 except ApiError as e :
296- # suppress any status which retry_for_status_callback is true (defaulting to 429 )
292+ # suppress any status in retry_on_status (429 by default )
297293 # since we will retry them
298- if attempt == max_retries or not retry_for_status_callback (
299- e .status_code
300- ):
294+ if attempt == max_retries or e .status_code not in retry_on_status :
301295 raise
302296 else :
303297 if not to_retry :
0 commit comments