Esframework (10) -- TCP Connection Pool

Source: Internet
Author: User

Any database with a "pool", such as the database connection pool, Object pool, and buffer pool (you can see Ibuffpool To avoid repeated Resource Creation. / The cost of destruction. What resource objects need to be created with a "pool? These resource objects generally meet the following characteristics:
(1) The application must be created repeatedly. / Destroy.
(2) Create / Large destruction overhead
(3) The number of resource objects required for a given time point in an application is relatively large.
(4) The resource object is preferably stateless ( Stateless ) To facilitate direct reuse.

As (Review) Forward all functional service requests As The FS One of the groups (see ...), Then FS Send the request to the corresponding function plug-in for processing. So As And FS In what way does communication between them take place? Optional methods: . Netremoting , WebService , TCP/UDP . Based on efficiency and accuracy, WebService And UDP Is not suitable. So TCP And . Netremoting Which one should I choose? We know . Netremoting The underlying layer is also based on TCP Or HTTP Protocol, in order to simulate the local method call method, . Netremoting A lot of conversion operations (stack frame "=" Message) are also performed, resulting in some overhead, and direct use TCP This can be avoided, and As And FS The format of messages between them is compatible (basically using the same message header, which is enough). That is to say, a message is sent from the client and can be directly sent without any conversion. FS Function plug-in processing (encryption, compression excluded ).

As you think,EsframeworkThe recommended method isAsAndFSDirectly through the lower-LayerTCPCommunication. To avoidTCPOverhead of continuous connection establishment and destruction,AsAndFSBefore communication, you can establishTCPConnection Pool. This article focuses onTCPThe Principle and Implementation of the connection pool.

TCPWhat is stored in the connection pool isTCPConnection-that isNetworkstreamObject.TCP"Rent"One connection, and return it after use"Giveback"To the connection pool.

Public   Class Tcpstreampool: itcpstreampool, itcppool

From the above definitionWe can see thatTcpstreampoolInherit from two interfaces:ItcppoolAndItcpstreampool. First lookItcpstreampoolDefinition:

1 ///   <Summary>
2 /// Itcpstreampool TCP connection pool is used to manage a large number of TCP connections
3 /// Author: Zhu Wei sky.zhuwei@163.com
4 /// Sky 2005.02.24
5 ///   </Summary>
6 Public   Interface Itcpstreampool
7 {
8 Int Serverid { Get ; Set ;}
9 Int Streamcount { Get ; Set ;} // Expected Total number of connections
10 Int Activeconnectioncount { Get ;} // Actual number of available connections
11 Ipendpoint fsipe { Get ; Set ;} // Feature server's IPE
12 Int Reconnectspan { Get ; Set ;} // Minutes
13 Bool Isactive { Get ;}
14
15 Void Reconnect (); // Manual Reconnection
16 Void Initialize ();
17 Void Disposeconnections (); // Release all connections in the pool. You can use reconnect to establish a new connection.
18 Void Setstreamdamaged ( Int Streamhashcode );
19
20 Networkstream implements tcpstream ();
21 Void Givebacktcpstream ( Int Streamhashcode ); // Return TCP connection rules to the connection pool
22 }

As And each FS There is a connection pool between each function server. Serverid So the connection pool also has Serverid Attribute indicates which connection pool is associated FS Connected. Reconnectspan This attribute indicates that the connection pool must support the reconnection mechanism, that is, when all connections in the connection pool are disconnected (may be FS ), The connection pool should be capable of timed reconnection. FS Until all connections in the pool are reestablished.
If the application runs from the connection pool rent one connection, then the connection is disconnected during use, the application should call the setstreamdamaged method of the connection pool to notify the connection pool that the connection is unavailable. effectcpstream method and givebacktcpstream the method is the most common leasing method. / returns the connection..
note, the streamhashcode parameter is used in many methods, it is networkstream Object hashcode , in the system, each networkstream Object hashcode is different, in addition, its hashcode in networkstream the entire lifecycle of an object remains unchanged, therefore, you can use hashcode to uniquely identify each connection.

It seems that,ItcpstreampoolThe interface already reflects everything in a connection pool. Yes. SoItcppoolWhat role does the interface play? Now let's take a lookItcppoolFormat:

1 ///   <Summary>
2 /// Itcppool is used to unify a TCP connection pool and a set of TCP connection pools. In this way, the message dispatcher only needs to use the itcppool interface.
3 /// Zhuweisky
4 ///   </Summary>
5 Public   Interface Itcppool
6 {
7 Rentstreamresult implements tcpstream ( Int Pooltypekey, Int Servicekey, Out Networkstream stream, Out   Int Serverid ); // Pooltypekey indicates a city, and servicekey indicates a service.
8 Void Givebacktcpstream ( Int Streamhashcode, Int Serverid ); // Return TCP connection rules to the connection pool
9 Void Setstreamdamaged ( Int Streamhashcode, Int Serverid ); // If poolkey is not easy to save, simply transfer-1 here.
10
11 Event Callbackcountchanged activeconnectioncountchanged;
12 Event Callbackpoolstatechanged poolstatechanged;
13 }
14
15 Public   Delegate   Void Callbackcountchanged ( Int Serverid, Int Activeconncount );
16 Public   Delegate   Void Callbackpoolstatechanged ( Int Serverid, Bool Disconnected );
17
18 Public   Enum Rentstreamresult
19 {
20 Succeed, busy, theservicenotexist
21 }

you may have discovered that, all elements in itcppool the corresponding content can be found in the itcpstreampool interface, but the parameters of some methods become complicated. This is mainly because the itcpstreampool interface is for a FS, the itcppool may be for a FS or a group of FS. When the itcppool is a set of FS, the parameter serverid is required to distinguish each FS. we know that, as and each one TCP connection pool is used for communication between FS and :

all these connection pools must be managed, esframework itcppoolsmanager ( connection pool manager) the component manages multiple TCP connection pools. To unify the connection pool manager and a single connection pool so that they have the same external interface, therefore, the itcppool interface is introduced.
the advantage of doing so is, you can directly use the itcppool interface in the application, instead of worrying about the interface, there is a "single connection pool" (corresponding to a single FS ) or a set of connection pools managed by the connection pool manager (corresponding to multiple FS ). In addition, itcppoolsmanager manages many complex applications, for example, the scheduling of the kinetic energy server (implementing FS load balancing), dynamically add connection pool / remove. These will be described below.

Next articleArticle: Esframework introduction (11) -- TCP connection pool manager

Previous Article: esframework (6)-4-layer architecture based on C/S

Go to: esframework reusable Communication Framework (sequence)

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.