proxygen
wangle::ServerWorkerPool Class Reference

#include <ServerBootstrap-inl.h>

Inheritance diagram for wangle::ServerWorkerPool:
folly::ThreadPoolExecutor::Observer

Public Member Functions

 ServerWorkerPool (std::shared_ptr< AcceptorFactory > acceptorFactory, folly::IOThreadPoolExecutor *exec, std::shared_ptr< std::vector< std::shared_ptr< folly::AsyncSocketBase >>> sockets, std::shared_ptr< ServerSocketFactory > socketFactory)
 
template<typename F >
void forEachWorker (F &&f) const
 
void threadStarted (folly::ThreadPoolExecutor::ThreadHandle *) override
 
void threadStopped (folly::ThreadPoolExecutor::ThreadHandle *) override
 
void threadPreviouslyStarted (folly::ThreadPoolExecutor::ThreadHandle *thread) override
 
void threadNotYetStopped (folly::ThreadPoolExecutor::ThreadHandle *thread) override
 
- Public Member Functions inherited from folly::ThreadPoolExecutor::Observer
virtual ~Observer ()=default
 

Private Types

using WorkerMap = std::map< folly::ThreadPoolExecutor::ThreadHandle *, std::shared_ptr< Acceptor >>
 
using Mutex = folly::SharedMutexReadPriority
 

Private Attributes

std::shared_ptr< WorkerMapworkers_
 
std::shared_ptr< MutexworkersMutex_
 
std::shared_ptr< AcceptorFactoryacceptorFactory_
 
folly::IOThreadPoolExecutorexec_ {nullptr}
 
std::shared_ptr< std::vector< std::shared_ptr< folly::AsyncSocketBase > > > sockets_
 
std::shared_ptr< ServerSocketFactorysocketFactory_
 

Detailed Description

Definition at line 273 of file ServerBootstrap-inl.h.

Member Typedef Documentation

Constructor & Destructor Documentation

wangle::ServerWorkerPool::ServerWorkerPool ( std::shared_ptr< AcceptorFactory acceptorFactory,
folly::IOThreadPoolExecutor exec,
std::shared_ptr< std::vector< std::shared_ptr< folly::AsyncSocketBase >>>  sockets,
std::shared_ptr< ServerSocketFactory socketFactory 
)
inlineexplicit

Definition at line 275 of file ServerBootstrap-inl.h.

References f.

280  : workers_(std::make_shared<WorkerMap>())
281  , workersMutex_(std::make_shared<Mutex>())
282  , acceptorFactory_(acceptorFactory)
283  , exec_(exec)
284  , sockets_(sockets)
285  , socketFactory_(socketFactory) {
286  CHECK(exec);
287  }
std::shared_ptr< WorkerMap > workers_
folly::IOThreadPoolExecutor * exec_
std::shared_ptr< Mutex > workersMutex_
std::shared_ptr< std::vector< std::shared_ptr< folly::AsyncSocketBase > > > sockets_
std::shared_ptr< ServerSocketFactory > socketFactory_
std::shared_ptr< AcceptorFactory > acceptorFactory_

Member Function Documentation

template<typename F >
void wangle::ServerWorkerPool::forEachWorker ( F &&  f) const

Definition at line 318 of file ServerBootstrap-inl.h.

References f.

318  {
319  Mutex::ReadHolder holder(workersMutex_.get());
320  for (const auto& kv : *workers_) {
321  f(kv.second.get());
322  }
323 }
std::shared_ptr< WorkerMap > workers_
auto f
std::shared_ptr< Mutex > workersMutex_
void wangle::ServerWorkerPool::threadNotYetStopped ( folly::ThreadPoolExecutor::ThreadHandle thread)
inlineoverridevirtual

Reimplemented from folly::ThreadPoolExecutor::Observer.

Definition at line 298 of file ServerBootstrap-inl.h.

References map().

299  {
300  threadStopped(thread);
301  }
void threadStopped(folly::ThreadPoolExecutor::ThreadHandle *) override
void wangle::ServerWorkerPool::threadPreviouslyStarted ( folly::ThreadPoolExecutor::ThreadHandle thread)
inlineoverridevirtual

Reimplemented from folly::ThreadPoolExecutor::Observer.

Definition at line 294 of file ServerBootstrap-inl.h.

295  {
296  threadStarted(thread);
297  }
void threadStarted(folly::ThreadPoolExecutor::ThreadHandle *) override
void wangle::ServerWorkerPool::threadStarted ( folly::ThreadPoolExecutor::ThreadHandle h)
overridevirtual

