B
    Ñš[	  ã               @   s8   d Z ddlZdd„ ZG dd„ deƒZG dd„ deƒZdS )	zImplementing load balancingé    Nc             C   s"   t | | d jt d¡tjd�ƒS )z%Calculate ratio

    Returns int
    éd   Ú1)Úrounding)ÚintÚquantizeÚdecimalÚDecimalÚROUND_HALF_DOWN)ÚpartZwhole© r   ú¤C:\Users\HIRONO~1\AppData\Local\Temp\pip-install-ioykiiwb\mysql-connector-python-rf\mysql_connector_python_rf-2.2.2.data\purelib\mysql\connector\fabric\balancing.pyÚ_calc_ratio   s    r   c               @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zedd	„ ƒZed
d„ ƒZ	dS )ÚBaseSchedulingzABase class for all scheduling classes dealing with load balancingc             C   s   g | _ g | _dS )Z
InitializeN)Ú_membersÚ_ratios)Úselfr   r   r   Ú__init__+   s    zBaseScheduling.__init__c             G   s   t ‚dS )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.
        N)ÚNotImplementedError)r   Úargsr   r   r   Úset_members0   s    zBaseScheduling.set_membersc             C   s   t ‚dS )zReturns the next memberN)r   )r   r   r   r   Úget_nextC   s    zBaseScheduling.get_nextc             C   s   | j S )z(Returns the members of this loadbalancer)r   )r   r   r   r   ÚmembersG   s    zBaseScheduling.membersc             C   s   | j S )z"Returns the ratios for all members)r   )r   r   r   r   ÚratiosL   s    zBaseScheduling.ratiosN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Úpropertyr   r   r   r   r   r   r   '   s   r   c                   sT   e Zd ZdZ‡ fdd„Zedd„ ƒZdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ Z‡  ZS )ÚWeightedRoundRobinz.Class for doing Weighted Round Robin balancingc                s2   t t| ƒ ¡  g | _d| _d| _|r.| j|Ž  dS )ZInitializingr   N)Úsuperr   r   Ú_loadÚ_next_memberÚ_nr_membersr   )r   r   )Ú	__class__r   r   r   V   s    zWeightedRoundRobin.__init__c             C   s   | j S )zReturns the current load)r    )r   r   r   r   Úload`   s    zWeightedRoundRobin.loadc          	   G   sú   |sg | _ d S g }xd|D ]\}t|ƒ}yt t|d ƒ¡|d< W n& tjk
rd   tdj|d�ƒ‚Y nX | t	|ƒ¡ qW |j
dd„ dd� | j |kr˜d S || _ t|ƒ| _tdd	„ | j D ƒƒ}g | _x*| j D ] \}}| j t|| d
 ƒ¡ qÊW |  ¡  d S )Né   zMember '{member}' is invalid)Úmemberc             S   s   | d S )Nr%   r   )Úxr   r   r   Ú<lambda>t   s    z0WeightedRoundRobin.set_members.<locals>.<lambda>T)ÚkeyÚreversec             s   s   | ]}|d  V  qdS )r%   Nr   )Ú.0Úir   r   r   ú	<genexpr>z   s    z1WeightedRoundRobin.set_members.<locals>.<genexpr>r   )r   Úlistr   r   ÚstrÚInvalidOperationÚ
ValueErrorÚformatÚappendÚtupleÚsortÚlenr"   Úminr   r   Úreset)r   r   Znew_membersr&   Z
min_weightÚ_Zweightr   r   r   r   e   s,    


zWeightedRoundRobin.set_membersc             C   s   d| _ dg| j | _dS )zReset the loadr   N)r!   r"   r    )r   r   r   r   r8   €   s    zWeightedRoundRobin.resetc             C   sn   | j | jkr|  ¡  | j}x&| j| | j | kr@|d | j }qW | j|  d7  < |d | j | _| j| S )zReturns the next memberr%   )r   r    r8   r!   r"   r   )r   Úcurrentr   r   r   r   …   s    zWeightedRoundRobin.get_nextc             C   s   dj | j| j| jd�S )Nz&{class_}(load={load}, ratios={ratios}))Úclass_r$   r   )r2   r#   r$   r   )r   r   r   r   Ú__repr__—   s    zWeightedRoundRobin.__repr__c             C   s   | j |jkS )N)r   r   )r   Úotherr   r   r   Ú__eq__ž   s    zWeightedRoundRobin.__eq__)r   r   r   r   r   r   r$   r   r8   r   r<   r>   Ú__classcell__r   r   )r#   r   r   R   s   
r   )r   r   r   Úobjectr   r   r   r   r   r   Ú<module>   s   	+