HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //proc/thread-self/root/lib/python3/dist-packages/twisted/protocols/__pycache__/htb.cpython-38.pyc
U


W[o$�@s�dZddlmZddlmZmZddlmZGdd�d�ZGdd�de�Zee�Gd	d
�d
��Z	Gdd�de	�Z
Gd
d�de	�ZGdd�dej�Z
Gdd�de
�ZGdd�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@s6eZdZdZdZdZdZd
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}
    NrcCsd|_||_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}
        rN)�content�parentBucketr�lastDrip)�selfr�r�7/usr/lib/python3/dist-packages/twisted/protocols/htb.py�__init__1s	zBucket.__init__cCsR|��|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�minrr�add)r
�amountZ	allowablerrrr?s


z
Bucket.addcCsb|jdk	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)rr�raterrr	�max)r
ZnowZ	deltaTimeZdeltaTokensrrrrXs





zBucket.drip)N)
�__name__�
__module__�__qualname__�__doc__rr�	_refcountr
rrrrrrrs
rc@seZdZdd�ZdS)�
IBucketFiltercOsdS)zn
        Return a L{Bucket} corresponding to the provided parameters.

        @returntype: L{Bucket}
        Nr)Z
somethingsZsome_kwrrr�getBucketForqszIBucketFilter.getBucketForN)rrrrrrrrrpsrc@s:eZdZdZeZdZddd�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}
    NcCsi|_||_t�|_dS�N)�buckets�parentFilterr�	lastSweep)r
rrrrr
�sz!HierarchicalBucketFilter.__init__cOs||jdk	r$t�|j|jkr$|��|jr@|jj|f|�|�}nd}|j||�}|j�|�}|dkrx|�	|�}||j|<|S)z�
        Find or create a L{Bucket} corresponding to the provided parameters.

        Any parameters are passed on to L{getBucketKey}, from them it
        decides which bucket you get.

        @returntype: L{Bucket}
        N)
�
sweepIntervalrr �sweeprr�getBucketKeyr�get�
bucketFactory)r
�a�kwr�key�bucketrrrr�s	
�

z%HierarchicalBucketFilter.getBucketForcOsdS)a%
        Construct a key based on the input parameters to choose a L{Bucket}.

        The default implementation returns the same key for all
        arguments. Override this method to provide L{Bucket} selection.

        @returns: Something to be used as a key in the bucket cache.
        Nr)r
r&r'rrrr#�s	z%HierarchicalBucketFilter.getBucketKeycCs>|j��D]&\}}|��}|jdkr
|r
|j|=q
t�|_dS)z'
        Remove empty buckets.
        rN)r�itemsrrrr )r
r(r)Zbucket_is_emptyrrrr"�s

zHierarchicalBucketFilter.sweep)N)rrrrrr%r!r
rr#r"rrrrrxs
rc@seZdZdZdZdd�ZdS)�FilterByHostzF
    A Hierarchical Bucket filter with a L{Bucket} for each host.
    i�cCs|��dS�N�)ZgetPeer�r
�	transportrrrr#�szFilterByHost.getBucketKeyN�rrrrr!r#rrrrr+�sr+c@seZdZdZdZdd�ZdS)�FilterByServerzI
    A Hierarchical Bucket filter with a L{Bucket} for each service.
    NcCs|��dS)N�)ZgetHostr.rrrr#�szFilterByServer.getBucketKeyr0rrrrr1�sr1c@s,eZdZdZdZdd�Zdd�Zdd�Zd	S)
�ShapedConsumerzL
    Wraps a C{Consumer} and shapes the rate at which it receives data.
    FcCs(tj�||�||_|jjd7_dSr,)r�ProducerConsumerProxyr
r)r)r
�consumerr)rrrr
�szShapedConsumer.__init__cCs&|j�t|��}tj�||d|��Sr)r)r�lenrr4�_writeSomeData)r
�datarrrrr7�szShapedConsumer._writeSomeDatacCs tj�|�|jjd8_dSr,)rr4�
stopProducingr)r)r
rrrr9�szShapedConsumer.stopProducingN)rrrr�iAmStreamingr
r7r9rrrrr3�s
r3c@seZdZdZdZdd�ZdS)�ShapedTransportaR
    Wraps a C{Transport} and shapes the rate at which it receives data.

    This is a L{ShapedConsumer} with a little bit of magic to provide for
    the case where the consumer it wraps is also a C{Transport} and people
    will be attempting to access attributes this does not proxy as a
    C{Consumer} (e.g. C{loseConnection}).
    FcCst|j|�Sr)�getattrr5)r
�namerrr�__getattr__�szShapedTransport.__getattr__N)rrrrr:r>rrrrr;�s
r;c@s eZdZdZdd�Zdd�ZdS)�ShapedProtocolFactorya�
    Dispense C{Protocols} with traffic shaping on their transports.

    Usage::

        myserver = SomeFactory()
        myserver.protocol = ShapedProtocolFactory(myserver.protocol,
                                                  bucketFilter)

    Where C{SomeServerFactory} is a L{twisted.internet.protocol.Factory}, and
    C{bucketFilter} is an instance of L{HierarchicalBucketFilter}.
    cCs||_||_dS)a�
        Tell me what to wrap and where to get buckets.

        @param protoClass: The class of C{Protocol} this will generate
          wrapped instances of.
        @type protoClass: L{Protocol<twisted.internet.interfaces.IProtocol>}
          class
        @param bucketFilter: The filter which will determine how
          traffic is shaped.
        @type bucketFilter: L{HierarchicalBucketFilter}.
        N)�protocol�bucketFilter)r
Z
protoClassrArrrr
szShapedProtocolFactory.__init__cs*�j||�}|j���fdd�}||_|S)z�
        Make a C{Protocol} instance with a shaped transport.

        Any parameters will be passed on to the protocol's initializer.

        @returns: A C{Protocol} instance with a L{ShapedTransport}.
        cs�j�|�}t||�}�|�Sr)rArr;)r/r)ZshapedTransport�ZorigMakeConnectionr
rr�makeConnection"s
z6ShapedProtocolFactory.__call__.<locals>.makeConnection)r@rC)r
r&r'�protorCrrBr�__call__s
zShapedProtocolFactory.__call__N)rrrrr
rErrrrr?�sr?N)rrZzope.interfacerrZtwisted.protocolsrrrrr+r1r4r3r;r?rrrr�<module>sTA