There is actually a lot to gain performance wise, depending on how long the 
call is going to take it can be 1 or 2 orders of magnitude faster to use thread 
local connections.  Linux also tends to get angry if you have thousands of 
processes opening sockets at a high rate.

I use boost thread local on the client side, this is pretty much all you need 
to do:

PlowClient* getClient()
{
    static boost::thread_specific_ptr<PlowClient> instance;
    if(!instance.get())
    {
        instance.reset(new PlowClient);
    }
    return instance.get();
}

Then I just have a macro I use everywhere.

#define PLOW_SERVICE plow::v1::getClient()->getService()

For Java, its slightly more complex but I can post an example later.

-Matt



> On Jun 7, 2016, at 9:29 AM, BCG <[email protected]> wrote:
> 
> 
> On 06/07/2016 07:54 AM, Matt Chambers wrote:
>> Another way to do it would be to use ThreadLocal connections.
>> https://docs.oracle.com/javase/8/docs/api/java/lang/ThreadLocal.html 
>> <https://docs.oracle.com/javase/8/docs/api/java/lang/ThreadLocal.html>
>> 
>>> On Jun 7, 2016, at 6:14 AM, Daniel Wu <[email protected]> wrote:
>>> 
>>> Thrift client/connection is not thread safe. Use a lock or object pool.
>>> 
>>> On 7/6/2016, 5:51 PM, "Tuan Le Dinh" <[email protected]> wrote:
>>> 
>>> 
> You might not see too much gain in simply pooling/locking/reusing clients, as 
> the real overhead that you are trying to avoid (I presume) is the overhead of 
> establishing connections.  Simply reusing THttpClient might not avoid this 
> overhead.
> 
> In addition to (or perhaps instead of) reusing client instances you might 
> want to investigate the connection pooling functionality built into Commons 
> HTTP Components:
> 
> https://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html
> 
> You could, for example, manually configure an HttpClient instance to use 
> connection pooling and then just pass that instance to the THttpClient(String 
> url, HttpClient client) constructor.
> 

Reply via email to