Implements folly::ThreadPoolExecutor::Observer.

Definition at line 24 of file ServerBootstrap.cpp.

References acceptorFactory_, exec_, folly::IOThreadPoolExecutor::getEventBase(), h, folly::netops::socket(), socketFactory_, sockets_, workers_, and workersMutex_.

25  {
26  auto worker = acceptorFactory_->newAcceptor(exec_->getEventBase(h));
27  {
28  Mutex::WriteHolder holder(workersMutex_.get());
29  workers_->insert({h, worker});
30  }
31 
32  for(auto socket : *sockets_) {
33  socket->getEventBase()->runImmediatelyOrRunInEventBaseThreadAndWait(
34  [this, worker, socket](){
35  socketFactory_->addAcceptCB(
36  socket, worker.get(), worker->getEventBase());
37  });
38  }
39 }
std::shared_ptr< WorkerMap > workers_
*than *hazptr_holder h
Definition: Hazptr.h:116
folly::IOThreadPoolExecutor * exec_
std::shared_ptr< Mutex > workersMutex_
std::shared_ptr< std::vector< std::shared_ptr< folly::AsyncSocketBase > > > sockets_
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
folly::EventBase * getEventBase() override
std::shared_ptr< ServerSocketFactory > socketFactory_
std::shared_ptr< AcceptorFactory > acceptorFactory_
void wangle::ServerWorkerPool::threadStopped ( folly::ThreadPoolExecutor::ThreadHandle h)
overridevirtual

Implements folly::ThreadPoolExecutor::Observer.

Definition at line 41 of file ServerBootstrap.cpp.

References folly::gen::move, folly::netops::socket(), socketFactory_, sockets_, workers_, and workersMutex_.

42  {
43  auto worker = [&]() -> std::shared_ptr<Acceptor> {
44  Mutex::WriteHolder holder(workersMutex_.get());
45  auto workerIt = workers_->find(h);
46  if (workerIt == workers_->end()) {
47  // The thread handle may not be present in the map if newAcceptor() throws
48  // an exception. For example, some acceptors require TLS keys / certs to
49  // start and will throw exceptions if those files do not exist.
50  return nullptr;
51  }
52  auto w = std::move(workerIt->second);
53  workers_->erase(workerIt);
54  return w;
55  }();
56  if (!worker) {
57  return;
58  }
59 
60  for (auto socket : *sockets_) {
61  socket->getEventBase()->runImmediatelyOrRunInEventBaseThreadAndWait(
62  [&]() {
63  socketFactory_->removeAcceptCB(
64  socket, worker.get(), nullptr);
65  });
66  }
67 
68  auto evb = worker->getEventBase();
69 
70  evb->runImmediatelyOrRunInEventBaseThreadAndWait(
71  [w = std::move(worker)]() mutable {
72  w->dropAllConnections();
73  w.reset();
74  });
75 }
std::shared_ptr< WorkerMap > workers_
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::shared_ptr< Mutex > workersMutex_
std::shared_ptr< std::vector< std::shared_ptr< folly::AsyncSocketBase > > > sockets_
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
std::shared_ptr< ServerSocketFactory > socketFactory_

Member Data Documentation

std::shared_ptr<AcceptorFactory> wangle::ServerWorkerPool::acceptorFactory_
private

Definition at line 310 of file ServerBootstrap-inl.h.

Referenced by threadStarted().

folly::IOThreadPoolExecutor* wangle::ServerWorkerPool::exec_ {nullptr}
private

Definition at line 311 of file ServerBootstrap-inl.h.

Referenced by threadStarted().

std::shared_ptr<ServerSocketFactory> wangle::ServerWorkerPool::socketFactory_
private

Definition at line 314 of file ServerBootstrap-inl.h.

Referenced by threadStarted(), and threadStopped().

std::shared_ptr<std::vector<std::shared_ptr<folly::AsyncSocketBase> > > wangle::ServerWorkerPool::sockets_
private

Definition at line 313 of file ServerBootstrap-inl.h.

Referenced by threadStarted(), and threadStopped().

std::shared_ptr<WorkerMap> wangle::ServerWorkerPool::workers_
private

Definition at line 308 of file ServerBootstrap-inl.h.

Referenced by threadStarted(), and threadStopped().

std::shared_ptr<Mutex> wangle::ServerWorkerPool::workersMutex_
private

Definition at line 309 of file ServerBootstrap-inl.h.

Referenced by threadStarted(), and threadStopped().


The documentation for this class was generated from the following files: