proxygen
PushServer.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 "PushRequestHandler.h"
18 #include "PushStats.h"
19 
20 using namespace PushService;
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 PushStats);
40  }
41 
42  void onServerStop() noexcept override {
43  stats_.reset();
44  }
45 
47  return new PushRequestHandler(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 = true;
77  .build();
78 
79  HTTPServer server(std::move(options));
80  server.bind(IPs);
81 
82  // Start HTTPServer mainloop in a separate thread
83  std::thread t([&] () {
84  server.start();
85  });
86 
87  t.join();
88  return 0;
89 }
void start(std::function< void()> onSuccess=nullptr, std::function< void(std::exception_ptr)> onError=nullptr)
Definition: HTTPServer.cpp:119
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)
folly::ThreadLocalPtr< PushStats > stats_
Definition: PushServer.cpp:51
void onServerStart(folly::EventBase *) noexceptoverride
Definition: PushServer.cpp:38
requires E e noexcept(noexcept(s.error(std::move(e))))
std::vector< std::thread::id > threads
DEFINE_int32(http_port, 11000,"Port to listen on with HTTP protocol")
char ** argv
std::vector< std::unique_ptr< RequestHandlerFactory > > handlerFactories
int main(int argc, char *argv[])
Definition: PushServer.cpp:54
DEFINE_string(ip,"localhost","IP/Hostname to bind to")
RequestHandler * onRequest(RequestHandler *, HTTPMessage *) noexceptoverride
Definition: PushServer.cpp:46
void onServerStop() noexceptoverride
Definition: PushServer.cpp:42
std::vector< int > shutdownOn
HTTPServer::Protocol Protocol
Definition: PushServer.cpp:27