Ϫf$dZddlmZddlmZddlmZmZddlmZGddZ Gdd eZ ee Gd d Z Gd d e Z Gdde Z GddejZGddeZGddZy)a Hierarchical Token Bucket traffic shaping. Patterned after U{Martin Devera's Hierarchical Token Bucket traffic shaper for the Linux kernel}. @seealso: U{HTB Linux queuing discipline manual - user guide } @seealso: U{Token Bucket Filter in Linux Advanced Routing & Traffic Control HOWTO} )time)Optional) Interface implementer)pcpcReZdZUdZdZeeed<dZeeed<dZ d dZ dZ dZ y) 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} Nmaxburstraterc>d|_||_t|_y)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 parentBucketrlastDrip)selfrs 7/usr/lib/python3/dist-packages/twisted/protocols/htb.py__init__zBucket.__init__3s ( c|j|j|}n#t||j|jz }|j|jj |}|xj|z c_|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} )dripr minr radd)ramount allowables rrz Bucket.add@sk == IFDMMDLL$@AI    ())--i8I ! rc.|j|jj|jd|_nMt }||j z }||jz}t d|j|z |_||_|jdk(S)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)rrr r rrmax)rnow deltaTime deltaTokenss rrz Bucket.dripXs    (    " " $ 99 DL&Cdmm+I#dii/Kq$,,"<=DLDM||q  rN) __name__ __module__ __qualname____doc__r rint__annotations__r _refcountrrrrrr r s9 #Hhsm"D(3-I 0!rr ceZdZdZy) IBucketFiltercy)zn Return a L{Bucket} corresponding to the provided parameters. @returntype: L{Bucket} Nr') somethingssome_kws r getBucketForzIBucketFilter.getBucketForqsrN)r r!r"r-r'rrr)r)ps rr)cDeZdZUdZeZdZeee d<ddZ dZ dZ dZ y) 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} N sweepIntervalc>i|_||_t|_yr)buckets parentFilterr lastSweep)rr3s rrz!HierarchicalBucketFilter.__init__s (rc|j4t|jz |jkDr|j|jr!|jj |g|i|}nd}|j |i|}|jj|}| |j|}||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) r0rr4sweepr3r- getBucketKeyr2get bucketFactory)rakwrkeybuckets rr-z%HierarchicalBucketFilter.getBucketFors    * Vdnn $(:(: : JJL   94,,99$IIbILLd)b)!!#& >'' 5F &DLL  rcy)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')rr:r;s rr7z%HierarchicalBucketFilter.getBucketKeysrc|jjD]5\}}|j}|jdk(s&|s)|j|=7t |_y)z' Remove empty buckets. rN)r2itemsrr&rr4)rr<r=bucket_is_emptys rr6zHierarchicalBucketFilter.sweepsV <<--/ &KC$kkmO  A%?LL% & rr)r r!r"r#r r9r0rr$r%rr-r7r6r'rrr/r/ys0M#'M8C=' 4   rr/ceZdZdZdZdZy) FilterByHostzF A Hierarchical Bucket filter with a L{Bucket} for each host. ic(|jdSN)getPeerr transports rr7zFilterByHost.getBucketKey  "1%%rNr r!r"r#r0r7r'rrrCrCsM&rrCceZdZdZdZdZy)FilterByServerzI A Hierarchical Bucket filter with a L{Bucket} for each service. Nc(|jdS)N)getHostrHs rr7zFilterByServer.getBucketKeyrJrrKr'rrrMrMsM&rrMc&eZdZdZdZdZdZdZy)ShapedConsumerzL Wraps a C{Consumer} and shapes the rate at which it receives data. Fctjj||||_|jxjdz c_yrE)rProducerConsumerProxyrr=r&)rconsumerr=s rrzShapedConsumer.__init__s4 !!**4:  "rc|jjt|}tjj ||d|Sr)r=rlenrrT_writeSomeData)rdatars rrXzShapedConsumer._writeSomeDatas9 T+((77d7FmLLrctjj||jxjdzc_yrE)rrT stopProducingr=r&)rs rr[zShapedConsumer.stopProducings+ !!//5 "rN)r r!r"r# iAmStreamingrrXr[r'rrrRrRs L# M#rrRceZdZdZdZdZy)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}). Fc.t|j|Sr)getattrrU)rnames r __getattr__zShapedTransport.__getattr__st}}d++rN)r r!r"r#r\rbr'rrr^r^sL,rr^ceZdZdZdZdZy)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}. c ||_||_y)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} class @param bucketFilter: The filter which will determine how traffic is shaped. @type bucketFilter: L{HierarchicalBucketFilter}. N)protocol bucketFilter)r protoClassrgs rrzShapedProtocolFactory.__init__s# (rc`j|i|}|jfd}||_|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}. cbjj|}t||}|Sr)rgr-r^)rIr=shapedTransportorigMakeConnectionrs rmakeConnectionz6ShapedProtocolFactory.__call__..makeConnection,s0&&33I>F-i@O%o6 6r)rfrm)rr:r;protormrls` @r__call__zShapedProtocolFactory.__call__!s: q'B'"11 7 . rN)r r!r"r#rror'rrrdrds )"rrdN)r#rtypingrzope.interfacerrtwisted.protocolsrr r)r/rCrMrTrRr^rdr'rrrss  1!O!O!d I  ]@ @ @ F&+&&-&#S..#6,n,(00r