Skip to content

Conversation

@kevinlin09
Copy link
Collaborator

@kevinlin09 kevinlin09 commented Jan 25, 2026

Description

[Describe what this PR does and why]

Related Issue: Fixes #[issue_number] or Relates to #[issue_number]

Security Considerations: [Check if API keys or sensitive credentials are exposed in code/logs]

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Refactoring

Component(s) Affected

  • Model
  • Application
  • Common
  • Documentation
  • Tests
  • CI/CD

Checklist

  • Pre-commit hooks pass
  • Tests pass locally
  • Documentation updated (if needed)
  • Ready for review

Testing

[How to test these changes]

Additional Notes

[Optional: any other context]

@gemini-code-assist
Copy link

Summary of Changes

Hello @kevinlin09, 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 introduces a significant performance enhancement by implementing HTTP connection pooling for both synchronous and asynchronous operations within the SDK. By centralizing the management of HTTP client sessions, the changes aim to reduce the overhead of establishing new connections for every request, leading to lower latency and improved resource utilization. The new API provides flexible control over connection pool settings, ensuring that users can optimize network interactions according to their specific application needs, while maintaining backward compatibility for existing codebases.

Highlights

  • HTTP Connection Pooling: Introduced robust HTTP connection pooling for both synchronous (using requests) and asynchronous (using aiohttp) requests to significantly reduce latency and improve performance by reusing existing connections.
  • Dedicated Session Managers: Added new singleton classes, SessionManager and AioSessionManager, to centrally manage and configure HTTP client sessions, ensuring thread-safe and efficient connection handling.
  • Public API for Control: Exposed new functions in the main dashscope module (enable_http_connection_pool, disable_http_connection_pool, reset_http_connection_pool, configure_http_connection_pool and their aio_ counterparts) allowing users to easily control and customize connection pooling behavior.
  • Flexible Session Prioritization: Modified the HttpRequest class to prioritize external requests.Session or aiohttp.ClientSession objects provided by the user, falling back to the global managed session, and finally to a temporary session if no pooling is enabled.
  • Comprehensive Testing: Included extensive unit tests for both synchronous and asynchronous connection pooling mechanisms, covering configuration, lifecycle management, and thread/async safety.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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.

@kevinlin09 kevinlin09 merged commit 9f529ec into dashscope:main Jan 25, 2026
2 checks passed
Copy link

@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 valuable feature for reusing HTTP connections for both synchronous and asynchronous requests, which should significantly improve performance by reducing latency. The implementation is well-structured, introducing SessionManager and AioSessionManager to handle connection pooling, and is accompanied by a comprehensive set of unit tests.

The changes are well-designed, maintaining backward compatibility while allowing users to opt-in to the new connection pooling feature. The public API is clear and provides good control over the pool's behavior.

I've identified one high-severity bug in the synchronous SessionManager where calling enable() with new parameters on an already-enabled pool does not apply the new configuration. I've provided a detailed comment with a suggested fix for this issue.

Overall, this is a great contribution. Once the identified issue is addressed, this will be a solid addition to the library.

Comment on lines +124 to +170
def enable(
self,
pool_connections: Optional[int] = None,
pool_maxsize: Optional[int] = None,
max_retries: Optional[int] = None,
pool_block: Optional[bool] = None,
):
"""
启用连接复用

Args:
pool_connections: 连接池大小,默认 10
pool_maxsize: 最大连接数,默认 20
max_retries: 重试次数,默认 3
pool_block: 连接池满时是否阻塞,默认 False

Examples:
# 使用默认配置
enable()

# 使用命名参数
enable(pool_connections=50, pool_maxsize=100)
"""
with self._session_lock:
# 使用命名参数更新配置
if pool_connections is not None:
self._config.pool_connections = pool_connections
if pool_maxsize is not None:
self._config.pool_maxsize = pool_maxsize
if max_retries is not None:
self._config.max_retries = max_retries
if pool_block is not None:
self._config.pool_block = pool_block

# 参数验证
if self._config.pool_maxsize < self._config.pool_connections:
raise ValueError(
f"pool_maxsize ({self._config.pool_maxsize}) 必须 >= "
f"pool_connections ({self._config.pool_connections})",
)

self._enabled = True
self._ensure_session()
logger.info(
"HTTP connection pool enabled with config: %s",
self._config,
)

Choose a reason for hiding this comment

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

high

The current implementation of the enable method has a bug. If enable is called with new configuration parameters after the connection pool is already enabled, the new configuration is not applied to the active session. The session is only created once in _ensure_session if self._session is None.

This is inconsistent with the behavior of configure and the AioSessionManager.enable method, which both correctly recreate the session to apply new settings.

To fix this, the enable method should check if the configuration has changed and, if so, close the existing session so that a new one with the updated configuration can be created. This ensures that dynamically changing the pool settings via enable works as expected.

    def enable(
        self,
        pool_connections: Optional[int] = None,
        pool_maxsize: Optional[int] = None,
        max_retries: Optional[int] = None,
        pool_block: Optional[bool] = None,
    ):
        """
        启用连接复用

        Args:
            pool_connections: 连接池大小,默认 10
            pool_maxsize: 最大连接数,默认 20
            max_retries: 重试次数,默认 3
            pool_block: 连接池满时是否阻塞,默认 False

        Examples:
            # 使用默认配置
            enable()

            # 使用命名参数
            enable(pool_connections=50, pool_maxsize=100)
        """
        with self._session_lock:
            config_changed = any(
                p is not None
                for p in [pool_connections, pool_maxsize, max_retries, pool_block]
            )

            if config_changed:
                # 使用命名参数更新配置
                if pool_connections is not None:
                    self._config.pool_connections = pool_connections
                if pool_maxsize is not None:
                    self._config.pool_maxsize = pool_maxsize
                if max_retries is not None:
                    self._config.max_retries = max_retries
                if pool_block is not None:
                    self._config.pool_block = pool_block

                # 参数验证
                if self._config.pool_maxsize < self._config.pool_connections:
                    raise ValueError(
                        f"pool_maxsize ({self._config.pool_maxsize}) 必须 >= "
                        f"pool_connections ({self._config.pool_connections})",
                    )

                # If already enabled and config changed, recreate session to apply new config.
                if self._enabled and self._session:
                    try:
                        self._session.close()
                    except Exception as e:
                        logger.warning("Error closing session during re-enable: %s", e)
                    finally:
                        self._session = None

            self._enabled = True
            self._ensure_session()
            logger.info(
                "HTTP connection pool enabled with config: %s",
                self._config,
            )

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