Skip to content

Commit 2e03c8a

Browse files
committed
feat: manage new HTTP server properties
Signed-off-by: Leonardo Cecchi <leonardo.cecchi@enterprisedb.com>
1 parent 364593a commit 2e03c8a

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ Environment variables are your friends:
1515
* `SQL_QUERY` is the SQL query that will be executed when accessing the `/tx`
1616
endpoint.
1717

18+
* `KEEP_ALIVE_MAX_COUNT` TCP keepalive configuration
19+
20+
* `KEEP_ALIVE_TIMEOUT` TCP keepalive configuration
21+
22+
* `READ_TIMEOUT` HTTP request handling read timeout
23+
24+
* `WRITE_TIMEOUT` HTTP request handling write timeout
25+
26+
* `IDLE_INTERVAL` HTTP request timeout idle interval
27+
1828
The endpoint `/.well-known/check` will always return "ok".
1929

2030
## Other considerations

http_test.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ void ok(const httplib::Request &req, httplib::Response &res);
2525
void logger(const httplib::Request &req, const httplib::Response &res);
2626
std::string get_configuration(const std::string &env_name,
2727
const std::string &default_value = "");
28+
void set_server_option_from_env(const std::string& env_name, std::function<void(int)>setter);
2829

2930
/**
3031
* The database URL to be used as a libpq connection string
@@ -45,11 +46,39 @@ int main() {
4546
httplib::Server svr;
4647
svr.Get("/tx", tx);
4748
svr.Get("/.well-known/check", ok);
49+
50+
// Keepalive settings
51+
set_server_option_from_env("KEEP_ALIVE_MAX_COUNT", [&svr](int value){ svr.set_keep_alive_max_count(value); });
52+
set_server_option_from_env("KEEP_ALIVE_TIMEOUT", [&svr](int value){ svr.set_keep_alive_timeout(value); });
53+
54+
// Timeout settings
55+
set_server_option_from_env("READ_TIMEOUT", [&svr](int value){ svr.set_read_timeout(value); });
56+
set_server_option_from_env("WRITE_TIMEOUT", [&svr](int value){ svr.set_write_timeout(value); });
57+
set_server_option_from_env("IDLE_INTERVAL", [&svr](int value){ svr.set_idle_interval(value); });
58+
4859
svr.listen("0.0.0.0", 8080);
4960

5061
return 0;
5162
}
5263

64+
/**
65+
* This is an helper function to set an HTTP server property from
66+
* an environment variable.
67+
* The property to be set is an integer.
68+
*/
69+
void set_server_option_from_env(const std::string& env_name, std::function<void(int)>setter) {
70+
const std::string env_value = get_configuration(env_name);
71+
72+
if (!env_value.empty()) {
73+
try {
74+
std::cout << env_name << ": " << std::quoted(env_value) << std::endl;
75+
setter(std::stoi(env_value));
76+
} catch(std::exception &e) {
77+
std::cout << "Error parsing " << env_name << " as an integer" << std::endl;
78+
}
79+
}
80+
}
81+
5382
/**
5483
* This is a context class that to be used inside a request handler. It will
5584
* log the request handling time

0 commit comments

Comments
 (0)