File: //proc/thread-self/root/lib/python3/dist-packages/twisted/protocols/__pycache__/htb.cpython-38.pyc
U
W[o$ � @ s� d Z ddlmZ ddlmZmZ ddlmZ G dd� d�ZG dd� de�Zee�G d d
� d
��Z G dd� de �Z
G d
d� de �ZG dd� dej�Z
G dd� de
�ZG dd� d�ZdS )a�
Hierarchical Token Bucket traffic shaping.
Patterned after U{Martin Devera's Hierarchical Token Bucket traffic
shaper for the Linux kernel<http://luxik.cdi.cz/~devik/qos/htb/>}.
@seealso: U{HTB Linux queuing discipline manual - user guide
<http://luxik.cdi.cz/~devik/qos/htb/manual/userg.htm>}
@seealso: U{Token Bucket Filter in Linux Advanced Routing & Traffic Control
HOWTO<http://lartc.org/howto/lartc.qdisc.classless.html#AEN682>}
� )�time)�implementer� Interface)�pcpc @ s6 e Zd ZdZdZdZdZd
dd�Zdd� Zdd � Z dS )�Bucketa�
Implementation of a Token bucket.
A bucket can hold a certain number of tokens and it drains over time.
@cvar maxburst: The maximum number of tokens that the bucket can
hold at any given time. If this is L{None}, the bucket has
an infinite size.
@type maxburst: C{int}
@cvar rate: The rate at which the bucket drains, in number
of tokens per second. If the rate is L{None}, the bucket
drains instantaneously.
@type rate: C{int}
Nr c C s d| _ || _t� | _dS )a(
Create a L{Bucket} that may have a parent L{Bucket}.
@param parentBucket: If a parent Bucket is specified,
all L{add} and L{drip} operations on this L{Bucket}
will be applied on the parent L{Bucket} as well.
@type parentBucket: L{Bucket}
r N)�content�parentBucketr �lastDrip)�selfr � r �7/usr/lib/python3/dist-packages/twisted/protocols/htb.py�__init__1 s zBucket.__init__c C sR | � � | jdkr|}nt|| j| j �}| jdk r@| j�|�}| j|7 _|S )at
Adds tokens to the L{Bucket} and its C{parentBucket}.
This will add as many of the C{amount} tokens as will fit into both
this L{Bucket} and its C{parentBucket}.
@param amount: The number of tokens to try to add.
@type amount: C{int}
@returns: The number of tokens that actually fit.
@returntype: C{int}
N)�drip�maxburst�minr r �add)r
�amountZ allowabler r r r ? s
z
Bucket.addc C sb | j dk r| j �� | jdkr&d| _n2t� }|| j }|| j }td| j| �| _|| _| jdkS )z�
Let some of the bucket drain.
The L{Bucket} drains at the rate specified by the class
variable C{rate}.
@returns: C{True} if the bucket is empty after this drip.
@returntype: C{bool}
Nr )r r �rater r r �max)r
ZnowZ deltaTimeZdeltaTokensr r r r X s
zBucket.drip)N)
�__name__�
__module__�__qualname__�__doc__r r � _refcountr
r r r r r r r s
r c @ s e Zd Zdd� ZdS )�
IBucketFilterc O s dS )zn
Return a L{Bucket} corresponding to the provided parameters.
@returntype: L{Bucket}
Nr )Z
somethingsZsome_kwr r r �getBucketForq s zIBucketFilter.getBucketForN)r r r r r r r r r p s r c @ s: e Zd ZdZeZdZddd�Zdd� Zdd� Z d d
� Z
dS )�HierarchicalBucketFilterz�
Filter things into buckets that can be nested.
@cvar bucketFactory: Class of buckets to make.
@type bucketFactory: L{Bucket}
@cvar sweepInterval: Seconds between sweeping out the bucket cache.
@type sweepInterval: C{int}
Nc C s i | _ || _t� | _d S �N)�buckets�parentFilterr � lastSweep)r
r r r r r
� s z!HierarchicalBucketFilter.__init__c O s| | j dk r$t� | j | j kr$| �� | jr@| jj| f|�|�}nd}| j||�}| j�|�}|dkrx| � |�}|| j|<