Team Ai
Apppublic

Yash030/claude-code-proxy

sourceHugging Faceupdated 5mo agoView on Hugging Face
2likes
limiter.cpython-314.pyc114 linesDownload Raw Back to __pycache__
1+
2�Ej�/��~�Rt^RIt^RIHt^RIHtHt^RIHt^RI	H3t4^RIHt^RI
Ht^RIHt!R	R54tR#)z�6Global Rate Limiter for Messaging Platforms.7 8Centralizes outgoing message requests and ensures compliance with rate limits9using a strict sliding window algorithm and a task queue.10N)�deque)�	Awaitable�Callable)�Any)�logger)�get_settings)�StrictSlidingWindowLimiter)�format_exception_for_logc�0aa�]tRt^toRtRt]P!4tV3Rlt	]11R^RR/V3RlRll4tV3R	lR12ltV3RlRlt
R
tRV3RlRllt]13RV3RlRll4tRRltRRltRV3RlRlltRV3RlRlltV3RltRtVtV;t#)�MessagingRateLimiterz�14A thread-safe global rate limiter for messaging.15 16Uses a custom queue with task compaction (deduplication) to ensure17only the latest version of a message update is processed.18Nc�"<�\SV`V4#�N)�super�__new__)�cls�args�kwargs�	__class__s&*,��<C:\Users\yashw\.claude\claude-code-free\messaging\limiter.pyr�MessagingRateLimiter.__new__ s����w��s�#�#��19rate_limit�rate_windowg�?c�,<�V^8�dQhRS[RS[RS[/#)�rr�return)�int�floatr)�format�
__classdict__s"�r�__annotate__�!MessagingRateLimiter.__annotate__$s+��������	�2021�rc��>"�VP;_uu_4GRjx�L22VPf)V!WR7VnVPP4RRR4GRjx�L23VP#LVL +GRjx�L24'giTP#;i5i)z�Get the singleton instance of the limiter.25 26``rate_limit`` and ``rate_window`` apply only when the singleton is first27created. Call :meth:`shutdown_instance` before changing parameters.28N)rr)�_lock�	_instance�
_start_worker)rrrs&$$r�get_instance�!MessagingRateLimiter.get_instance#sb����9�9�9�9��}�}�$� #�z� S��
��
�
�+�+�-�	�9�29�}�}���9�9�9�30�}�}��sJ�B�A2�B�7A6�B�!A4�"B�4B�6B	�<A?�=31B	�B	�32Bc�*<�V^8�dQhRS[RS[RR/#)rrrrN)rr)rrs"�rr r!6s"���33�34�c�35��36�$�37rc�F�\VR4'dR#\W4Vn\4Vn/Vn\P!4Vn\P!4Vn38RVnRVn^Vn
\P!RVRVR24R#)�_initializedNTz"MessagingRateLimiter initialized (z req / zs with Task Compaction))�hasattr�SlidingWindowLimiter�limiterr�_queue_list�39_queue_map�asyncio�	Condition�40_condition�Event�	_shutdown�_worker_taskr*�
_paused_untilr�info)�selfrrs&$$r�__init__�MessagingRateLimiter.__init__6s����4��(�(��+�J�D���',�w���
�	
��"�+�+�-��� ������15��� ���������0���G�K�=�Pg�h�	41rc�<�V^8�dQhRR/#)rrN�)rrs"�rr r!Ls���42�43�t�44rc���VP'd#VPP4'gR#\P!VP	4RR7VnR#)zEnsure the worker task exists.Nzmsg-limiter-worker)�name)r5�doner0�create_task�_worker)r8s&rr%�"MessagingRateLimiter._start_workerLsD������T�%6�%6�%;�%;�%=�%=��#�/�/��L�L�N�!5�45��rc��	"�\P!R4VPP4'Eg�VP;_uu_4GRjx�L46VP47'gEVPP4'g%VPP
4GRjx�L48KVVPP4'dRRR4GRjx�L49R#VP50P4pVPPV4wr#RRR4GRjx�L51\P!4P4pVPV8�dMVPV,52p\P!RVR
R24\P!V4GRjx�L53VP ;_uu_4GRjx�L54X!4GRjx�L55pXF,pVP#4'dKVP%V4K.	RRR4GRjx�L56EKR#EL�ELxELHEL +GRjx�L57'giEL;iL�L�Lx \&Ed�pXF,pTP#4'dKTP)T4K.	\+T4P-4p	RT	9g	RT	9Ed^p58\/TR4'dTP0p59MaR	T	9dIT	P3R	4p\5T4^8�d(\7T^,P34^,4p60M \&dMi;i\P8!R61T62R24\;T63\6\<\*34'd\=T644MRp\P!4P4T,Tn
Rp?EL�\?4P@p
\P8!R
X\CY�R74Rp?EL�Rp?ii;iEL� +GRjx�L65'giEK�;i \PDdR#\&d�p\?4P@p
T
'd\P8!RTRR7M*\P8!R\GT4PH4\P!^4GRjx�L66Rp?EK�Rp?ii;i5i)z8Background worker that processes queued messaging tasks.z#MessagingRateLimiter worker startedNzLimiter worker paused, waiting z.1fz	s more...�flood�wait�secondszafter z'FloodWait detected! Pausing worker for �sg>@z&Error in limiter worker for key {}: {}��log_full_messagez.MessagingRateLimiter worker critical error: {}T)�exc_infoz7MessagingRateLimiter worker critical error: exc_type={})%rr7r4�is_setr2r.rE�popleftr/�popr0�get_event_loop�timer6�warning�sleepr-r?�67set_result�	Exception�
set_exception�str�lowerr+rF�split�lenr�error�68isinstancerr�log_messaging_error_detailsr	�CancelledError�type�__name__)r8�	dedup_key�func�futures�now�	wait_time�result�f�e�	error_msgrF�parts�	wait_secs�ds&             rrA�MessagingRateLimiter._workerUsl������9�:��.�.�'�'�)�)�Q
'��?�?�?�?�"�.�.�.�t�~�~�7L�7L�7N�7N�"�o�o�2�2�4�4�4��~�~�,�,�.�.��+�?�?�!%� 0� 0� 8� 8� :�I�$(�O�O�$7�$7�	�$B�M�D�+�?��,�,�.�3�3�5���%�%��+� $� 2� 2�S� 8�I��N�N�9�)�C��	�R��"�-�-�	�2�2�2� �<�<�<�<�*�'+�v���!(�A�#$�6�6�8�8� !���V� 4�")�(�<�<�/*�+�4�+�?�?�?��"3�(�!-��%�%�!(�A�#$�6�6�8�8� !���� 2�")�%(��F�L�L�N�	�"�i�/�6�Y�3F�&(�G�	%�#*�1�i�#8�#8�./�i�i�G�%-��%:�,5�O�O�H�,E�E�'*�5�z�A�~�25�e�A�h�n�n�6F�q�6I�2J���#,�%� $�%��#�L�L�"I�'��RS� T��69$.�g��U�C�7H�#I�#I�!&�g��%)�&�!(� 6� 6� 8� =� =� ?�)� K�!�.�.�!-�� J� J�A�"�L�L� H� )� 8�� O����C%��
(�<�<�<��X�)�)�
���

