proxygen
ServerBootstrap.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
19 #include <wangle/channel/Handler.h>
21 
22 namespace wangle {
23 
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 }
40 
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 }
76 
77 } // namespace wangle
std::shared_ptr< WorkerMap > workers_
*than *hazptr_holder h
Definition: Hazptr.h:116
folly::IOThreadPoolExecutor * exec_
void threadStopped(folly::ThreadPoolExecutor::ThreadHandle *) override
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_
void threadStarted(folly::ThreadPoolExecutor::ThreadHandle *) override
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_