[#util-TokenBucket]
= xref:util.adoc[util]::TokenBucket
:relfileprefix: ../
:mrdocs:

A token bucket rate limiter&period;

== Synopsis

Declared in `&lt;util&sol;tokenbucket&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
template&lt;typename Clock&gt;
class TokenBucket;
----

== Description

Tokens are added at a steady rate (m&lowbar;rate per second) up to a capacity cap (m&lowbar;cap)&period; Tokens are removed by calling decrement(), which returns false if the bucket is emptied&period;

Typical usage&colon; bucket&period;increment(now)&semi; &sol;&sol; refill based on elapsed time if (bucket&period;value() &gt;&equals; 1) bucket&period;decrement(1)&semi; &sol;&sol; consume a token

== Type Aliases

[cols="1,4"]
|===
| Name| Description
| xref:util/TokenBucket/clock.adoc[`clock`] 
| The clock type used to measure elapsed time&period;
| xref:util/TokenBucket/duration.adoc[`duration`] 
| The clock&apos;s duration type&period;
| xref:util/TokenBucket/time_point.adoc[`time&lowbar;point`] 
| The clock&apos;s point&hyphen;in&hyphen;time type, used for refill timestamps&period;
|===


== Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:util/TokenBucket/2constructor.adoc[`TokenBucket`]         [.small]#[constructor]#
| Construct a token bucket with a given rate, initial balance, and capacity&period;
| xref:util/TokenBucket/decrement.adoc[`decrement`] 
| Consume n tokens&period; Returns false if the balance dropped to&sol;below the given floor&period;
| xref:util/TokenBucket/increment.adoc[`increment`] 
| Refill tokens based on elapsed time since last call&period; No refill occurs on the first call (establishes the time baseline)&period;
| xref:util/TokenBucket/value.adoc[`value`] 
| Current token balance&period;
|===


== Data Members

[cols="1,4"]
|===
| Name| Description
| xref:util/TokenBucket/m_cap.adoc[`m&lowbar;cap`] 
| Maximum token balance
| xref:util/TokenBucket/m_rate.adoc[`m&lowbar;rate`] 
| Tokens added per second
|===



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