Large number of simulteneous connections in thrift

爷,独闯天下 提交于 2019-11-30 15:01:18

Taking another approach, if you are using C++ to build your server, you can use TNonblockingServer instead of TThreadPoolServer, which will allow you to accept many connections at once, regardless of how many threads are active, etc...

That being said, you won't necessarily be able to actually do work faster (handlers still execute in a thread pool), but more clients will be able to connect to you at once.

Here's what the code looks like for the NB server:

shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
shared_ptr<MyHandler> handler(new MyHandler());
shared_ptr<TProcessor> processor(new MyProcessor(handler));
TNonblockingServer server(processor, protocolFactory, port);

Your limitation of four threads in the pool is built into the default constructor of the SimpleThreadManager:

class SimpleThreadManager : public ThreadManager::Impl {

 public:
  SimpleThreadManager(size_t workerCount=4, size_t pendingTaskCountMax=0) :
    workerCount_(workerCount),
    pendingTaskCountMax_(pendingTaskCountMax),
    firstTime_(true) {
  }
...
};

This ThreadManager object is passed to the ThreadPoolServer coonstructor, so pass a larger number to the constructor of this object to increase the size of your thread pool.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!