pub trait Chooser: DynClone + Debug + Sync + Send {
    fn choose(
        &self,
        ips: &[IpAddrWithPort],
        opts: ChooseOptions<'_>
    ) -> ChosenResults; fn feedback(&self, feedback: ChooserFeedback<'_>); fn async_choose<'a>(
        &'a self,
        ips: &'a [IpAddrWithPort],
        opts: ChooseOptions<'a>
    ) -> BoxFuture<'a, ChosenResults> { ... } fn async_feedback<'a>(
        &'a self,
        feedback: ChooserFeedback<'a>
    ) -> BoxFuture<'a, ()> { ... } }
Expand description

选择 IP 地址接口

还提供了对选择结果的反馈接口,用以修正自身选择逻辑,优化选择结果

同时提供阻塞接口和异步接口,异步接口则需要启用 async 功能

Required Methods§

选择 IP 地址列表

反馈选择的 IP 地址列表的结果

Provided Methods§

Available on crate feature async only.

异步选择 IP 地址列表

该方法的异步版本为 Self::async_choose

Examples found in repository?
src/client/chooser/shuffled.rs (line 43)
41
42
43
44
45
46
47
    fn async_choose<'a>(&'a self, ips: &'a [IpAddrWithPort], opts: ChooseOptions<'a>) -> BoxFuture<'a, ChosenResults> {
        Box::pin(async move {
            let mut ips = self.chooser.async_choose(ips, opts).await;
            ips.shuffle(&mut thread_rng());
            ips
        })
    }
More examples
Hide additional examples
src/client/chooser/never_empty_handed.rs (line 63)
61
62
63
64
65
66
67
68
69
70
    fn async_choose<'a>(&'a self, ips: &'a [IpAddrWithPort], opts: ChooseOptions<'a>) -> BoxFuture<'a, ChosenResults> {
        Box::pin(async move {
            let chosen = self.inner_chooser.async_choose(ips, opts).await;
            if chosen.is_empty() {
                self.random_choose(ips).into()
            } else {
                chosen
            }
        })
    }
src/client/call/utils.rs (line 506)
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
    pub(in super::super) async fn async_choose(
        parts: &InnerRequestParts<'_>,
        ips: &[IpAddrWithPort],
        extensions: &mut Extensions,
        retried: &RetriedStatsInfo,
    ) -> Result<Vec<IpAddrWithPort>, TryError> {
        call_to_choose_ips_callbacks(parts, ips, extensions, retried)?;
        let chosen_ips = parts
            .http_client()
            .chooser()
            .async_choose(ips, Default::default())
            .await
            .into_ip_addrs();
        call_ips_chosen_callbacks(parts, ips, &chosen_ips, extensions, retried)?;
        Ok(chosen_ips)
    }
Available on crate feature async only.

异步反馈选择的 IP 地址列表的结果

Examples found in repository?
src/client/chooser/never_empty_handed.rs (line 76)
75
76
77
    fn async_feedback<'a>(&'a self, feedback: ChooserFeedback<'a>) -> BoxFuture<'a, ()> {
        self.inner_chooser.async_feedback(feedback)
    }
More examples
Hide additional examples
src/client/chooser/shuffled.rs (line 58)
57
58
59
    fn async_feedback<'a>(&'a self, feedback: ChooserFeedback<'a>) -> BoxFuture<'a, ()> {
        Box::pin(async move { self.chooser.async_feedback(feedback).await })
    }
src/client/call/try_domain_or_ip_addr.rs (lines 180-186)
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
pub(super) async fn async_try_domain_or_ip_addr(
    domain_or_ip: &DomainOrIpAddr,
    parts: &InnerRequestParts<'_>,
    body: &mut AsyncRequestBody<'_>,
    mut extensions: Extensions,
    retried: &mut RetriedStatsInfo,
) -> Result<AsyncResponse, TryErrorWithExtensions> {
    let (url, resolved_ips) =
        make_url(domain_or_ip, parts, retried).map_err(|err| err.with_extensions(take(&mut extensions)))?;
    let on_uploading_progress = |info: TransferProgressInfo<'_>| parts.call_uploading_progress_callbacks(parts, info);
    let on_receive_response_status =
        |status_code: StatusCode| parts.call_receive_response_status_callbacks(parts, status_code);
    let on_receive_response_header =
        |name: &HeaderName, value: &HeaderValue| parts.call_receive_response_header_callbacks(parts, name, value);
    let mut http_request: HttpRequest<AsyncRequestBody> =
        make_request(url, parts, body.into(), extensions, &resolved_ips);
    reset_async_request_body(http_request.body_mut(), retried)
        .await
        .map_err(|err| err.with_request(&mut http_request))?;
    call_before_request_signed_callbacks(parts, &mut http_request, retried)
        .map_err(|err| err.with_request(&mut http_request))?;
    sign_async_request(&mut http_request, parts.authorization(), retried)
        .await
        .map_err(|err| err.with_request(&mut http_request))?;
    call_after_request_signed_callbacks(parts, &mut http_request, retried)
        .map_err(|err| err.with_request(&mut http_request))?;
    reset_async_request_body(http_request.body_mut(), retried)
        .await
        .map_err(|err| err.with_request(&mut http_request))?;

    if parts.uploading_progress_callbacks_count() > 0 {
        *http_request.on_uploading_progress_mut() = Some(OnProgressCallback::reference(&on_uploading_progress));
    }
    if parts.receive_response_status_callbacks_count() > 0 {
        *http_request.on_receive_response_status_mut() =
            Some(OnStatusCodeCallback::reference(&on_receive_response_status));
    }
    if parts.receive_response_header_callbacks_count() > 0 {
        *http_request.on_receive_response_header_mut() = Some(OnHeaderCallback::reference(&on_receive_response_header));
    }

    let (extracted_ips, domain) = extract_ips_from(domain_or_ip);
    match async_send_http_request(&mut http_request, parts, retried).await {
        Ok(response) => {
            if !extracted_ips.is_empty() {
                parts
                    .http_client()
                    .chooser()
                    .async_feedback(make_positive_feedback(
                        &extracted_ips,
                        domain,
                        &mut http_request,
                        response.metrics(),
                        retried,
                    ))
                    .await;
            }
            Ok(response)
        }
        Err(err) => {
            if !extracted_ips.is_empty() {
                parts
                    .http_client()
                    .chooser()
                    .async_feedback(make_negative_feedback(
                        &extracted_ips,
                        domain,
                        &mut http_request,
                        &err,
                        retried,
                    ))
                    .await;
            }
            Err(err.with_request(&mut http_request))
        }
    }
}

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

Implementations on Foreign Types§

Available on crate feature async only.
Available on crate feature async only.
Available on crate feature async only.
Available on crate feature async only.
Available on crate feature async only.
Available on crate feature async only.
Available on crate feature async only.
Available on crate feature async only.
Available on crate feature async only.
Available on crate feature async only.

Implementors§