| <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Service load measurement"><meta name="keywords" content="rust, rustlang, rust-lang, load"><title>tower::load - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../normalize.css"><link rel="stylesheet" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../ayu.css" disabled><link rel="stylesheet" href="../../dark.css" disabled><link rel="stylesheet" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></head><body class="rustdoc mod"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../tower/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../tower/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Module load</a></h2><div class="sidebar-elems"><section><ul class="block"><li><a href="#reexports">Re-exports</a></li><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li></ul></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Module <a href="../index.html">tower</a>::<wbr><a class="mod" href="#">load</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../../src/tower/load/mod.rs.html#1-89">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Service load measurement</p> |
| <p>This module provides the <a href="trait.Load.html" title="Load"><code>Load</code></a> trait, which allows measuring how loaded a service is. |
| It also provides several wrapper types that measure load in different ways:</p> |
| <ul> |
| <li><a href="struct.Constant.html" title="Constant"><code>Constant</code></a> — Always returns the same constant load value for a service.</li> |
| <li><a href="pending_requests/struct.PendingRequests.html" title="PendingRequests"><code>PendingRequests</code></a> — Measures load by tracking the number of in-flight requests.</li> |
| <li><a href="peak_ewma/struct.PeakEwma.html" title="PeakEwma"><code>PeakEwma</code></a> — Measures load using a moving average of the peak latency for the service.</li> |
| </ul> |
| <p>In general, you will want to use one of these when using the types in <a href="../balance/index.html"><code>tower::balance</code></a> which |
| balance services depending on their load. Which load metric to use depends on your exact |
| use-case, but the ones above should get you quite far!</p> |
| <p>When the <code>discover</code> feature is enabled, wrapper types for <a href="../discover/trait.Discover.html"><code>Discover</code></a> that |
| wrap the discovered services with the given load estimator are also provided.</p> |
| <h2 id="when-does-a-request-complete"><a href="#when-does-a-request-complete">When does a request complete?</a></h2> |
| <p>For many applications, the request life-cycle is relatively simple: when a service responds to |
| a request, that request is done, and the system can forget about it. However, for some |
| applications, the service may respond to the initial request while other parts of the system |
| are still acting on that request. In such an application, the system load must take these |
| requests into account as well, or risk the system underestimating its own load.</p> |
| <p>To support these use-cases, the load estimators in this module are parameterized by the |
| <a href="completion/trait.TrackCompletion.html" title="TrackCompletion"><code>TrackCompletion</code></a> trait, with <a href="completion/struct.CompleteOnResponse.html"><code>CompleteOnResponse</code></a> as the default type. The behavior of |
| <a href="completion/struct.CompleteOnResponse.html"><code>CompleteOnResponse</code></a> is what you would normally expect for a request-response cycle: when the |
| response is produced, the request is considered “finished”, and load goes down. This can be |
| overriden by your own user-defined type to track more complex request completion semantics. See |
| the documentation for <a href="completion/index.html" title="completion"><code>completion</code></a> for more details.</p> |
| <h2 id="examples"><a href="#examples">Examples</a></h2> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tower::util::ServiceExt; |
| <span class="kw">use </span>tower::{load::Load, Service}; |
| <span class="kw">async fn </span>simple_balance<S1, S2, R>( |
| svc1: <span class="kw-2">&mut </span>S1, |
| svc2: <span class="kw-2">&mut </span>S2, |
| request: R |
| ) -> <span class="prelude-ty">Result</span><S1::Response, S1::Error> |
| <span class="kw">where |
| </span>S1: Load + Service<R>, |
| S2: Load<Metric = S1::Metric> + Service<R, Response = S1::Response, Error = S1::Error> |
| { |
| <span class="kw">if </span>svc1.load() < svc2.load() { |
| svc1.ready().<span class="kw">await</span><span class="question-mark">?</span>.call(request).<span class="kw">await |
| </span>} <span class="kw">else </span>{ |
| svc2.ready().<span class="kw">await</span><span class="question-mark">?</span>.call(request).<span class="kw">await |
| </span>} |
| }</code></pre></div> |
| </div></details><h2 id="reexports" class="small-section-header"><a href="#reexports">Re-exports</a></h2><div class="item-table"><div class="item-row"><div class="item-left import-item" id="reexport.CompleteOnResponse"><code>pub use self::completion::<a class="struct" href="completion/struct.CompleteOnResponse.html" title="struct tower::load::completion::CompleteOnResponse">CompleteOnResponse</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.TrackCompletion"><code>pub use self::completion::<a class="trait" href="completion/trait.TrackCompletion.html" title="trait tower::load::completion::TrackCompletion">TrackCompletion</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.PeakEwma"><code>pub use self::peak_ewma::<a class="struct" href="peak_ewma/struct.PeakEwma.html" title="struct tower::load::peak_ewma::PeakEwma">PeakEwma</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.PendingRequests"><code>pub use self::pending_requests::<a class="struct" href="pending_requests/struct.PendingRequests.html" title="struct tower::load::pending_requests::PendingRequests">PendingRequests</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.PeakEwmaDiscover"><code>pub use self::peak_ewma::<a class="struct" href="peak_ewma/struct.PeakEwmaDiscover.html" title="struct tower::load::peak_ewma::PeakEwmaDiscover">PeakEwmaDiscover</a>;</code></div></div><div class="item-row"><div class="item-left import-item" id="reexport.PendingRequestsDiscover"><code>pub use self::pending_requests::<a class="struct" href="pending_requests/struct.PendingRequestsDiscover.html" title="struct tower::load::pending_requests::PendingRequestsDiscover">PendingRequestsDiscover</a>;</code></div></div></div><h2 id="modules" class="small-section-header"><a href="#modules">Modules</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="mod" href="completion/index.html" title="tower::load::completion mod">completion</a></div><div class="item-right docblock-short">Application-specific request completion semantics.</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="peak_ewma/index.html" title="tower::load::peak_ewma mod">peak_ewma</a></div><div class="item-right docblock-short">A <code>Load</code> implementation that measures load using the PeakEWMA response latency.</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="pending_requests/index.html" title="tower::load::pending_requests mod">pending_requests</a></div><div class="item-right docblock-short">A <a href="trait.Load.html" title="Load"><code>Load</code></a> implementation that measures load using the number of in-flight requests.</div></div></div><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Constant.html" title="tower::load::Constant struct">Constant</a></div><div class="item-right docblock-short">Wraps a type so that it implements <a href="trait.Load.html" title="Load"><code>Load</code></a> and returns a constant load metric.</div></div></div><h2 id="traits" class="small-section-header"><a href="#traits">Traits</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.Load.html" title="tower::load::Load trait">Load</a></div><div class="item-right docblock-short">Types that implement this trait can give an estimate of how loaded they are.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="tower" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html> |