'� �N�>�>����L�L�H��!%���L�L�Q��Q��(�(���m�m�A�&�&�&�&��

'�s��7S�P�H�P�H(�*H(�70H(�'H�(%H(�H(�P�H"�P�S�!7H(�P�#H%�$A?P�#I�$P�I�P�O8�71I�I	�I�2I�P�O5�P�S�P�H(�"P�%P�(I	�.H1�/72I	�:I	�<73P�P�	I�O2�O-�4?O-�4L#�O-�AL#�"O-�#L1	�.O-�0L1	�1A:O-�+O8�16O-�'O8�-O2�2O8�5P�8P	�>P�?74P	�75P	�P�S�P�S�+S�.S�7S�8A9R>�1R4�2R>�7S�>S�Sc�$<�V^8�dQhRS[RR/#�r�timeoutrN�r)rrs"�rr r!�s���%�%�e�%�d�%rc	��."�VPP4VP;_uu_4GRjx�L76VPP4RRR4GRjx�L77TP78pT'dTP
4'd79RTnR#TP4\P!Y!R7GRjx�L80RTnR#L�Lr +GRjx�L81'giL�;i \dL�i;iL> \d\P!R4L_\PdLu\d@p\4Pp\P !R\#Y4R74Rp?L�Rp?ii;i RTni;i5i)z<Stop the background worker so process shutdown doesn't hang.N�rnz7MessagingRateLimiter worker did not stop before timeoutz.MessagingRateLimiter worker shutdown error: {}rH)r4�setr2�82notify_allrSr5r?�cancelr0�wait_for�TimeoutErrorrrPr\rr[�debugr	)r8rn�taskrfrjs&&   r�shutdown�MessagingRateLimiter.shutdown�s���������	���������*�*�,�'��� � ���t�y�y�{�{� $�D������
�
	%��"�"�4�9�9�9�!%�D��3'������	��	��
:���	V��N�N�T�U��%�%�	���	���:�:�A��L�L�@�(��?�
�
��	��!%�D��s��F�C1�C�C1�C�C1� C�!C1�%F�9F�F�)D�D�D�	F�C1�C1�C.	�C�83C.	�'C.	�)C1�-F�.C1�1C?�<F�>C?�?F�D�!F�%F	�'F�:F�;F	�=F�F�5F�<F	�F�F	�		F�Fc�$<�V^8�dQhRS[RR/#rmro)rrs"�rr r!�s���!�!�e�!�d�!rc��"�VPpV'gR#VPVR7GRjx�L84RVnR#L
 RTni;i5i)zHShutdown and clear the singleton instance (safe to call multiple times).Nrq)r$ry)rrn�insts&& r�shutdown_instance�&MessagingRateLimiter.shutdown_instance�sC����}�}����	!��-�-��-�0�0�0� �C�M�
