cloudwego / volo

Rust RPC framework with high-performance and strong-extensibility for building micro-services.
https://crates.io/crates/volo
Apache License 2.0
2.13k stars 166 forks source link

chore(volo-http): refactor client configs #423

Closed wfly1998 closed 1 month ago

wfly1998 commented 2 months ago

Motivation

The previous config of HTTP client was confusing, some were used for building client, some were used for transporting, and some were generic functions.

Solution

Old implementation

The previous config in ClientContext:

pub struct Config {
    pub caller_name: CallerName,
    pub callee_name: CalleeName,
    pub stat_enable: bool,
    pub fail_on_error_status: bool,
    #[cfg(feature = "__tls")]
    pub disable_tls: bool,
}

New implementation

Config for builder:

struct BuilderConfig {
    caller_name_mode: CallerName,
    callee_name_mode: CalleeName,
    timeout: Option<Duration>,
    stat_enable: bool,
    fail_on_error_status: bool,
    #[cfg(feature = "__tls")]
    disable_tls: bool,
}

This config is used in ClientBuilder and it will split into four parts for four purposes.

ClientContext

Config in ClientContext:

struct Config {
    timeout: Option<Duration>,
}

This config is used for request, and it has one per request.

ClientInner

struct ClientInner {
    /* ...... */
    callee_name_mode: CalleeName,
    /* ...... */
}

The caller_name_mode is used only in ClientBuilder::build, so we do not save it, and only the callee_name_mode should be saved. Additionally, this config should have one per client.

MetaService

struct MetaServiceConfig {
    default_timeout: Option<Duration>,
    fail_on_error_status: bool,
}

There should be a default timeout, it is better to save it in the MetaService, and the fail_on_error_status should also be in here.

This config should have one per client. Note that the default timeout can be overridden by the timeout in ClientContext.

ClientTransport

struct ClientTransportConfig {
    stat_enable: bool,
    #[cfg(feature = "__tls")]
    disable_tls: bool,
}

TLS can be force disabled for each client, and TLS related configs can only be used during transport, so it should be saved in ClientTransport.

And the stat of Volo-HTTP only records the cost of transporting, so it should also be saved here.

Others

Additionally, this PR supports timeout for request in MetaService, it can be set by ClientBuilder::set_request_timeout (for the whole client) or RequestBuilder::set_request_timeout (only for the request).