proxygen
ProxyServer.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  */
11 #include <folly/Memory.h>
15 
16 #include "ProxyHandler.h"
17 #include "ProxyStats.h"
18 
19 using namespace ProxyService;
20 using namespace proxygen;
21 
22 using folly::EventBase;
26 
28 
29 DEFINE_int32(http_port, 11000, "Port to listen on with HTTP protocol");
30 DEFINE_int32(h2_port, 11002, "Port to listen on with HTTP/2 protocol");
31 DEFINE_string(ip, "localhost", "IP/Hostname to bind to");
32 DEFINE_int32(threads, 0, "Number of threads to listen on. Numbers <= 0 "
33  "will use the number of cores on this machine.");
34 DEFINE_int32(server_timeout, 60,
35  "How long to wait for a server response (sec)");
36 
38  public:
39  void onServerStart(folly::EventBase* evb) noexcept override {
40  stats_.reset(new ProxyStats);
41  timer_->timer = HHWheelTimer::newTimer(
42  evb,
43  std::chrono::milliseconds(HHWheelTimer::DEFAULT_TICK_INTERVAL),
45  std::chrono::seconds(FLAGS_server_timeout));
46  }
47 
48  void onServerStop() noexcept override {
49  stats_.reset();
50  timer_->timer.reset();
51  }
52 
54  return new ProxyHandler(stats_.get(), timer_->timer.get());
55  }
56 
57  private:
58  struct TimerWrapper {
59  HHWheelTimer::UniquePtr timer;
60  };
63 };
64 
65 int main(int argc, char* argv[]) {
66  gflags::ParseCommandLineFlags(&argc, &argv, true);
67  google::InitGoogleLogging(argv[0]);
68  google::InstallFailureSignalHandler();
69 
70  std::vector<HTTPServer::IPConfig> IPs = {
71  {SocketAddress(FLAGS_ip, FLAGS_http_port, true), Protocol::HTTP},
72  {SocketAddress(FLAGS_ip, FLAGS_h2_port, true), Protocol::HTTP2},
73  };
74 
75  if (FLAGS_threads <= 0) {
76  FLAGS_threads = sysconf(_SC_NPROCESSORS_ONLN);
77  CHECK(FLAGS_threads > 0);
78  }
79 
80  HTTPServerOptions options;
81  options.threads = static_cast<size_t>(FLAGS_threads);
82  options.idleTimeout = std::chrono::milliseconds(60000);
83  options.shutdownOn = {SIGINT, SIGTERM};
84  options.enableContentCompression = false;
87  .build();
88  options.h2cEnabled = true;
89  options.supportsConnect = true;
90 
91  HTTPServer server(std::move(options));
92  server.bind(IPs);
93 
94  // Start HTTPServer mainloop in a separate thread
95  std::thread t([&] () {
96  server.start();
97  });
98 
99  t.join();
100  return 0;
101 }
folly::ThreadLocal< TimerWrapper > timer_
Definition: ProxyServer.cpp:62
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
int main(int argc, char *argv[])
Definition: ProxyServer.cpp:65
void bind(std::vector< IPConfig > &&addrs)
Definition: HTTPServer.cpp:85
RequestHandlerChain & addThen(Args &&...args)
requires E e noexcept(noexcept(s.error(std::move(e))))
stop_watch< std::chrono::milliseconds > timer_
void onServerStart(folly::EventBase *evb) noexceptoverride
Definition: ProxyServer.cpp:39
std::vector< std::thread::id > threads
folly::ThreadLocalPtr< ProxyStats > stats_
Definition: ProxyServer.cpp:61
HHWheelTimer::UniquePtr timer
Definition: ProxyServer.cpp:59
RequestHandler * onRequest(RequestHandler *, HTTPMessage *) noexceptoverride
Definition: ProxyServer.cpp:53
char ** argv
std::vector< std::unique_ptr< RequestHandlerFactory > > handlerFactories
DEFINE_int32(http_port, 11000,"Port to listen on with HTTP protocol")
DEFINE_string(ip,"localhost","IP/Hostname to bind to")
std::vector< int > shutdownOn
void onServerStop() noexceptoverride
Definition: ProxyServer.cpp:48
HTTPServer::Protocol Protocol
Definition: ProxyServer.cpp:27