Files
GopherGate/target/doc/tower/retry/trait.Policy.html
2026-02-26 12:00:21 -05:00

79 lines
12 KiB
HTML

<!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="A “retry policy” to classify if a request should be retried."><title>Policy in tower::retry - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="tower" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../../static.files/storage-e2aeef58.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-044be391.svg"></head><body class="rustdoc trait"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">Policy</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../tower/index.html">tower</a><span class="version">0.5.3</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Policy</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#example" title="Example">Example</a></li></ul><h3><a href="#required-associated-types">Required Associated Types</a></h3><ul class="block"><li><a href="#associatedtype.Future" title="Future">Future</a></li></ul><h3><a href="#required-methods">Required Methods</a></h3><ul class="block"><li><a href="#tymethod.clone_request" title="clone_request">clone_request</a></li><li><a href="#tymethod.retry" title="retry">retry</a></li></ul><h3><a href="#implementors">Implementors</a></h3></section><div id="rustdoc-modnav"><h2><a href="index.html">In tower::<wbr>retry</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="../index.html">tower</a>::<wbr><a href="index.html">retry</a></div><h1>Trait <span class="trait">Policy</span>&nbsp;<button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../../src/tower/retry/policy.rs.html#46-90">Source</a> </span></div><pre class="rust item-decl"><code>pub trait Policy&lt;Req, Res, E&gt; {
type <a href="#associatedtype.Future" class="associatedtype">Future</a>: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a>&lt;Output = <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.unit.html">()</a>&gt;;
// Required methods
fn <a href="#tymethod.retry" class="fn">retry</a>(
&amp;mut self,
req: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut Req</a>,
result: &amp;mut <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Res, E&gt;,
) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;Self::<a class="associatedtype" href="trait.Policy.html#associatedtype.Future" title="type tower::retry::Policy::Future">Future</a>&gt;;
<span class="item-spacer"></span> fn <a href="#tymethod.clone_request" class="fn">clone_request</a>(&amp;mut self, req: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;Req</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;Req&gt;;
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A “retry policy” to classify if a request should be retried.</p>
<h2 id="example"><a class="doc-anchor" href="#example">§</a>Example</h2>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tower::retry::Policy;
<span class="kw">use </span>std::future;
<span class="kw">type </span>Req = String;
<span class="kw">type </span>Res = String;
<span class="kw">struct </span>Attempts(usize);
<span class="kw">impl</span>&lt;E&gt; Policy&lt;Req, Res, E&gt; <span class="kw">for </span>Attempts {
<span class="kw">type </span>Future = future::Ready&lt;()&gt;;
<span class="kw">fn </span>retry(<span class="kw-2">&amp;mut </span><span class="self">self</span>, req: <span class="kw-2">&amp;mut </span>Req, result: <span class="kw-2">&amp;mut </span><span class="prelude-ty">Result</span>&lt;Res, E&gt;) -&gt; <span class="prelude-ty">Option</span>&lt;<span class="self">Self</span>::Future&gt; {
<span class="kw">match </span>result {
<span class="prelude-val">Ok</span>(<span class="kw">_</span>) =&gt; {
<span class="comment">// Treat all `Response`s as success,
// so don't retry...
</span><span class="prelude-val">None
</span>},
<span class="prelude-val">Err</span>(<span class="kw">_</span>) =&gt; {
<span class="comment">// Treat all errors as failures...
// But we limit the number of attempts...
</span><span class="kw">if </span><span class="self">self</span>.<span class="number">0 </span>&gt; <span class="number">0 </span>{
<span class="comment">// Try again!
</span><span class="self">self</span>.<span class="number">0 </span>-= <span class="number">1</span>;
<span class="prelude-val">Some</span>(future::ready(()))
} <span class="kw">else </span>{
<span class="comment">// Used all our attempts, no retry...
</span><span class="prelude-val">None
</span>}
}
}
}
<span class="kw">fn </span>clone_request(<span class="kw-2">&amp;mut </span><span class="self">self</span>, req: <span class="kw-2">&amp;</span>Req) -&gt; <span class="prelude-ty">Option</span>&lt;Req&gt; {
<span class="prelude-val">Some</span>(req.clone())
}
}</code></pre></div></div></details><h2 id="required-associated-types" class="section-header">Required Associated Types<a href="#required-associated-types" class="anchor">§</a></h2><div class="methods"><details class="toggle" open><summary><section id="associatedtype.Future" class="method"><a class="src rightside" href="../../src/tower/retry/policy.rs.html#48">Source</a><h4 class="code-header">type <a href="#associatedtype.Future" class="associatedtype">Future</a>: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a>&lt;Output = <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.unit.html">()</a>&gt;</h4></section></summary><div class="docblock"><p>The <a href="https://doc.rust-lang.org/1.93.1/core/future/future/trait.Future.html" title="trait core::future::future::Future"><code>Future</code></a> type returned by <a href="trait.Policy.html#tymethod.retry" title="method tower::retry::Policy::retry"><code>Policy::retry</code></a>.</p>
</div></details></div><h2 id="required-methods" class="section-header">Required Methods<a href="#required-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="tymethod.retry" class="method"><a class="src rightside" href="../../src/tower/retry/policy.rs.html#83">Source</a><h4 class="code-header">fn <a href="#tymethod.retry" class="fn">retry</a>(
&amp;mut self,
req: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut Req</a>,
result: &amp;mut <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Res, E&gt;,
) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;Self::<a class="associatedtype" href="trait.Policy.html#associatedtype.Future" title="type tower::retry::Policy::Future">Future</a>&gt;</h4></section></summary><div class="docblock"><p>Check the policy if a certain request should be retried.</p>
<p>This method is passed a reference to the original request, and either
the <a href="../trait.Service.html#associatedtype.Response" title="associated type tower::Service::Response"><code>Service::Response</code></a> or <a href="../trait.Service.html#associatedtype.Error" title="associated type tower::Service::Error"><code>Service::Error</code></a> from the inner service.</p>
<p>If the request should <strong>not</strong> be retried, return <code>None</code>.</p>
<p>If the request <em>should</em> be retried, return <code>Some</code> future that will delay
the next retry of the request. This can be used to sleep for a certain
duration, to wait for some external condition to be met before retrying,
or resolve right away, if the request should be retried immediately.</p>
<h6 id="mutating-requests"><a class="doc-anchor" href="#mutating-requests">§</a>Mutating Requests</h6>
<p>The policy MAY chose to mutate the <code>req</code>: if the request is mutated, the
mutated request will be sent to the inner service in the next retry.
This can be helpful for use cases like tracking the retry count in a
header.</p>
<h6 id="mutating-results"><a class="doc-anchor" href="#mutating-results">§</a>Mutating Results</h6>
<p>The policy MAY chose to mutate the result. This enables the retry
policy to convert a failure into a success and vice versa. For example,
if the policy is used to poll while waiting for a state change, the
policy can switch the result to emit a specific error when retries are
exhausted.</p>
<p>The policy can also record metadata on the request to include
information about the number of retries required or to record that a
failure failed after exhausting all retries.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="tymethod.clone_request" class="method"><a class="src rightside" href="../../src/tower/retry/policy.rs.html#89">Source</a><h4 class="code-header">fn <a href="#tymethod.clone_request" class="fn">clone_request</a>(&amp;mut self, req: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;Req</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;Req&gt;</h4></section></summary><div class="docblock"><p>Tries to clone a request before being passed to the inner service.</p>
<p>If the request cannot be cloned, return <a href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html#variant.None" title="variant core::option::Option::None"><code>None</code></a>. Moreover, the retry
function will not be called if the <a href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html#variant.None" title="variant core::option::Option::None"><code>None</code></a> is returned.</p>
</div></details></div><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"></div><script src="../../trait.impl/tower/retry/policy/trait.Policy.js" async></script></section></div></main></body></html>