* Creates a new connection either by reusing an object off the stack or * by allocating a new one entirely */
| 910 | * by allocating a new one entirely |
| 911 | */ |
| 912 | TNonblockingServer::TConnection* TNonblockingServer::createConnection(std::shared_ptr<TSocket> socket) { |
| 913 | // Check the stack |
| 914 | Guard g(connMutex_); |
| 915 | |
| 916 | // pick an IO thread to handle this connection -- currently round robin |
| 917 | assert(nextIOThread_ < ioThreads_.size()); |
| 918 | int selectedThreadIdx = nextIOThread_; |
| 919 | nextIOThread_ = static_cast<uint32_t>((nextIOThread_ + 1) % ioThreads_.size()); |
| 920 | |
| 921 | TNonblockingIOThread* ioThread = ioThreads_[selectedThreadIdx].get(); |
| 922 | |
| 923 | // Check the connection stack to see if we can re-use |
| 924 | TConnection* result = nullptr; |
| 925 | if (connectionStack_.empty()) { |
| 926 | result = new TConnection(socket, ioThread); |
| 927 | ++numTConnections_; |
| 928 | } else { |
| 929 | result = connectionStack_.top(); |
| 930 | connectionStack_.pop(); |
| 931 | result->setSocket(socket); |
| 932 | result->init(ioThread); |
| 933 | } |
| 934 | |
| 935 | activeConnections_.insert(result); |
| 936 | return result; |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * Returns a connection to the stack |
no test coverage detected