2 * Copyright 2015 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <folly/wangle/bootstrap/ServerBootstrap-inl.h>
19 #include <folly/Baton.h>
20 #include <folly/wangle/channel/ChannelPipeline.h>
24 typedef folly::wangle::ChannelPipeline<
25 folly::IOBufQueue&, std::unique_ptr<folly::IOBuf>> DefaultPipeline;
28 * ServerBootstrap is a parent class intended to set up a
29 * high-performance TCP accepting server. It will manage a pool of
30 * accepting threads, any number of accepting sockets, a pool of
31 * IO-worker threads, and connection pool for each IO thread for you.
33 * The output is given as a ChannelPipeline template: given a
34 * PipelineFactory, it will create a new pipeline for each connection,
35 * and your server can handle the incoming bytes.
37 * BACKWARDS COMPATIBLITY: for servers already taking a pool of
38 * Acceptor objects, an AcceptorFactory can be given directly instead
39 * of a pipeline factory.
41 template <typename Pipeline>
42 class ServerBootstrap {
49 typedef wangle::ChannelPipeline<
51 std::exception> AcceptPipeline;
53 * Pipeline used to add connections to event bases.
54 * This is used for UDP or for load balancing
55 * TCP connections to IO threads explicitly
57 ServerBootstrap* pipeline(
58 std::shared_ptr<PipelineFactory<AcceptPipeline>> factory) {
63 ServerBootstrap* channelFactory(
64 std::shared_ptr<ServerSocketFactory> factory) {
65 socketFactory_ = factory;
70 * BACKWARDS COMPATIBILITY - an acceptor factory can be set. Your
71 * Acceptor is responsible for managing the connection pool.
73 * @param childHandler - acceptor factory to call for each IO thread
75 ServerBootstrap* childHandler(std::shared_ptr<AcceptorFactory> h) {
81 * Set a pipeline factory that will be called for each new connection
83 * @param factory pipeline factory to use for each new connection
85 ServerBootstrap* childPipeline(
86 std::shared_ptr<PipelineFactory<Pipeline>> factory) {
87 childPipelineFactory_ = factory;
92 * Set the IO executor. If not set, a default one will be created
93 * with one thread per core.
95 * @param io_group - io executor to use for IO threads.
97 ServerBootstrap* group(
98 std::shared_ptr<folly::wangle::IOThreadPoolExecutor> io_group) {
99 return group(nullptr, io_group);
103 * Set the acceptor executor, and IO executor.
105 * If no acceptor executor is set, a single thread will be created for accepts
106 * If no IO executor is set, a default of one thread per core will be created
108 * @param group - acceptor executor to use for acceptor threads.
109 * @param io_group - io executor to use for IO threads.
111 ServerBootstrap* group(
112 std::shared_ptr<folly::wangle::IOThreadPoolExecutor> accept_group,
113 std::shared_ptr<wangle::IOThreadPoolExecutor> io_group) {
115 accept_group = std::make_shared<folly::wangle::IOThreadPoolExecutor>(
116 1, std::make_shared<wangle::NamedThreadFactory>("Acceptor Thread"));
119 io_group = std::make_shared<folly::wangle::IOThreadPoolExecutor>(
120 32, std::make_shared<wangle::NamedThreadFactory>("IO Thread"));
123 // TODO better config checking
124 // CHECK(acceptorFactory_ || childPipelineFactory_);
125 CHECK(!(acceptorFactory_ && childPipelineFactory_));
127 if (acceptorFactory_) {
128 workerFactory_ = std::make_shared<ServerWorkerPool>(
129 acceptorFactory_, io_group.get(), &sockets_, socketFactory_);
131 workerFactory_ = std::make_shared<ServerWorkerPool>(
132 std::make_shared<ServerAcceptorFactory<Pipeline>>(
133 childPipelineFactory_,
135 io_group.get(), &sockets_, socketFactory_);
138 io_group->addObserver(workerFactory_);
140 acceptor_group_ = accept_group;
141 io_group_ = io_group;
147 * Bind to an existing socket
149 * @param sock Existing socket to use for accepting
151 void bind(folly::AsyncServerSocket::UniquePtr s) {
152 if (!workerFactory_) {
156 // Since only a single socket is given,
157 // we can only accept on a single thread
158 CHECK(acceptor_group_->numThreads() == 1);
160 std::shared_ptr<folly::AsyncServerSocket> socket(
161 s.release(), DelayedDestruction::Destructor());
163 folly::Baton<> barrier;
164 acceptor_group_->add([&](){
165 socket->attachEventBase(EventBaseManager::get()->getEventBase());
166 socket->listen(socketConfig.acceptBacklog);
167 socket->startAccepting();
172 // Startup all the threads
173 workerFactory_->forEachWorker([this, socket](Acceptor* worker){
174 socket->getEventBase()->runInEventBaseThreadAndWait(
175 [this, worker, socket](){
176 socketFactory_->addAcceptCB(socket, worker, worker->getEventBase());
180 sockets_.push_back(socket);
183 void bind(folly::SocketAddress& address) {
184 bindImpl(-1, address);
188 * Bind to a port and start listening.
189 * One of childPipeline or childHandler must be called before bind
191 * @param port Port to listen on
193 void bind(int port) {
195 folly::SocketAddress address;
196 bindImpl(port, address);
199 void bindImpl(int port, folly::SocketAddress& address) {
200 if (!workerFactory_) {
204 bool reusePort = false;
205 if (acceptor_group_->numThreads() > 1) {
209 std::mutex sock_lock;
210 std::vector<std::shared_ptr<folly::AsyncSocketBase>> new_sockets;
213 std::exception_ptr exn;
215 auto startupFunc = [&](std::shared_ptr<folly::Baton<>> barrier){
218 auto socket = socketFactory_->newSocket(
219 port, address, socketConfig.acceptBacklog, reusePort, socketConfig);
222 new_sockets.push_back(socket);
226 socket->getAddress(&address);
227 port = address.getPort();
232 exn = std::current_exception();
242 auto wait0 = std::make_shared<folly::Baton<>>();
243 acceptor_group_->add(std::bind(startupFunc, wait0));
246 for (size_t i = 1; i < acceptor_group_->numThreads(); i++) {
247 auto barrier = std::make_shared<folly::Baton<>>();
248 acceptor_group_->add(std::bind(startupFunc, barrier));
253 std::rethrow_exception(exn);
256 for (auto& socket : new_sockets) {
257 // Startup all the threads
258 workerFactory_->forEachWorker([this, socket](Acceptor* worker){
259 socket->getEventBase()->runInEventBaseThreadAndWait([this, worker, socket](){
260 socketFactory_->addAcceptCB(socket, worker, worker->getEventBase());
264 sockets_.push_back(socket);
269 * Stop listening on all sockets.
272 for (auto socket : sockets_) {
273 folly::Baton<> barrier;
274 socket->getEventBase()->runInEventBaseThread([&]() mutable {
275 socketFactory_->stopSocket(socket);
282 if (acceptor_group_) {
283 acceptor_group_->join();
291 * Get the list of listening sockets
293 const std::vector<std::shared_ptr<folly::AsyncSocketBase>>&
298 std::shared_ptr<wangle::IOThreadPoolExecutor> getIOGroup() const {
302 template <typename F>
303 void forEachWorker(F&& f) const {
304 workerFactory_->forEachWorker(f);
307 ServerSocketConfig socketConfig;
310 std::shared_ptr<wangle::IOThreadPoolExecutor> acceptor_group_;
311 std::shared_ptr<wangle::IOThreadPoolExecutor> io_group_;
313 std::shared_ptr<ServerWorkerPool> workerFactory_;
314 std::vector<std::shared_ptr<folly::AsyncSocketBase>> sockets_;
316 std::shared_ptr<AcceptorFactory> acceptorFactory_;
317 std::shared_ptr<PipelineFactory<Pipeline>> childPipelineFactory_;
318 std::shared_ptr<PipelineFactory<AcceptPipeline>> pipeline_{
319 std::make_shared<DefaultAcceptPipelineFactory>()};
320 std::shared_ptr<ServerSocketFactory> socketFactory_{
321 std::make_shared<AsyncServerSocketFactory>()};