ó
    zÉjg  ã                   ób  • S r SSKrSSKrSSKrSSKrSSKJrJrJr  SSK	r	SSK
Jr  \R                  " \5      r\" S5      rSSKJr  \R$                  r\R(                  r\R,                  " S5      rSrS	\S
\4S jrS\S
\4S jrSS\S.S\/ \\   4   S\S\S-  S\S-  S\\/\4   S
\4S jjr g)u,  
Reintento con backoff para las llamadas a los servicios de StoryFace.

Los endpoints de IA (`picasso-fast-seed`) y de remociÃ³n de fondo (`remove-bg`)
fallan de forma esporÃ¡dica cuando les entra un lote en paralelo: se han visto
500 Â«Failed to generate image with modelÂ» y 500 Â«Failed to use creditsÂ» con la
misma peticiÃ³n funcionando 20 s despuÃ©s. Sin reintento esas generaciones se
pierden para siempre: nadie vuelve a pasar por una foto ya ordenada.

Solo se repite lo que puede arreglarse solo (red, timeout, 429, 5xx). Un error
de configuraciÃ³n â€”API key ausente o invÃ¡lida, cualquier 4xxâ€” falla al primer
intento: repetirlo Ãºnicamente retrasa el error.

Ajustable por entorno: `SF_RETRY_ATTEMPTS` (intentos totales, 1 = sin
reintento) y `SF_RETRY_BASE_DELAY` (segundos de la primera espera).
é    N)Ú	AwaitableÚCallableÚTypeVar)ÚHTTPExceptionÚT)ÚconfigzHTTP (\d{3}))z
no api keyzinvalid api keyzsin api keyÚcodeÚreturnc                 ó$   • U S;   =(       d    U S:¬  $ )N)i˜  i­  iô  © )r	   s    ÚND:\Ingenieria\TRABAJOS ONLINE\Projects\11- Photo School\backend\utils\retry.pyÚ_retriable_statusr   )   s   € Ø�:Ñ×, ¨¡Ð,ó    Úexcc                 óÔ  ^• [        U [        R                  5      (       a  g[        U [        R                  5      (       a  [	        U R
                  R                  5      $ [        U [        5      (       at  [        U R                  5      m[        U4S j[         5       5      (       a  g[        R                  T5      nU(       a#  [	        [        UR                  S5      5      5      $ gg)u/   Â¿Merece la pena repetir esta llamada tal cual?Tc              3   óH   >#   • U  H  oTR                  5       ;   v •  M     g 7f)N)Úlower)Ú.0ÚmÚdetails     €r   Ú	<genexpr>Úis_transient.<locals>.<genexpr>5   s   øé € Ð?Ò,> q�F—L‘L“NÖ"Ò,>ùs   ƒ"Fé   )Ú
isinstanceÚhttpxÚRequestErrorÚHTTPStatusErrorr   ÚresponseÚstatus_coder   Ústrr   ÚanyÚ_PERMANENT_MARKERSÚ_HTTP_STATUS_REÚsearchÚintÚgroup)r   r   r   s     @r   Úis_transientr'   -   s    ø€ ä�#”u×)Ñ)×*Ñ*ØÜ�#”u×,Ñ,×-Ñ-Ü  §¡×!9Ñ!9Ó:Ð:Ü�#”}×%Ñ%Ü�S—Z‘Z“ˆÜÔ?Õ,>Ó?×?Ñ?ØÜ×"Ñ" 6Ó*ˆÞÜ$¤S¨¯©°«£_Ó5Ð5ØØr   )ÚattemptsÚ
base_delayÚretry_onÚfactoryÚlabelr(   r)   r*   c          
   ƒ   óD  #   • Uc  [         O[        SU5      nUc  [        OUn[        SUS-   5       H  n U " 5       I Sh  v•N s  $    [        S	U S
35      e N! [         a¹  n[        U5      =(       d    [        U5      R                   S3n	Xu:¼  d  U" U5      (       d  [        R                  SXXY5        e USUS-
  -  -  [        R                  " SS5      -  n
[        R                  SXXYU
5        [        R                  " U
5      I Sh  v•N     SnAMæ  SnAff = f7f)u›  
Ejecuta `factory()` reintentando los fallos transitorios con backoff
exponencial y jitter (â‰ˆ3 s, â‰ˆ6 s con los valores por defecto).

`factory` es una funciÃ³n que DEVUELVE la corrutina, no una corrutina ya
creada: hay que poder llamarla de nuevo en cada intento.

Propaga la Ãºltima excepciÃ³n si se agotan los intentos o si el fallo no es
transitorio, asÃ­ que el llamante no cambia su manejo de errores.
Nr   z (sin mensaje)u1   â�Œ %s fallÃ³ definitivamente (intento %d/%d): %sé   gš™™™™™é?g333333ó?u9   ðŸ”� %s fallÃ³ (intento %d/%d): %s â€” reintento en %.1fszwith_retries(z): estado inalcanzable)ÚRETRY_ATTEMPTSÚmaxÚRETRY_BASE_DELAYÚrangeÚ	Exceptionr    ÚtypeÚ__name__ÚloggerÚerrorÚrandomÚuniformÚwarningÚasyncioÚsleepÚRuntimeError)r+   r,   r(   r)   r*   ÚtotalÚdelayÚattemptr   r   Úwaits              r   Úwith_retriesrB   >   s  é € ð$ 'Ñ.�N´C¸¸8Ó4D€EØ *Ñ 2Õ¸
€Eä˜˜E A™IÖ&ˆð	&Ù ›—?Ò"ñ 'ô0 ˜ u gÐ-CÐDÓ
EÐEñ- #øÜó 	&ô ˜“X×F¤D¨£I×$6Ñ$6Ð#7°~Ð!FˆFØÓ¡x°§}¡}ô
 —‘ØGØ Eôð Ø˜A '¨A¡+Ñ.Ñ/´&·.².ÀÀcÓ2JÑJˆDÜ�N‰NØKØ ¨tôô —-’- Ó%×%×%ûð'	&üsM   ‚3D ¶
AÁ AÁAÁD ÁAÁ
DÁ$B(DÄDÄDÄD ÄDÄD )!Ú__doc__r;   Úloggingr8   ÚreÚtypingr   r   r   r   Úfastapir   Ú	getLoggerr5   r6   r   Úcorer   ÚSF_RETRY_ATTEMPTSr/   ÚSF_RETRY_BASE_DELAYr1   Úcompiler#   r"   r%   Úboolr   ÚBaseExceptionr'   r    ÚfloatrB   r   r   r   Ú<module>rP      s  ðñó  Û Û Û 	ß /Ñ /ã Ý !à	×	Ò	˜8Ó	$€áˆCƒL€å à×)Ñ)€Ø×-Ñ-Ð ð —*’*˜_Ó-€ØEÐ ð-˜Cð - Dô -ð�mð ¨ô ð*  Ø#Ø0<ò-FØ�b˜) A™,Ð&Ñ'ð-Fð ð-Fð �D‰jð	-Fð
 ˜‘ð-Fð ˜�¨Ð,Ñ-ð-Fð ö-Fr   