dynatrace-api-balancer
Version:
A wrapper around Axios that balances and throttles requests across tenants, clusters and cluster nodes.
722 lines (149 loc) • 8.76 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Class: Throttle</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Class: Throttle</h1>
<section>
<header>
<h2><span class="attribs"><span class="type-signature"></span></span>Throttle<span class="signature">(limit, window)</span><span class="type-signature"></span></h2>
<div class="class-description"><p>The Throttle rate-limits access to a resource over a moving time window
using a 'leaky bucket' algorithm.</p></div>
</header>
<article>
<div class="container-overview">
<h2>Constructor</h2>
<h4 class="name" id="Throttle"><span class="type-signature"></span>new Throttle<span class="signature">(limit, window)</span><span class="type-signature"></span></h4>
<div class="description">
<p>Creates a throttle.</p>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>limit</code></td>
<td class="type">
<span class="param-type">number</span>
</td>
<td class="description last"><p>Number of requests allowed.</p></td>
</tr>
<tr>
<td class="name"><code>window</code></td>
<td class="type">
<span class="param-type">number</span>
</td>
<td class="description last"><p>Per this time window (in ms).</p></td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="lib_Throttle.js.html">lib/Throttle.js</a>, <a href="lib_Throttle.js.html#line8">line 8</a>
</li></ul></dd>
</dl>
</div>
<h3 class="subsection-title">Members</h3>
<h4 class="name" id="nextSlot"><span class="type-signature"></span>nextSlot<span class="type-signature"></span></h4>
<div class="description">
<p>Returns the time (ms) until the throttle opens again (plus 1ms).
Note that this getter just returns the delay - it does not update the throttle.</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="lib_Throttle.js.html">lib/Throttle.js</a>, <a href="lib_Throttle.js.html#line58">line 58</a>
</li></ul></dd>
</dl>
<h4 class="name" id="remainder"><span class="type-signature"></span>remainder<span class="type-signature"></span></h4>
<div class="description">
<p>Returns how much capacity is left for this time window. This value is useful
for selecting the least constricted resource among a pool of throttled resources.</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="lib_Throttle.js.html">lib/Throttle.js</a>, <a href="lib_Throttle.js.html#line50">line 50</a>
</li></ul></dd>
</dl>
<h4 class="name" id="waitTime"><span class="type-signature"></span>waitTime<span class="type-signature"></span></h4>
<div class="description">
<p>Returns the time (ms) until a next request can be honored (plus 1ms if there's a wait).
This is useful in case multiple throttles need to be checked before a request can be
consumed. Note that this getter updates the throttle's state before it produces a value.</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="lib_Throttle.js.html">lib/Throttle.js</a>, <a href="lib_Throttle.js.html#line68">line 68</a>
</li></ul></dd>
</dl>
<h3 class="subsection-title">Methods</h3>
<h4 class="name" id="consume"><span class="type-signature"></span>consume<span class="signature">()</span><span class="type-signature"></span></h4>
<div class="description">
<p>Consumes one unit of capacity. Should only be called if <a href="Throttle.html#waitTime"><code>waitTime</code></a> > 0.</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="lib_Throttle.js.html">lib/Throttle.js</a>, <a href="lib_Throttle.js.html#line92">line 92</a>
</li></ul></dd>
</dl>
<h5>Example</h5>
<pre class="prettyprint"><code>function doSomething() {
const delay = myThrottle.waitTime;
if (delay > 0)
return "I can't do this right now, but in " + delay + "ms I can.";
myThrottle.consume();
// Do it.
return "I did it";
}</code></pre>
<h4 class="name" id="permit"><span class="type-signature"></span>permit<span class="signature">()</span><span class="type-signature"></span></h4>
<div class="description">
<p>Returns a promise that is guaranteed to resolve (in FIFO order), but not sooner
than the throttle allows. For certain use cases this provides a more convenient
alternative compared to using the <a href="Throttle.html#waitTime"><code>waitTime</code></a> and
<a href="Throttle.html#consume"><code>consume()</code></a> pair.</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="lib_Throttle.js.html">lib/Throttle.js</a>, <a href="lib_Throttle.js.html#line108">line 108</a>
</li></ul></dd>
</dl>
<h5>Example</h5>
<pre class="prettyprint"><code>async function doSomething() {
await myThrottle.permit(); // Resolves immediately or as soon as possible.
// Do it.
return "I did it";
}</code></pre>
<h4 class="name" id="reset"><span class="type-signature"></span>reset<span class="signature">()</span><span class="type-signature"></span></h4>
<div class="description">
<p>Resets the throttle to maximum capacity.</p>
</div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="lib_Throttle.js.html">lib/Throttle.js</a>, <a href="lib_Throttle.js.html#line41">line 41</a>
</li></ul></dd>
</dl>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BalancedAPIRequest.html">BalancedAPIRequest</a></li><li><a href="CancellableEventEmitter.html">CancellableEventEmitter</a></li><li><a href="CancellablePromise.html">CancellablePromise</a></li><li><a href="DirectAPIRequest.html">DirectAPIRequest</a></li><li><a href="Throttle.html">Throttle</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Sun Dec 19 2021 10:28:40 GMT-0600 (Central Standard Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>