[#BloombergLP-balb-LeakyBucket]
= xref:BloombergLP.adoc[BloombergLP]::xref:BloombergLP/balb.adoc[balb]::LeakyBucket
:relfileprefix: ../../
:mrdocs:

This mechanism implements a leaky bucket that allows clients to monitor whether a resource is being consumed at a particular rate&period; The behavior of a leak bucket is determined by two properties&colon; the drain rate (in units&sol;s) and capacity (in units), both of which can be specified at construction or using the `setRateAndCapacity` method&period;

== Synopsis

Declared in `&lt;balb&lowbar;leakybucket&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
class LeakyBucket;
----

== Description

Units can be added to a leaky bucket by either submitting them using the `submit` method or reserving them using the `reserve` method&period; Submitted units are removed from a leaky bucket at the drain rate, while reserved units stays unaffected in a leaky bucket until they are either cancelled (removed from the leaky bucket) using the `cancelReserved` method or submitted using the `submitReserved` method&period;

Adding units to a leaky bucket will cause it to overflow if after the units are added, the total number of units in the leaky bucket (including both submitted and reserved units) exceeds its capacity&period; A leaky bucket can be queried whether adding a specified number of units would cause it to overflow via the `wouldOverflow` method&period; If submitting units to a leaky bucket will cause it to overflow, the estimated amount of time to wait before 1 more units can be submitted without causing the leaky bucket to overflow can be determined using the `calculateTimeToSubmit` method&period;

The state of a leaky bucket must be updated manually using the `updateState` method supplying the current time interval&period; The time intervals supplied should all refer to the same time origin&period;

A leaky bucket keeps some statistics, including the number of submitted units, that can be accessed using the `getStatistics` method and reset using the `resetStatistics` method&period;

The class invariants are&colon; &ast; `capacity() &gt; 0` &ast; `drainRate() &gt; 0`

This class&colon; &ast; is _exception_ _neutral_ (agnostic) &ast; is _const_ _thread&hyphen;safe_ For terminology see `bsldoc&lowbar;glossary`&period;

== Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:BloombergLP/balb/LeakyBucket/2constructor.adoc[`LeakyBucket`]         [.small]#[constructor]#
| Create an empty leaky bucket having the specified `drainRate`, the specified `capacity`, and the specified `currentTime` as the initial `lastUpdateTime`&period; The behavior is undefined unless `0 &lt; newRate`, `0 &lt; newCapacity`, and `LLONG&lowbar;MIN !&equals; currentTime&period;seconds()`&period;
| xref:BloombergLP/balb/LeakyBucket/2destructor.adoc[`&#126;LeakyBucket`] [.small]#[destructor]#
| Destroy this object&period;
| xref:BloombergLP/balb/LeakyBucket/calculateTimeToSubmit.adoc[`calculateTimeToSubmit`] 
| If 1 more unit can be submitted to this leaky bucket without causing it to overflow, then return a time interval of 0 immediately&period; Otherwise, first update the state of this leaky bucket to the specified `currentTime`&period; Then, return the estimated time interval that should pass from `currentTime` until 1 more unit can be submitted to this leaky bucket without causing it to overflow&period; The number of nanoseconds in the returned time interval is rounded up&period; Note that a time interval of 0 can still be return after the state of this leaky bucket has been updated to `currentTime`&period; Also note that after waiting for the returned time interval, clients should typically check again using this method, because additional units may have been submitted in the interim&period; The behavior is undefined unless `LLONG&lowbar;MIN !&equals; currentTime&period;seconds()` and the total number of seconds in the time interval resulting from `currentTime &hyphen; lastUpdateTime()` can be represented with a 64&hyphen;bit signed integer&period;
| xref:BloombergLP/balb/LeakyBucket/cancelReserved.adoc[`cancelReserved`] 
| Cancel the specified `numUnits` that were previously reserved&period; This method reduces the number of reserved units by `numUnits`&period; The behavior is undefined unless `numUnits &lt;&equals; unitsReserved()`&period;
| xref:BloombergLP/balb/LeakyBucket/capacity.adoc[`capacity`] 
| Return the capacity of this leaky bucket&period;
| xref:BloombergLP/balb/LeakyBucket/drainRate.adoc[`drainRate`] 
| Return the drain rate of this leaky bucket&period;
| xref:BloombergLP/balb/LeakyBucket/getStatistics.adoc[`getStatistics`] 
| Load, into the specified `submittedUnits` and the specified `unusedUnits` respectively, the numbers of submitted units and the number of unused units for this leaky bucket from the `statisticsCollectionStartTime` to the `lastUpdateTime`&period; The number of unused units is the difference between the number of units that could have been consumed and the number of units actually submitted for the time period&period;
| xref:BloombergLP/balb/LeakyBucket/lastUpdateTime.adoc[`lastUpdateTime`] 
| Return the time interval when this leaky bucket was last updated&period;
| xref:BloombergLP/balb/LeakyBucket/reserve.adoc[`reserve`] 
| Reserve the specified `numUnits` for future use by this leaky bucket&period; The behavior is undefined unless &apos;unitsReserved() &plus; unitsInBucket() &plus; numUnits&apos; can be represented by a 64&hyphen;bit unsigned integral type&period; Note that after this operation, this bucket may overflow&period; Also note that the time interval between the invocations of `reserve` and `submitReserved` or `cancelReserved` should be kept as short as possible&semi; otherwise, the precision of the time interval calculated by `calculateTimeToSubmit` may be negatively affected&period;
| xref:BloombergLP/balb/LeakyBucket/reset.adoc[`reset`] 
| Reset the following statistic counters for this leaky bucket to 0&colon; `unitsInBucket`, `unitsReserved`, `submittedUnits`, and `unusedUnits`&period; Set the `lastUpdateTime` and the `statisticCollectionStartTime` to the specified `currentTime` of this leaky bucket&period; The behavior is undefined unless &apos;LLONG&lowbar;MIN !&equals; currentTime&period;seconds()&apos;&period;
| xref:BloombergLP/balb/LeakyBucket/resetStatistics.adoc[`resetStatistics`] 
| Reset the statics collected for this leaky bucket by setting the number of units used and the number of units submitted to 0, and set the `statisticsCollectionStartTime` to the `lastUpdateTime` of this leaky bucket&period;
| xref:BloombergLP/balb/LeakyBucket/setRateAndCapacity.adoc[`setRateAndCapacity`] 
| Set the drain rate of this leaky bucket to the specified `newRate` and the capacity of this leaky bucket to the specified `newCapacity`&period; The behavior is undefined unless `0 &lt; newRate` and `0 &lt; newCapacity`&period;
| xref:BloombergLP/balb/LeakyBucket/statisticsCollectionStartTime.adoc[`statisticsCollectionStartTime`] 
| Return the time interval when the collection of the statistics (as returned by `getStatistics`) started&period;
| xref:BloombergLP/balb/LeakyBucket/submit.adoc[`submit`] 
| Submit the specified `numUnits` to this leaky bucket&period; The behavior is undefined unless `unitsReserved() &plus; unitsInBucket() &plus; numUnits` can be represented by a 64&hyphen;bit unsigned integral type&period; Note that after this operation, this leaky bucket may overflow&period;
| xref:BloombergLP/balb/LeakyBucket/submitReserved.adoc[`submitReserved`] 
| Submit the specified `numUnits` that were previously reserved&period; This method reduces the number of reserved units by `numUnits` and submits `numUnits` to this leaky bucket&period; The behavior is undefined unless `numUnits &lt;&equals; unitsReserved()`&period;
| xref:BloombergLP/balb/LeakyBucket/unitsInBucket.adoc[`unitsInBucket`] 
| Return the number of submitted units in this leaky bucket&period;
| xref:BloombergLP/balb/LeakyBucket/unitsReserved.adoc[`unitsReserved`] 
| Return the number of reserved units in this leaky bucket&period;
| xref:BloombergLP/balb/LeakyBucket/updateState.adoc[`updateState`] 
| Set the `lastUpdateTime` of this leaky bucket to the specified `currentTime`&period; If `currentTime` is after `lastUpdateTime`, then update the `unitsInBucket` of this leaky bucket by subtracting from it the number of units drained from `lastUpdateTime` to `currentTime`&period; If `currentTime` is before the `statisticsCollectionStartTime` of this leaky bucket, set `statisticsCollectionStartTime` to `currentTime`&period; The behavior is undefined unless `LLONG&lowbar;MIN !&equals; currentTime&period;seconds()` and the total number of seconds in the time interval resulting from `currentTime &hyphen; lastUpdateTime()` can be represented with a 64&hyphen;bit signed integer&period;
| xref:BloombergLP/balb/LeakyBucket/wouldOverflow.adoc[`wouldOverflow`] 
| Update the state of this this leaky bucket to the specified `currentTime`, and return `true` if adding 1 more unit to this leaky bucket would cause the total number of units held by this leaky bucket to exceed its capacity, and `false` otherwise&period; Note that this method counts both submitted units and reserved units toward the total number of units held by this leaky bucket&period; The behavior is undefined unless `LLONG&lowbar;MIN !&equals; currentTime&period;seconds()` and the total number of seconds in the time interval resulting from `currentTime &hyphen; lastUpdateTime()` can be represented with a 64&hyphen;bit signed integer&period;
|===


== Static Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:BloombergLP/balb/LeakyBucket/calculateCapacity.adoc[`calculateCapacity`] 
| Return the capacity of a leaky bucket as the rounded&hyphen;down product of the specified `drainRate` by the specified `timeWindow`&period; If the result evaluates to 0, return 1&period; The behavior is undefined unless the product of `drainRate` and `timeWindow` can be represented by a 64&hyphen;bit unsigned integral type&period;
| xref:BloombergLP/balb/LeakyBucket/calculateDrainTime.adoc[`calculateDrainTime`] 
| Return the time interval required to drain the specified `numUnits` at the specified `drainRate`, round up the number of nanoseconds in the time interval if the specified `ceilFlag` is set to `true`, otherwise, round down the number of nanoseconds&period; The behavior is undefined unless the number of seconds in the calculated interval may be represented by a 64&hyphen;bit signed integral type&period;
| xref:BloombergLP/balb/LeakyBucket/calculateTimeWindow.adoc[`calculateTimeWindow`] 
| Return the time interval over which a leaky bucket _approximates_ a moving&hyphen;total of submitted units, as the rounded&hyphen;down ratio between the specified `capacity` and the specified `drainRate`&period; If the rounded ratio is 0, return a time interval of 1 nanosecond&period; The behavior is undefined unless `drainRate &gt; 0` and `capacity &sol; drainRate` can be represented with 64&hyphen;bit signed integral type&period;
|===



[.small]#Created with https://www.mrdocs.com[MrDocs]#
