Skip to content

[router] Add builder pattern for RouterConfig with zero duplication#12030

Merged
slin1237 merged 2 commits intomainfrom
routerconfig-builder
Oct 23, 2025
Merged

[router] Add builder pattern for RouterConfig with zero duplication#12030
slin1237 merged 2 commits intomainfrom
routerconfig-builder

Conversation

@slin1237
Copy link
Collaborator

Summary

Implements a fluent builder pattern for RouterConfig that eliminates code duplication and simplifies config construction across the codebase.

Key Changes

1. Zero-Duplication Builder

Builder now wraps RouterConfig directly instead of duplicating all 70+ fields:

#[derive(Debug, Clone, Default)]
pub struct RouterConfigBuilder {
    config: RouterConfig,  // Single source of truth
}

2. Setters

  • Added .maybe_*() methods for optional fields (eliminates if-let chains)
  • Added conditional boolean setters (eliminates if statements)

3. Replaced 32 Manual Constructions

Replaced verbose manual config construction with builder pattern across:

  • All config tests (9 in types.rs)
  • lib.rs and main.rs
  • All test files (20 constructions)

Checklist

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @slin1237, 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 configuration management for the router by implementing a fluent builder pattern for the RouterConfig struct. This change aims to improve code clarity, reduce boilerplate, and enhance maintainability by centralizing configuration logic and simplifying how RouterConfig instances are created and modified throughout the application.

Highlights

  • New Builder Pattern for RouterConfig: Introduced a fluent builder pattern for RouterConfig to streamline its construction and configuration.
  • Zero-Duplication Implementation: The RouterConfigBuilder now directly wraps the RouterConfig struct, eliminating the need to duplicate its 70+ fields and ensuring automatic synchronization.
  • Enhanced Setter Methods: Added convenient .maybe_*() methods for optional fields to avoid verbose if-let chains, and conditional boolean setters to simplify flag management.
  • Widespread Adoption: Replaced 32 instances of manual RouterConfig construction across the codebase, including config tests, lib.rs, main.rs, and various test files, with the new builder pattern.
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.

Copy link
Contributor

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

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 introduces a well-designed builder pattern for RouterConfig, significantly improving configuration ergonomics and maintainability across the codebase. By wrapping the RouterConfig struct, the builder avoids field duplication and provides a fluent, expressive API. The inclusion of maybe_* setters for optional fields and conditional boolean setters is a thoughtful touch that simplifies config construction. The refactoring has been applied consistently, cleaning up numerous manual struct instantiations in tests and application logic. I have one suggestion to further simplify the builder's usage in a test case. Overall, this is an excellent contribution to the project's code quality.

Comment on lines +165 to 183
let config = match mode {
RoutingMode::PrefillDecode {
prefill_urls,
decode_urls,
..
} => RouterConfig::builder()
.prefill_decode_mode(prefill_urls, decode_urls)
.policy(policy)
.host("127.0.0.1")
.port(3001)
.max_payload_size(1024 * 1024)
.request_timeout_secs(60)
.worker_startup_timeout_secs(10)
.worker_startup_check_interval_secs(1)
.max_concurrent_requests(64)
.queue_timeout_secs(60)
.build_unchecked(),
_ => panic!("Expected PrefillDecode mode"),
};
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The match statement here is unnecessarily complex. Since you already have the mode and policy variables from the test_cases loop, you can directly use the .mode() and .policy() setters on the builder. This avoids deconstructing mode only to reconstruct it again, making the code simpler and more direct.

            let config = RouterConfig::builder()
                .mode(mode)
                .policy(policy)
                .host("127.0.0.1")
                .port(3001)
                .max_payload_size(1024 * 1024)
                .request_timeout_secs(60)
                .worker_startup_timeout_secs(10)
                .worker_startup_check_interval_secs(1)
                .max_concurrent_requests(64)
                .queue_timeout_secs(60)
                .build_unchecked();

@slin1237 slin1237 merged commit 6d6e24b into main Oct 23, 2025
39 of 40 checks passed
@slin1237 slin1237 deleted the routerconfig-builder branch October 23, 2025 23:46
@slin1237 slin1237 added enhancement New feature or request router labels Oct 23, 2025
@slin1237 slin1237 restored the routerconfig-builder branch October 24, 2025 05:00
@slin1237 slin1237 deleted the routerconfig-builder branch October 24, 2025 05:01
@slin1237 slin1237 mentioned this pull request Oct 27, 2025
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request router run-ci

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments