Skip to content

Implement HTTP connection pooling for WxPayServiceApacheHttpImpl #3648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 24, 2025

This PR implements HTTP connection pooling for WxPayServiceApacheHttpImpl to address performance issues caused by creating new HttpClient instances for each request.

Problem

The current implementation creates a new HttpClient for every HTTP request:

HttpClientBuilder httpClientBuilder = this.createHttpClientBuilder(useKey);
try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
    // Make request and close client
}

This approach causes:

  • Unnecessary overhead from frequent connection establishment/teardown
  • Poor performance in high-concurrency scenarios
  • Inefficient resource utilization

Solution

Implemented connection pooling using Apache HttpClient's PoolingHttpClientConnectionManager:

Key Changes

  1. Added connection pool configuration to WxPayConfig:

    • maxConnTotal (default: 20) - Maximum total connections
    • maxConnPerRoute (default: 10) - Maximum connections per route
    • Separate pooled clients for SSL and non-SSL requests
  2. Enhanced WxPayConfig with pooled client initialization:

    public CloseableHttpClient initHttpClient() throws WxPayException
    public CloseableHttpClient initSslHttpClient() throws WxPayException
  3. Modified request methods to use pooled clients:

    CloseableHttpClient httpClient = this.createHttpClient(useKey);
    // Reuse pooled client instead of creating new one

Features

  • Backward Compatible: Zero breaking changes, existing code works without modification
  • SSL Support: Separate connection pool for SSL requests with proper certificate handling
  • Proxy Support: Maintains existing proxy configuration functionality
  • Customizable: Supports existing HttpClientBuilderCustomizer interface
  • Configurable: Adjustable pool size parameters

Performance Benefits

  • Eliminates per-request HttpClient creation overhead
  • Enables HTTP connection reuse and keep-alive
  • Better resource management for high-concurrency applications
  • Improved scalability for payment processing workflows

Usage

Connection pooling is automatically enabled with sensible defaults:

WxPayConfig config = new WxPayConfig();
config.setMaxConnTotal(50);      // Optional: customize max connections
config.setMaxConnPerRoute(20);   // Optional: customize per-route limit

WxPayServiceApacheHttpImpl payService = new WxPayServiceApacheHttpImpl();
payService.setConfig(config);
// All requests now use connection pooling automatically

Fixes #3640.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits July 24, 2025 07:53
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
…tation

Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] WxPayServiceApacheHttpImpl.java中使用HttpClient发送http请求时可以调整为使用连接池的形式吗? Implement HTTP connection pooling for WxPayServiceApacheHttpImpl Jul 24, 2025
@Copilot Copilot AI requested a review from binarywang July 24, 2025 08:05
Copilot finished work on behalf of binarywang July 24, 2025 08:05
@binarywang binarywang marked this pull request as ready for review July 30, 2025 01:17
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.

WxPayServiceApacheHttpImpl.java中使用HttpClient发送http请求时可以调整为使用连接池的形式吗?
2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy