C # implement thrift connection pool [new]

Source: Internet
Author: User

Updated on February 8

1. Because the object pool uses a stack, and there is no "Object in use, but it is still in the pool", it is directly pop out, so the idle state is discarded, however, the concept of "Number of work objects" is added.

2. By the way, the stack is changed to the thread-safe concurrentstack, but this is not very important because the lock is retained.

3. Some places that do not require lock are removed.

4. When the object is returned, the connection is closed (but the connection is not destroyed, the first version of the connection is not closed, and there is always a problem). This makes validateonborrow must be set to true.

5. The process method in the example packages the service object with using and releases the connection at the time.

Original article:

The project decided to adopt thrift. After all tests passed, it was necessary to solve the connection pool problem. If I had a review on the Internet, was my keyword incorrect? There is no C # Implementation of thrift connection pool, no way, only write one according to the object pool mode, after multi-person multi-thread testing, basically no problem, relatively rough, welcome to Brick

Principles:

1. controls the total number of objects in the object pool.

2. The total number of idle objects in the object pool can be controlled (changed to the total number of working objects)

3. provides an interface for lending objects

4. provides an interface for returning objects

5. Check the lending and return objects

In this way, we can use it. In the future, we can add a timeout automatic release mechanism, which is not implemented yet.

It consists of the following parts:

1: configuration file/configuration object

The configuration file must be available. The configuration object is only used for convenience (for example, in other places). If there is no special requirement, you can directly write the configuration item to the private property.

Public class thriftconfig {// <summary> // server address // </Summary> Public String host {Get; set ;} /// <summary> /// service port /// </Summary> Public int port {Get; set ;} /// <summary> /// Transfer Encoding /// </Summary> Public encoding encode {Get; set ;} /// <summary> /// whether compression is enabled /// </Summary> Public bool zipped {Get; set ;} /// <summary> /// connection timeout /// </Summary> Public int timeout {Get; set ;} /// <summary >/// maximum number of objects that can be allocated from the cache pool /// </Summary> Public int maxactive {Get; set ;} /// <summary> /// maximum number of idle objects in the cache pool /// </Summary> Public int maxidle {Get; set ;} /// <summary> /// minimum number of idle objects in the cache pool /// </Summary> Public int minidle {Get; set ;} /// <summary> /// maximum number of blocking operations /// </Summary> Public int maxwait {Get; set ;} /// <summary >/// whether to verify the object when an object is allocated from the cache pool /// </Summary> Public bool validateonborrow {Get; set ;} /// <summary >/// whether to verify the object when returning objects from the cache pool /// </Summary> Public bool validateonreturn {Get; set ;} /// <summary> /// verify whether the object is verified when an object is suspended from the cache. // </Summary> Public bool validatewhiledidle {Get; set ;}

The configuration file is only in XML format.

2. Connection Pool

Public class thriftpool {# region attribute private thriftconfig config; /// <summary> /// object cache pool /// </Summary> // Private Static stack <tTransport> objectpool {Get; set ;} private Static concurrentstack <tTransport> objectpool {Get; Set ;}/// <summary> /// synchronize object /// </Summary> Private Static autoresetevent resetevent; /// <summary> /// each time an example is taken, it indicates that the activation object is added with 1. This attribute controls the object pool capacity. /// </Summary> Private Static volatile int activedcount = 0; /// <Summary> // synchronize Object locks /// </Summary> private static object locker = new object (); # endregion # region constructor public thriftpool () {Config = getconfig (); createresetevent (); createthriftpool ();} # endregion # Region Public method // <summary> // retrieve an object from the object pool /// </Summary> /// <returns> </returns> Public tTransport borrowinstance () {lock (locker) {// console. writeline ("number of objects before lending: {0}, number of working objects: {1}", objectpool. count (), activedcount); TT Ransport transport; // No idle object in the object pool if (objectpool. count () = 0) {// The number of activated objects in the object pool reaches the upper limit. If (activedcount = config. maxactive) {resetevent. waitone () ;}else {pushobject (createinstance () ;}} if (! Objectpool. trypop (out transport) throw new exception ("connection pool exception"); // transport = objectpool. pop (); activedcount ++; // check the inventory of the Object pool // the inventory of the Object pool is smaller than the minimum idle quantity, and the activation quantity is smaller than the maximum activation quantity, add an object to the object pool if (objectpool. count () <config. minidle & activedcount <config. maxactive) {pushobject (createinstance ();} If (config. validateonborrow) {validateonborrow (transport);} return transport ;}} /// <summary> /// return an object /// </Summary> /// <Param name = "instance "> </Param> Public void returninstance (tTransport instance) {// The object pool capacity reaches the upper limit. If (objectpool) is directly destroyed without returning the thread pool. count () = config. maxidle) {destoryinstance (instance);} else {If (config. validateonreturn) {validateonreturn (instance);} pushobject (instance); activedcount --; // sends a notification signal, and an object is returned to the resetevent of the Object pool. set ();} // console. writeline ("number of returned object pools: {0}, number of returned work objects: {1}", objectpool. count (), activedcount) ;}# endregion # region private method // <summ Ary >/// create a thread synchronization object /// </Summary> private void createresetevent () {If (resetevent = NULL) {resetevent = new autoresetevent (false );}} /// <summary> /// create an object pool /// </Summary> private void createthriftpool () {If (objectpool = NULL) {objectpool = new concurrentstack <tTransport> (); // new stack <tTransport> ();}} /// <summary> /// Add the object to the object pool /// </Summary> /// <Param name = "transport"> </param> private void pushobject (ttrans Port transport) {objectpool. push (Transport) ;}/// <summary> /// create an object /// </Summary> /// <returns> </returns> private tTransport createinstance () {tTransport transport = new tsocket (config. host, config. port); transport. open (); return transport ;}/// <summary> /// check the object when the object is retrieved /// </Summary> private void validateonborrow (tTransport instance) {If (! Instance. isopen) {instance. open () ;}/// <summary> // check the object when returning the object /// </Summary> private void validateonreturn (tTransport instance) {If (instance. isopen) {instance. close ();}} /// <summary> /// destroy object /// </Summary> /// <Param name = "instance"> </param> private void destoryinstance (tTransport instance) {instance. flush (); If (instance. isopen) {instance. close ();} instance. dispose () ;}/// <summary> // get the configuration parameter /// </Summary> /// <returns> </returns> private thriftconfig getconfig () {return utility. getconfig () ;}# endregion}

Encapsulates a service class to retrieve thrift connection objects and hides the object pool mode.

Note that this object must implement the idisposable interface, because the object must be returned after the connection is completed manually.

Internal class service: idisposable {thriftpool pool; tTransport transport; mythrifttest. client client; // The test server I wrote is called mythrifttest bool disposed; Public thriftconfig config {Get; set;} Public Service () {disposed = false; pool = new thriftpool (); Transport = pool. borrowinstance (); // retrieves an object from the object pool tprotocol protocol = new tbinaryprotocol (transport); client = new mythrifttest. client (Protocol);} public String invoke (string arg1, string arg2) {// The test method I wrote is called invoke return client. Invoke (arg1, arg2 );}~ Service () {dispose (false);} protected void dispose (bool disposing) {If (! Disposed) {If (disposing) {pool. returninstance (transport); // return the current object to the object pool} // release unmanaged resources disposed = true ;}} public void dispose () {dispose (true); GC. suppressfinalize (this );}}

Test:

static void Main(string[] args){for (int i = 0; i < 1000; i++){Thread t = new Thread(new ThreadStart(process));t.Name = "thread:" + i;t.Start();}}private static void process(){try{using(Service svr=new Service()){string o = svr.Invoke("hello", "world");log(o);}}catch (Exception ex){log(ex);}}static object o = new object();private static void log(string msg){lock (o){using (StreamWriter sm = new StreamWriter(@"D:\thrift.txt", true)){sm.WriteLine(msg);}}}

Writing a file in the test code instead of outputting it to the console is mainly because there are too few entries that can be displayed on the console. Once an error occurs, it will be rolled over.

The test is good. If any problem is found, modify it. Make another statement. Thank you!

Reference link:

C # http://www.cnblogs.com/RushSykes/archive/2012/04/16/2451880.html

Javahttp: // www.cnblogs.com/51cto/archive/2010/08/18/thrift_connection_pool.html

Related Article

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.