+
    Dfj$                        R t ^ RIHt ^ RIHt ^ RIHtHt ^ RIHt  ! R R4      t	 ! R R]4      t
]! ]
4       ! R	 R
4      4       t ! R R]4      t ! R R]4      t ! R R]P                  4      t ! R R]4      t ! R R4      tR# )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)Optional)	Interfaceimplementer)pcpc                   R   a  ] tR t^t o RtRtRt^ tRR ltR t	R t
V 3R ltRtV tR# )	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}
Nc                >    ^ V n         Wn        \        4       V n        R# )z
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}
N)contentparentBucketr   lastDrip)selfr   s   &&7/usr/lib/python3/dist-packages/twisted/protocols/htb.py__init__Bucket.__init__3   s     (    c                   V P                  4        V P                  f   TpM&\        WP                  V P                  ,
          4      pV P                  e   V P                  P                  V4      pV ;P                  V,          un        V# )a4  
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}
)dripmaxburstminr
   r   add)r   amount	allowables   && r   r   
Bucket.add@   si     			== IFMMDLL$@AI())--i8I	!r   c                H   V P                   e   V P                   P                  4        V P                  f	   ^ V n        MV\	        4       pWP
                  ,
          pW P                  ,          p\        ^ V P                  V,
          4      V n        Wn        V P                  ^ 8H  # )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}
)r   r   rater
   r   r   max)r   now	deltaTimedeltaTokenss   &   r   r   Bucket.dripX   s}     (""$99DL&Cmm+I#ii/Kq$,,"<=DLM||q  r   c                R   < V ^8  d   Qh/ S[ S[,          ;R&   S[ S[,          ;R&   # )   r   r   r   int)format__classdict__s   "r   __annotate__Bucket.__annotate__   s'       sm"! " 3-# r   )r
   r   r   N)__name__
__module____qualname____firstlineno____doc__r   r   	_refcountr   r   r   __annotate_func____static_attributes____classdictcell__r&   s   @r   r   r      s2      #HDI0!u  r   r   c                   &   a  ] tR t^pt o R tRtV tR# )IBucketFilterc                     R# )zV
Return a L{Bucket} corresponding to the provided parameters.

@returntype: L{Bucket}
N )
somethingssome_kws   *,r   getBucketForIBucketFilter.getBucketForq   s    r   r7   N)r*   r+   r,   r-   r:   r1   r2   r3   s   @r   r5   r5   p   s      r   r5   c                   T   a  ] tR t^yt o Rt]tRtR	R ltR t	R t
R tV 3R ltRtV tR# )
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                >    / V n         Wn        \        4       V n        R # r)   )bucketsparentFilterr   	lastSweep)r   r@   s   &&r   r   !HierarchicalBucketFilter.__init__   s    (r   c                   V P                   e;   \        4       V P                  ,
          V P                   8  d   V P                  4        V P                  '       d"   V P                  P
                  ! V .VO5/ VB pMRpV P                  ! V/ VB pV P                  P                  V4      pVf    V P                  V4      pWPP                  V&   V# )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)
