[#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;

== Synopsis

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

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

== Description

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;

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 with the specified rate and capacity&period;
| xref:BloombergLP/balb/LeakyBucket/2destructor.adoc[`&#126;LeakyBucket`] [.small]#[destructor]#
| Destroy this object&period;
| xref:BloombergLP/balb/LeakyBucket/calculateTimeToSubmit.adoc[`calculateTimeToSubmit`] 
| Return the estimated wait before one more unit can be submitted&period;
| xref:BloombergLP/balb/LeakyBucket/cancelReserved.adoc[`cancelReserved`] 
| Cancel the specified `numUnits` that were previously reserved&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 submitted and unused unit statistics for this leaky bucket&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;
| xref:BloombergLP/balb/LeakyBucket/reset.adoc[`reset`] 
| Reset the counters and update times for this leaky bucket&period;
| xref:BloombergLP/balb/LeakyBucket/resetStatistics.adoc[`resetStatistics`] 
| Reset the statistics collected for this leaky bucket&period;
| xref:BloombergLP/balb/LeakyBucket/setRateAndCapacity.adoc[`setRateAndCapacity`] 
| Set the drain rate and capacity of this leaky bucket&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;
| xref:BloombergLP/balb/LeakyBucket/submitReserved.adoc[`submitReserved`] 
| Submit the specified `numUnits` that were previously reserved&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`] 
| Update this leaky bucket&apos;s state to the specified `currentTime`&period;
| xref:BloombergLP/balb/LeakyBucket/wouldOverflow.adoc[`wouldOverflow`] 
| Return whether submitting one more unit would exceed capacity&period;
|===


== Static Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:BloombergLP/balb/LeakyBucket/calculateCapacity.adoc[`calculateCapacity`] 
| Return the capacity for a leaky bucket with the given rate and window&period;
| xref:BloombergLP/balb/LeakyBucket/calculateDrainTime.adoc[`calculateDrainTime`] 
| Return the time interval required to drain the specified units&period;
| xref:BloombergLP/balb/LeakyBucket/calculateTimeWindow.adoc[`calculateTimeWindow`] 
| Return the time window approximated by a leaky bucket of given size&period;
|===



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