
    Dh	                     B    d Z ddlZd Z G d de      Z G d de      Zy)zImplementing load balancing    Nc                     t        | |z  dz  j                  t        j                  d      t        j                              S )z%Calculate ratio

    Returns int
    d   1)rounding)intquantizedecimalDecimalROUND_HALF_DOWN)partwholes     `/var/www/html/civilisation/venv/lib/python3.12/site-packages/mysql/connector/fabric/balancing.py_calc_ratior      sB    
 U
3((w'>'> ) @ A A    c                   B    e Zd ZdZd Zd Zd Zed        Zed        Z	y)BaseSchedulingzABase class for all scheduling classes dealing with load balancingc                      g | _         g | _        y)
InitializeN)_members_ratiosselfs    r   __init__zBaseScheduling.__init__+   s    r   c                     t         )aH  Set members and ratios

        This methods sets the members using the arguments passed. Each
        argument must be a sequence where the second item is the weight.
        The first element is an identifier. For example:

            ('server1', 0.6), ('server2', 0.8)

        Setting members means that the load will be reset. If the members
        are the same as previously set, nothing will be reset or set.

        If no arguments were given the members will be set to an empty
        list.

        Raises ValueError when weight can't be converted to a Decimal.
        NotImplementedError)r   argss     r   set_memberszBaseScheduling.set_members0   s
    " "!r   c                     t         )Returns the next memberr   r   s    r   get_nextzBaseScheduling.get_nextC   s    !!r   c                     | j                   S )z(Returns the members of this loadbalancer)r   r   s    r   memberszBaseScheduling.membersG   s     }}r   c                     | j                   S )z"Returns the ratios for all members)r   r   s    r   ratioszBaseScheduling.ratiosL   s     ||r   N)
__name__
__module____qualname____doc__r   r   r!   propertyr#   r%    r   r   r   r   '   s<    K
"&"    r   r   c                   P     e Zd ZdZ fdZed        Zd Zd Zd Z	d Z
d Z xZS )	WeightedRoundRobinz.Class for doing Weighted Round Robin balancingc                 x    t         t        |           g | _        d| _        d| _        |r | j                  |  yy)Initializingr   N)superr-   r   _load_next_member_nr_membersr   )r   r   	__class__s     r   r   zWeightedRoundRobin.__init__V   s@     $02
Dd# r   c                     | j                   S )zReturns the current load)r1   r   s    r   loadzWeightedRoundRobin.load`   s     zzr   c                 z   |sg | _         y g }|D ]L  }t        |      }	 t        j                  t	        |d               |d<   |j                  t        |             N |j                  d d       | j                   |k(  ry || _         t        |      | _        t        d | j                   D              }g | _        | j                   D ]/  \  }}| j                  j                  t        ||z  dz               1 | j!                          y # t        j
                  $ r t        dj                  |            w xY w)	N   zMember '{member}' is invalid)memberc                     | d   S )Nr8   r+   )xs    r   <lambda>z0WeightedRoundRobin.set_members.<locals>.<lambda>t   s
    qt r   T)keyreversec              3   &   K   | ]	  }|d      yw)r8   Nr+   ).0is     r   	<genexpr>z1WeightedRoundRobin.set_members.<locals>.<genexpr>z   s     5!15s   r   )r   listr	   r
   strInvalidOperation
ValueErrorformatappendtuplesortlenr3   minr   r   reset)r   r   new_membersr9   
min_weight_weights          r   r   zWeightedRoundRobin.set_memberse   s.   DM 	.F&\F$#OOCq	N;q	 uV}-	. 	^T:==K'#{+5t}}55
 	>IAvLLF:$5$; <=	>

 ++ $ !?!F!F! "G "# $ $$s   $D/D:c                 <    d| _         dg| j                  z  | _        y)zReset the loadr   N)r2   r3   r1   r   s    r   rM   zWeightedRoundRobin.reset   s    S4+++
r   c                    | j                   | j                  k(  r| j                          | j                  }| j                  |   | j                   |   k(  r2|dz   | j                  z  }| j                  |   | j                   |   k(  r2| j                  |xx   dz  cc<   |dz   | j                  z  | _        | j
                  |   S )r    r8   )r   r1   rM   r2   r3   r   )r   currents     r   r!   zWeightedRoundRobin.get_next   s    <<4::%JJL ##jj!T\\'%::{d&6&66G jj!T\\'%:: 	

7q $q[D,<,<< }}W%%r   c                 f    dj                  | j                  | j                  | j                        S )Nz&{class_}(load={load}, ratios={ratios}))class_r6   r%   )rG   r4   r6   r%   r   s    r   __repr__zWeightedRoundRobin.__repr__   s/    7>>>>;; ? 
 	
r   c                 4    | j                   |j                  k(  S )N)r   r#   )r   others     r   __eq__zWeightedRoundRobin.__eq__   s    }}--r   )r&   r'   r(   r)   r   r*   r6   r   rM   r!   rW   rZ   __classcell__)r4   s   @r   r-   r-   R   s8    8$  6,
&$
.r   r-   )r)   r	   r   objectr   r-   r+   r   r   <module>r]      s0   0 " A(V (VM. M.r   