proxygen
EchoServer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 #include <folly/Memory.h>
16 
17 #include "EchoHandler.h"
18 #include "EchoStats.h"
19 
20 using namespace EchoService;
21 using namespace proxygen;
22 
23 using folly::EventBase;
26 
28 
29 DEFINE_int32(http_port, 11000, "Port to listen on with HTTP protocol");
30 DEFINE_int32(spdy_port, 11001, "Port to listen on with SPDY protocol");
31 DEFINE_int32(h2_port, 11002, "Port to listen on with HTTP/2 protocol");
32 DEFINE_string(ip, "localhost", "IP/Hostname to bind to");
33 DEFINE_int32(threads, 0, "Number of threads to listen on. Numbers <= 0 "
34  "will use the number of cores on this machine.");
35 
37  public:
38  void onServerStart(folly::EventBase* /*evb*/) noexcept override {
39  stats_.reset(new EchoStats);
40  }
41 
42  void onServerStop() noexcept override {
43  stats_.reset();
44  }
45 
47  return new EchoHandler(stats_.get());
48  }
49 
50  private:
52 };
53 
54 int main(int argc, char* argv[]) {
55  gflags::ParseCommandLineFlags(&argc, &argv, true);
56  google::InitGoogleLogging(argv[0]);
57  google::InstallFailureSignalHandler();
58 
59  std::vector<HTTPServer::IPConfig> IPs = {
60  {SocketAddress(FLAGS_ip, FLAGS_http_port, true), Protocol::HTTP},
61  {SocketAddress(FLAGS_ip, FLAGS_spdy_port, true), Protocol::SPDY},
62  {SocketAddress(FLAGS_ip, FLAGS_h2_port, true), Protocol::HTTP2},
63  };
64 
65  if (FLAGS_threads <= 0) {
66  FLAGS_threads = sysconf(_SC_NPROCESSORS_ONLN);
67  CHECK(FLAGS_threads > 0);
68  }
69 
70  HTTPServerOptions options;
71  options.threads = static_cast<size_t>(FLAGS_threads);
72  options.idleTimeout = std::chrono::milliseconds(60000);
73  options.shutdownOn = {SIGINT, SIGTERM};
74  options.enableContentCompression = false;
77  .build();
78  options.h2cEnabled = true;
79 
80  HTTPServer server(std::move(options));
81  server.bind(IPs);
82 
83  // Start HTTPServer mainloop in a separate thread
84  std::thread t([&] () {
85  server.start();
86  });
87 
88  t.join();
89  return 0;
90 }
RequestHandler * onRequest(RequestHandler *, HTTPMessage *) noexceptoverride
Definition: EchoServer.cpp:46
void onServerStart(folly::EventBase *) noexceptoverride
Definition: EchoServer.cpp:38
void start(std::function< void()> onSuccess=nullptr, std::function< void(std::exception_ptr)> onError=nullptr)
Definition: HTTPServer.cpp:119
void onServerStop() noexceptoverride
Definition: EchoServer.cpp:42
std::chrono::milliseconds idleTimeout
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void bind(std::vector< IPConfig > &&addrs)
Definition: HTTPServer.cpp:85
RequestHandlerChain & addThen(Args &&...args)
requires E e noexcept(noexcept(s.error(std::move(e))))
DEFINE_int32(http_port, 11000,"Port to listen on with HTTP protocol")
std::vector< std::thread::id > threads
char ** argv
std::vector< std::unique_ptr< RequestHandlerFactory > > handlerFactories
DEFINE_string(ip,"localhost","IP/Hostname to bind to")
HTTPServer::Protocol Protocol
Definition: EchoServer.cpp:27
folly::ThreadLocalPtr< EchoStats > stats_
Definition: EchoServer.cpp:51
std::vector< int > shutdownOn
int main(int argc, char *argv[])
Definition: EchoServer.cpp:54