Skip to content

Commit 69ad576

Browse files
committed
apps: httpd: add --load-balancing-algorithm
Useful for, well, testing load balancing algorithms. Can be used with curl's --local-port option, and a local haproxy to test the proxy protocol.
1 parent 2b07f08 commit 69ad576

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

apps/httpd/main.cc

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <seastar/core/print.hh>
3535
#include <seastar/net/inet_address.hh>
3636
#include <seastar/util/defer.hh>
37+
#include <seastar/net/api.hh>
3738
#include "../lib/stop_signal.hh"
3839

3940
namespace bpo = boost::program_options;
@@ -80,6 +81,9 @@ int main(int ac, char** av) {
8081
app_template app;
8182

8283
app.add_options()("port", bpo::value<uint16_t>()->default_value(10000), "HTTP Server port");
84+
app.add_options()("load-balancing-algorithm",
85+
bpo::value<std::string>()->default_value("connection_distribution"),
86+
"Load balancing algorithm: connection_distribution, port, proxy_protocol_v2_and_port, fixed");
8387
app.add_options()("prometheus_port", bpo::value<uint16_t>()->default_value(9180), "Prometheus port. Set to zero in order to disable.");
8488
app.add_options()("prometheus_address", bpo::value<sstring>()->default_value("0.0.0.0"), "Prometheus address");
8589
app.add_options()("prometheus_prefix", bpo::value<sstring>()->default_value("seastar_httpd"), "Prometheus metrics prefix");
@@ -132,7 +136,24 @@ int main(int ac, char** av) {
132136
server->set_routes(set_routes).get();
133137
server->set_routes([rb](routes& r){rb->set_api_doc(r);}).get();
134138
server->set_routes([rb](routes& r) {rb->register_function(r, "demo", "hello world application");}).get();
135-
server->listen(port).get();
139+
140+
auto lba_str = config["load-balancing-algorithm"].as<std::string>();
141+
server_socket::load_balancing_algorithm lba;
142+
if (lba_str == "connection_distribution") {
143+
lba = server_socket::load_balancing_algorithm::connection_distribution;
144+
} else if (lba_str == "port") {
145+
lba = server_socket::load_balancing_algorithm::port;
146+
} else if (lba_str == "proxy_protocol_v2_and_port") {
147+
lba = server_socket::load_balancing_algorithm::proxy_protocol_v2_and_port;
148+
} else if (lba_str == "fixed") {
149+
lba = server_socket::load_balancing_algorithm::fixed;
150+
} else {
151+
throw std::runtime_error("Invalid load balancing algorithm: " + lba_str);
152+
}
153+
154+
listen_options lo;
155+
lo.lba = lba;
156+
server->listen(socket_address{net::inet_address{}, port}, lo).get();
136157

137158
std::cout << "Seastar HTTP server listening on port " << port << " ...\n";
138159

0 commit comments

Comments
 (0)