1�� �C�M�s%�A�?�=�?�	A�?�	A�Ac��H"�VPW.W44GRjx�L85R#L5ir
)�_enqueue_internal_multi)r8r`�futurer_�fronts&&&&&r�_enqueue_internal�&MessagingRateLimiter._enqueue_internal�s����*�*�4��9�L�L�L�s�"� �"c86��V"�VP;_uu_4GRjx�L87W0P9d^VPV,wrVVPV4W3VPV&\P!RVR\V4R24MiW3VPV&V'dVPPV4MVPPV4VPP4RRR4GRjx�L88R#L�L +GRjx�L89'giR#;i5i)NzCompacted task for key: z (now z	 futures))90r2r/�extendrrwrXr.�91appendleft�appendrs)r8r`rar_r��	_old_func�old_futuress&&&&&  rr��,MessagingRateLimiter._enqueue_internal_multi�s�����?�?�?�?��O�O�+�)-����)C�&�	��"�"�7�+�.2�-@����	�*����.�y�k���K�@P�?Q�QZ�[��/3�_����	�*���$�$�/�/�	�:��$�$�+�+�I�6����*�*�,�#�?�?�?�?�?�sK�D)�D�D)�CD�6D)�D92�D)�93D)�D&	�D�94D&	�D&	� 	D)c�^<�V^8�dQhRS[.S[S[,3,RS[R,RS[/#)rr`r_Nr�rrrrU)rrs"�rr r!�s7���
�
��R��3��/�0�
�=@�4�Z�
�	�
rc��"�Vf3R\V4R\P!4P42p\P!4P	4pVPWV4GRjx�L95VGRjx�L96#LL5i)z�97Enqueue a messaging task and return its future result.98If dedup_key is provided, subsequent tasks with the same key will replace this one.99N�task_�_)�idr0rNrO�
create_futurer�)r8r`r_r�s&&& r�enqueue�MessagingRateLimiter.enqueue�ss�������4��z��7�+A�+A�+C�+H�+H�+J�*K�L�I��'�'�)�7�7�9���$�$�T�9�=�=�=��|��	>��s$�A/B�1B�2	B�;B�<B�Bc�X<�V^8�dQhRS[.S[S[,3,RS[R,/#)rr`r_Nr�)rrs"�rr r!�s/���2,�2,��R��3��/�0�2,�=@�4�Z�2,rc�aaaa�Sf3R\S4R\P!4P42o\P!4P	4oVVVV3Rlp\P100!V!44pR#)z.Enqueue a task without waiting for the result.Nr�r�c��n<a"�^p\V^,4FpS	PSS4GRjx�L101u#	R#L102 \Edhp\T4P	4oY8d�\103;QJdT3RlR	4F'gKRM	RM!T3RlR	44'd�^T,p\
4PpT'd"\P!RT^,TT4M3\P!RT^,\T4PT4\P!T4GRjx�L104Rp?EK0\
4Pp\P!RS\Y$R74SP!4'gSP#T4Rp?R#Rp?ii;i5i)105rNc3�,<"�TF	qS9x�K	R#5ir
r<)�.0�xrgs& �r�	<genexpr>�IMessagingRateLimiter.fire_and_forget.<locals>._wrapped.<locals>.<genexpr>s����5�0P�1�Y��0P�s�TFzLLimiter fire_and_forget transient error (attempt {}): {}. Retrying in {}s...zULimiter fire_and_forget transient error (attempt {}): exc_type={}. Retrying in {}s...z-Final error in fire_and_forget for key {}: {}rH)�connectrn�broken)�ranger�rSrUrV�anyrr[rrPr]r^r0rQrYr	r?rT)106�max_retries�attemptrfrErjrgr_r`r�r8s107     @����r�_wrapped�6MessagingRateLimiter.fire_and_forget.<locals>._wrappedsJ�����K� ��q��1��$�!%���d�I�!>�>�>�2�>�� �"� #�A�����I��,���5�0P�5����5�0P�5�2�2� !�'�z��(�N�F�F���"�N�N� n� '�!�� !� $�	�#�N�N� w� '�!�� $�Q�� 0� 0� $�	�&�m�m�D�1�1�1� �$��B�B�A��L�L�G�!�0��G��108"�;�;�=�=��,�,�Q�/���E"�sc�F5�?�=�?�F5�?�F2�)F-�5F-�F-�*BF-�<D?�=F-�F5�	AF-�&F5�-F2�2F5)r�r0rNrOr�r@)r8r`r_r�r�r�sfff  @r�fire_and_forget�$MessagingRateLimiter.fire_and_forget�sj�������4��z��7�+A�+A�+C�+H�+H�+J�*K�L�I��'�'�)�7�7�9��'	�'	�R
����109�+�rc�4<�V^8�dQh/S[R,;R&#)rNr$)r)rrs"�rr r!s����$�d�*�1�r)r2r*r6r.r/r4r5r-)g@)Fr
)r^�110__module__�__qualname__�__firstlineno__�__doc__r$r0�Lockr#r�classmethodr&r9r%rAryr~r�r�r�r��__annotate_func__�__static_attributes__�__classdictcell__�
__classcell__)rrs@@rrrs������.2�I��L�L�N�E�$�����!�	����$111�112�,113�114�U'�n%�%�>�!�!��!�M�-�$
�
�2,�2,�K�rr)r�r0�collectionsr�collections.abcrr�typingr�logurur�config.settingsr�core.rate_limitrr,�safe_diagnosticsr	rr<rr�<module>r�s0�����/���(�N�6�W,�W,r