Skip to content

refactor(core): decouple from specific runtime - #1097

Closed
0x676e67 wants to merge 5 commits into
mainfrom
rt
Closed

0x676e67 wants to merge 5 commits into
mainfrom
rt

Conversation

@0x676e67

Copy link
Copy Markdown
Owner

No description provided.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors the core HTTP client to improve its modularity and reduce coupling to specific runtime implementations. By introducing a TcpConnector trait and abstracting timer functionalities, the changes enable greater flexibility in how TCP connections are established and managed, and how asynchronous operations are timed. This leads to a more maintainable and adaptable codebase, particularly for supporting different asynchronous runtimes or custom network configurations.

Highlights

  • Runtime Decoupling: Introduced a TcpConnector trait and a TokioTcpConnector implementation, allowing the core HTTP client to be decoupled from a specific runtime (Tokio) for TCP connections. This enhances flexibility and testability.
  • Centralized TCP Configuration: Created a new config.rs module to house SocketBindOptions and TcpOptions, centralizing and standardizing TCP socket configuration, including interface binding, local address binding, and keepalive settings.
  • HttpConnector Refactoring: Refactored HttpConnector to utilize the new TcpConnector trait and TcpOptions for managing connection parameters, simplifying its internal logic and making it more generic.
  • Timer Abstraction: Simplified the rt (runtime) module by removing the ArcTimer struct and making the Time enum directly implement the Timer trait. This streamlines timer management and reduces boilerplate.
  • Configuration Updates: Updated ClientBuilder, connection pooling (pool), and timeout layers (ResponseBodyTimeoutLayer, TimeoutBody) to integrate with the new SocketBindOptions and the abstracted Time and Timer mechanisms.
Changelog
  • src/client/conn.rs
    • Added new module imports for config and tcp.
    • Updated the HttpConnector type alias to include TokioTcpConnector.
  • src/client/conn/config.rs
    • Added SocketBindOptions struct for configuring socket binding to interfaces or local addresses.
    • Added TcpOptions struct to consolidate various TCP connection settings, including keepalive and buffer sizes.
    • Implemented TcpKeepaliveOptions to convert into socket2::TcpKeepalive.
  • src/client/conn/connector.rs
    • Updated imports to reflect the new HttpConnect trait and TokioTcpConnector.
    • Modified HttpConnector instantiation to use TokioTcpConnector::new().
    • Adapted the application of TCP options to use the new set_interface and set_local_addresses methods on HttpConnect.
  • src/client/conn/http.rs
    • Introduced the HttpConnect trait to define a generic interface for HTTP connection services.
    • Refactored HttpConnector to be generic over a TcpConnector and use TcpOptions for configuration.
    • Removed the internal Config and TcpConnectOptions structs, delegating their responsibilities to the new config and tcp modules.
    • Updated the HttpConnecting future to work with the new TcpConnector abstraction.
  • src/client/conn/tcp.rs
    • Added the TcpConnector trait, defining an interface for building TCP connections.
    • Implemented ConnectingTcp and ConnectingTcpRemote structs to manage Happy Eyeballs connection logic.
    • Defined ConnectError for standardized error reporting during TCP connection attempts.
    • Refactored the connect function to use the new TcpConnector trait for creating streams.
  • src/client/conn/tcp/tokio.rs
    • Added TokioTcpConnector struct, implementing the TcpConnector trait using tokio::net::TcpSocket and tokio::time::sleep.
  • src/client/core/conn/http2.rs
    • Removed ArcTimer import.
    • Updated the timer field in Builder to use Arc<dyn Timer + Send + Sync> directly.
    • Modified set_timer to use Arc::new(timer) instead of ArcTimer::new(timer).
  • src/client/core/proto/http2/ping.rs
    • Added Timer import to the module.
  • src/client/core/rt.rs
    • Removed ArcTimer from public exports.
  • src/client/core/rt/timer.rs
    • Removed the ArcTimer struct.
    • Modified the Time enum to directly implement the Timer trait, simplifying timer usage.
  • src/client/core/rt/tokio.rs
    • Added #[inline] attributes to poll and reset methods for TokioSleep.
  • src/client/http.rs
    • Replaced TcpConnectOptions with SocketBindOptions in Config.
    • Updated ClientBuilder to apply SocketBindOptions and use the new HttpConnect methods.
    • Modified ResponseBodyTimeoutLayer instantiation to pass TokioTimer::new().
  • src/client/http/client.rs
    • Replaced Option<ArcTimer> with Time for the pool_timer field in Builder.
    • Updated Builder::new to initialize pool_timer as Time::Empty.
    • Modified set_pool_timer to store Time::Timer(Arc::new(timer)).
  • src/client/http/client/extra.rs
    • Replaced TcpConnectOptions with SocketBindOptions in ConnectExtra.
  • src/client/http/client/pool.rs
    • Replaced ArcTimer with Time for the timer field in PoolInner and IdleTask.
    • Updated Pool::new to accept Time directly.
    • Modified PoolInner::now to call self.timer.now() directly.
    • Adjusted idle task initialization to use self.timer.clone().
  • src/client/layer/config/options.rs
    • Replaced TcpConnectOptions with SocketBindOptions in RequestOptions.
  • src/client/layer/timeout.rs
    • Added Timer and Time imports.
    • Modified ResponseBodyTimeoutLayer::new to accept a Timer and store it as Time.
    • Updated ResponseBodyTimeout to include a timer field.
  • src/client/layer/timeout/body.rs
    • Replaced tokio::time::Sleep with client::core::rt::Sleep and Time for timeout management.
    • Modified TimeoutBody::new to accept a Time instance and use it to create sleep futures.
  • src/client/layer/timeout/future.rs
    • Added Time import.
    • Updated ResponseFuture to include a timer field and pass it to TimeoutBody::new.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a significant refactoring to decouple the client's core from a specific runtime, primarily tokio. This is achieved by introducing a TcpConnector trait to abstract TCP connection logic and a Timer trait for time-related operations. The changes are extensive, touching many files to adapt to these new abstractions. The removal of ArcTimer in favor of a direct Arc<dyn Timer> simplifies the timer logic. Overall, the refactoring appears well-executed and improves the modularity of the codebase. I have one suggestion to improve code clarity.

Comment thread src/client/conn/config.rs
Comment on lines +112 to +121
match local_addr {
Some(IpAddr::V4(a)) => {
self.local_address_ipv4 = Some(a);
}
Some(IpAddr::V6(a)) => {
self.local_address_ipv6 = Some(a);
}
_ => {}
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The first match statement in this function is redundant as its logic is completely handled by the subsequent code block. This can be confusing and should be removed to improve clarity.

Note that this new implementation changes the behavior by clearing the local address for the other IP version, which the previous implementation did not do. If this is intended, it might be worth documenting.

This branch had an error being deployed

1 failed deployment
Windows — 08f92da7 Deployed Mar 19, 2026 by 0x676e67
Android — 08f92da7 Deployed Mar 19, 2026 by 0x676e67
Linux — 08f92da7 Deployed Mar 19, 2026 by 0x676e67
macOS — 08f92da7 Deployed Mar 19, 2026 by 0x676e67
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant