2

I have thousands of concurrent client threads with C10K synchronized request, newing a tcp connect when request comes might be time-consuming, and it cannot be scalable if the number of concurrent threads goes up.

So I thought a shared connection(tcp) pool might be a good choice for me, all connects(tcp) within the pool are activated, all requests share the pool, pull out when need to send message, push in when receiving message done.

But how can I achieve that? By the way, my server side conforms a Mulit-threading Async mode.

The piece code for connection(client-side):

class SyncTCPClient {
public:
    SyncTCPClient(const std::string &raw_ip_address, unsigned short port_num) :
            m_ep(asio::ip::address::from_string(raw_ip_address), port_num), m_sock(m_ios) {
      m_sock.open(m_ep.protocol());
    }
    void connect() {
      m_sock.connect(m_ep);
    }
    //...
private:
    asio::io_service m_ios;
    asio::ip::tcp::endpoint m_ep;
    asio::ip::tcp::socket m_sock;
};

The piece code for connection pool(client-side):

class ConnectionPool {
private:
    std::queue<std::shared_ptr<SyncTCPClient>> pool;
    std::mutex mtx;
    std::condition_variable no_empty;
public:
    ConnectionPool(int size) {
      std::cout << "Constructor for connection pool." << std::endl;
      for (int i = 0; i < size; i++) {
        std::shared_ptr<SyncTCPClient> cliPtr = std::make_shared<SyncTCPClient>(raw_ip_address, port_num);
        cliPtr->connect();
        pool.push(cliPtr);
      }
    }
    //...
};

The piece code for connection acceptance(server-side):

  std::shared_ptr<asio::ip::tcp::socket> sock = std::make_shared<asio::ip::tcp::socket>(m_ios);
  m_acceptor.async_accept(*sock.get(),
                          [this, sock](const boost::system::error_code& error) {
                              onAccept(error, sock);
                          }
  );

Much appreciated-)

joe
  • 177
  • 1
  • 1
  • 11
  • What do you mean "concurrent client threads with C10K blocked request"? Blocking requests is easily achieved by not listening/accepting in the first place. If you meant "blocking" (as in: synchronous) requests then pooling really doesn't help. "thousands of concurrent client threads" - that's a huge anti-pattern for any kind of high-performance server – sehe Nov 15 '17 at 14:35
  • Did you actually mean "how to create a thread pool"? I've created connection pools (for keeping idle connections to many servers) but this question confuses me – sehe Nov 15 '17 at 18:33
  • 1
    Sorry for confusing you so much-) – joe Nov 16 '17 at 02:12
  • Sorry for confusing you so much-) I'm not good at these concepts, here "blocking" means my requests, both sending and receiving, are in synchronous mode. "concurrent client threads with C10K blocked request" means about 1k concurrent threads, call request few times every limited microseconds within every thread. That is C10K, isn't it? – joe Nov 16 '17 at 02:24
  • If you pull it off, but I don't think you will, since with 1k threads, thread scheduling and synchronization is going to dominate runtime. – sehe Nov 16 '17 at 02:25
  • Establishing a new tcp connect may be too time-consuming when request comes, so creating a connection pool, all connects are activated, all requests share these connects within the pool. This is just what I want. Thanks-) – joe Nov 16 '17 at 02:28
  • Why did you choose thread-per-connection? – sehe Nov 16 '17 at 02:30
  • What they need to be synchronized is the pool(eg. queue), I think. I can make it a exclusive(eg. mutex) or atomic one(eg. lock-free). – joe Nov 16 '17 at 02:31
  • "thread-per-connection" actually cannot be scalable if the number of concurrent requests goes up. – joe Nov 16 '17 at 02:32
  • That was my point. Why do you keep saying you have blocking requests and 1k concurrent threads? That's your own description. Perhaps you can show some code, because I think there's a language barrier at work here. – sehe Nov 16 '17 at 02:45
  • Hi @sehe, sorry for troubling you so much, and thanks for being so patient with my question. SO seems not allowing to paste much code here, can I have your skype, I can show you the code, if it is necessary? – joe Nov 16 '17 at 03:03

0 Answers0