-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProxyServer.cpp
103 lines (87 loc) · 3.12 KB
/
ProxyServer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Copyright (c) 2017, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#include <gflags/gflags.h>
#include <folly/Memory.h>
#include <folly/io/async/EventBaseManager.h>
#include <proxygen/httpserver/HTTPServer.h>
#include <proxygen/httpserver/RequestHandlerFactory.h>
#include <unistd.h>
#include "ProxyHandler.h"
#include "ProxyStats.h"
using namespace ProxyService;
using namespace proxygen;
using folly::EventBase;
using folly::EventBaseManager;
using folly::SocketAddress;
using folly::HHWheelTimer;
using Protocol = HTTPServer::Protocol;
DEFINE_int32(http_port, 8080, "Port to listen on with HTTP protocol");
DEFINE_int32(spdy_port, 11001, "Port to listen on with SPDY protocol");
DEFINE_int32(h2_port, 11002, "Port to listen on with HTTP/2 protocol");
DEFINE_string(ip, "localhost", "IP/Hostname to bind to");
DEFINE_int32(threads, 0, "Number of threads to listen on. Numbers <= 0 "
"will use the number of cores on this machine.");
DEFINE_int32(server_timeout, 60,
"How long to wait for a server response (sec)");
class ProxyHandlerFactory : public RequestHandlerFactory {
public:
void onServerStart(folly::EventBase* evb) noexcept override {
stats_.reset(new ProxyStats);
timer_->timer = HHWheelTimer::newTimer(
evb,
std::chrono::milliseconds(HHWheelTimer::DEFAULT_TICK_INTERVAL),
folly::AsyncTimeout::InternalEnum::NORMAL,
std::chrono::seconds(FLAGS_server_timeout));
}
void onServerStop() noexcept override {
stats_.reset();
timer_->timer.reset();
}
RequestHandler* onRequest(RequestHandler*, HTTPMessage*) noexcept override {
return new ProxyHandler(stats_.get(), timer_->timer.get());
}
private:
struct TimerWrapper {
HHWheelTimer::UniquePtr timer;
};
folly::ThreadLocalPtr<ProxyStats> stats_;
folly::ThreadLocal<TimerWrapper> timer_;
};
int main(int argc, char* argv[]) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
google::InitGoogleLogging(argv[0]);
google::InstallFailureSignalHandler();
std::vector<HTTPServer::IPConfig> IPs = {
{SocketAddress(FLAGS_ip, FLAGS_http_port, true), Protocol::HTTP},
{SocketAddress(FLAGS_ip, FLAGS_spdy_port, true), Protocol::SPDY},
{SocketAddress(FLAGS_ip, FLAGS_h2_port, true), Protocol::HTTP2},
};
if (FLAGS_threads <= 0) {
FLAGS_threads = sysconf(_SC_NPROCESSORS_ONLN);
CHECK(FLAGS_threads > 0);
}
HTTPServerOptions options;
options.threads = static_cast<size_t>(FLAGS_threads);
options.idleTimeout = std::chrono::milliseconds(60000);
options.shutdownOn = {SIGINT, SIGTERM};
options.enableContentCompression = false;
options.handlerFactories = RequestHandlerChain()
.addThen<ProxyHandlerFactory>()
.build();
options.h2cEnabled = true;
HTTPServer server(std::move(options));
server.bind(IPs);
// Start HTTPServer mainloop in a separate thread
std::thread t([&] () {
server.start();
});
t.join();
return 0;
}