HttpComponents analysis-connection pool implementation

Source: Internet
Author: User

Imagine the call between the comet and Wang:

 

1. The Comet first finds the phone number of Wang weixing,

2. Call Wang's phone number again.

3. After talking over the phone, the star said "miao"

4. wang weixing calls "wang" after receiving "miao ",

When both parties can hear the other party's speech, the telephone line will pass.

 

This process is quite time-consuming, especially when talking over the phone "@#! $ % @ # "Is suspended. after a short period of time, you can find that you have something to say, and then establish connections repeatedly in the above process, the call efficiency is very low. The call process is equivalent to TCP's three-way handshake, and communication is equivalent to the communication information transmitted on it. But in fact, in the early stage, the connection was closed immediately after an Http request was completed. If the requested data is very small and the number of requests is very large, the communication efficiency is very low. How can we improve communication efficiency? In fact, it is very simple. You only need to wait for a while to complete the call after the connection is established to check whether the other party has something to say during this period. If there is something to say, continue communication, otherwise, the connection will be closed after this time. This solution is also reflected in the Http protocol, namely, keep-alive.

 

Back to the topic, Http is the most popular protocol on the internet, webservices, and network-based applications. While increasing Http support, it also strongly promotes Protocol Expansion from browser application limitations. Although java.net provides basic support for http functions such as obtaining resources from the network, it does not meet the requirements of many applications for comprehensive protocol functions and flexibility. For example, the http connection pool mentioned below is a very important function.

 

The Http connection pool uses the persistent connection feature of Http 1.1 KeepAlive. In the TCP protocol, establishing a connection between two machines involves three handshakes, which is time-consuming, especially when a small amount of data is continuously transmitted, if the connection can be reused continuously, a large throughput can be achieved. At the same time, it should be considered that if you can enable multiple socket connections for one server port to transmit information, it can increase the network bandwidth. Use popularSentence bodyIt is to manage the creation, allocation, reuse, and recovery of multiple persistent connections of a route.

 

Figure 1: connPool Inheritance System

I. Basic Structure:

1. ConnPool: Connection Pool interface:

  • Future <E> lease (final T route, final Object state, final FutureCallback <E> callback );

Remove a connection from the connection pool

  • Void release (E entry, boolean reusable );

Release connection

2. AbstractConnPool:

The data structure is as follows:

  • Map <T, RouteSpecificPool <T, C, E> routeToPool maps each route to its connection pool,
  • Set <E> connection Set of leased total pool lending, available list <E> available connection Set of available total pool,
  • Pending list <PoolEntryFuture <E> the pending pool waits for the queue to be connected,
  • Map <T, Integer> maxPerRoute ing table of the maximum number of connections for each route.
  • The maximum number of connections of the int maxTotal pool.

Resource synchronization and lifecycle methods such as shutdown () are performed based on the preceding data structure.

 

Figure 2: routeToPool Structure

 

In the routeToPool, different routes have their own RouteSpecificPool (route-related connection pool), which also has three data structures:

  • Pending queue with the same route waiting for connection
  • The connection queue that can be used by Avaliable.
  • Leased: a set of connections that have been rented by the same route.

 

Ii. Main Operation Analysis

 

Lease connection:

1. First, find the connection pool corresponding to the current route from the routeToPool

2. go to the connection pool to find the idle connection and check whether the connection is closed or timed out. Then, close the connection and find the next idle connection, until the available linked list avaliable is found or traversed.

3. If it is found, remove the entry from the available linked list avaliable, add it to the lease set Leased, and return it.

4. if not, query the maxPerRoute ing table for each route connection to find the maximum maxPerRoute of the current route, and check whether the current route connection count + 1 has exceeded the maximum route limit, if the value exceeds, the connection pool corresponding to the route isAvaliable queueThe oldest connection was closed.

5. Check whether the number of existing connections in the pool corresponding to the current route exceeds the upper limit of maxPerRoute and whether the total number of existing connections is less than the maximum count maxTotal in the total pool;

If the conditions are met, check whether the number of connections in the avaliable queue is greater than or equal to the number of allocable connections in the total pool. If the conditions are met, try to select the earliest join from the avaliable list of available connections in the total pool, the connection is closed in the pool corresponding to the corresponding route;

Finally, create a new connection and add it to the leased set corresponding to the total pool and route. If the connection is created successfully, return.

6. If the five conditions in step 5 are not met, add them to the waiting queue pending and enter the waiting mode.

7. If someone wakes up and then checks for timeout, if there is no timeout, it will jump back to 2.

 

Release return connection:

1. First Return the connection from the total pool

2. If 1 is successful, return it from the connection corresponding to the route

3. Finally, the task in the pending queue waiting to wake up and get the connection continues to get the connection.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.