sweepIntervalr   rA   sweepr@   r:   getBucketKeyr?   getbucketFactory)r   akwr   keybuckets   &*,   r   r:   %HierarchicalBucketFilter.getBucketFor   s     *Vdnn$(:(::JJL,,99$IIbILL)b)!!#&>''5F &LLr   c                    R# )z
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.
Nr7   )r   rI   rJ   s   &*,r   rF   %HierarchicalBucketFilter.getBucketKey   s     r   c                    V P                   P                  4        F?  w  rVP                  4       pVP                  ^ 8X  g   K(  V'       g   K2  V P                   V KA  	  \	        4       V n        R# )z
Remove empty buckets.
N)r?   itemsr   r/   r   rA   )r   rK   rL   bucket_is_emptys   &   r   rE   HierarchicalBucketFilter.sweep   sQ      <<--/KC$kkmO  A%??LL% 0
 r   c                6   < V ^8  d   Qh/ S[ S[,          ;R&   # )r"   rD   r#   )r%   r&   s   "r   r'   %HierarchicalBucketFilter.__annotate__y   s      C=' r   )r?   rA   r@   r)   )r*   r+   r,   r-   r.   r   rH   rD   r   r:   rF   rE   r0   r1   r2   r3   s   @r   r=   r=   y   s2      M#'M 
4		 q  r   r=   c                   .   a  ] tR t^t o RtRtR tRtV tR# )FilterByHostz>
A Hierarchical Bucket filter with a L{Bucket} for each host.
c                0    VP                  4       ^,          # )   )getPeerr   	transports   &&r   rF   FilterByHost.getBucketKey         "1%%r   r7   Ni  	r*   r+   r,   r-   r.   rD   rF   r1   r2   r3   s   @r   rW   rW      s      M& &r   rW   c                   .   a  ] tR t^t o RtRtR tRtV tR# )FilterByServerzA
A Hierarchical Bucket filter with a L{Bucket} for each service.
Nc                0    VP                  4       ^,          # )r"   )getHostr[   s   &&r   rF   FilterByServer.getBucketKey   r^   r   r7   r_   r3   s   @r   ra   ra      s      M& &r   ra   c                   :   a  ] tR t^t o RtRtR tR tR tRt	V t
R# )ShapedConsumerzD
Wraps a C{Consumer} and shapes the rate at which it receives data.
Fc                    \         P                  P                  W4       W n        V P                  ;P                  ^,          un        R# rY   N)r   ProducerConsumerProxyr   rL   r/   )r   consumerrL   s   &&&r   r   ShapedConsumer.__init__   s0    !!**4:"r   c                    V P                   P                  \        V4      4      p\        P                  P                  WR V 4      # r)   )rL   r   lenr   ri   _writeSomeData)r   datar   s   && r   rn   ShapedConsumer._writeSomeData   s7    
 T+((777FmLLr   c                    \         P                  P                  V 4       V P                  ;P                  ^,          un        R# rh   )r   ri   stopProducingrL   r/   )r   s   &r   rr   ShapedConsumer.stopProducing   s+    !!//5"r   )rL   N)r*   r+   r,   r-   r.   iAmStreamingr   rn   rr   r1   r2   r3   s   @r   rf   rf      s&      L#
M# #r   rf   c                   .   a  ] tR t^t o RtRtR tRtV tR# )ShapedTransporta:  
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}).
Fc                .    \        V P                  V4      # r)   )getattrrj   )r   names   &&r   __getattr__ShapedTransport.__getattr__   s     t}}d++r   r7   N)	r*   r+   r,   r-   r.   rt   rz   r1   r2   r3   s   @r   rv   rv      s      L, ,r   rv   c                   0   a  ] tR tRt o RtR tR tRtV tR# )ShapedProtocolFactoryi  ao  
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}.
c                    Wn         W n        R# )aT  
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   
protoClassr   s   &&&r   r   ShapedProtocolFactory.__init__  s     #(r   c                `   a a S P                   ! V/ VB pVP                  oVV 3R lpWCn        V# )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}.
c                 `   < SP                   P                  V 4      p\        W4      pS! V4      # r)   )r   r:   rv   )r\   rL   shapedTransportorigMakeConnectionr   s   &  r   makeConnection6ShapedProtocolFactory.__call__.<locals>.makeConnection,  s.    &&33I>F-i@O%o66r   )r   r   )r   rI   rJ   protor   r   s   f*,  @r   __call__ShapedProtocolFactory.__call__!  s6     q'B'"11	7
  .r   )r   r   N)	r*   r+   r,   r-   r.   r   r   r1   r2   r3   s   @r   r}   r}     s     )" r   r}   N)r.   r   typingr   zope.interfacer   r   twisted.protocolsr   r   r5   r=   rW   ra   ri   rf   rv   r}   r7   r   r   <module>r      s   
    1 !O! O!dI  ]@  @  @ F&+ &&- &#S.. #6,n ,(0 0r   