|
| 1 | +#include "../../blocks/http/api.h" |
| 2 | +#include"../../bricks/dflags/dflags.h" |
| 3 | + |
| 4 | +DEFINE_uint16(port, 8080, "The local port to use."); |
| 5 | +DEFINE_bool(unsafe, false, "Set to illustrate the unsafe behavior."); |
| 6 | + |
| 7 | +inline void RunUnsafe() { |
| 8 | + // This code does not do what the user expects, since listening on the port happens on a separate thread, |
| 9 | + // which means it is done asynchronously, so that a) the exception will be thrown in a different thread, and |
| 10 | + // 2) it will be thrown too late to be caught regardless. |
| 11 | + // |
| 12 | + // The output of the 2nd concurrent run of `./.current/safe_http_server --unsafe` would likely be: |
| 13 | + // $ ./.current/safe_http_server --unsafe |
| 14 | + // unsafe ok, listening |
| 15 | + // terminate called after throwing an instance of 'current::net::SocketBindException' |
| 16 | + // what(): ... SocketBindException(static_cast<uint16_t>(bare_port)) 8080 |
| 17 | + // zsh: abort (core dumped) ./.current/safe_http_server --unsafe |
| 18 | + // |
| 19 | + // This is why the recommended way to acquire the port in a try/catch block for the HTTP server |
| 20 | + // is via `current::net::AcquireLocalPort`, the return value of which is `std::move()`-d into `HTTP()` |
| 21 | + try { |
| 22 | + auto& http = HTTP(current::net::BarePort(FLAGS_port)); |
| 23 | + auto scope = http.Register("/", [](Request r) { r("unsafe ok\n"); }); |
| 24 | + std::cerr << "unsafe ok, listening" << std::endl; |
| 25 | + http.Join(); |
| 26 | + } catch (current::net::SocketBindException const&) { |
| 27 | + // NOTE(dkorolev): This `catch` block will *not* be executed! |
| 28 | + // TODO(dkorolev): Maybe even remove this old `HTTP()` constructor, or at least mark it as deprecated. |
| 29 | + std::cerr << "unsafe can not listen, the local port is taken" << std::endl; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +inline void RunSafe() { |
| 34 | + // This is the correct way to ensure the `current::net::SocketBindException` is caught. |
| 35 | + // TODO(dkorolev): Now that I think about it, we may well make `RunUnsafe()` do the same thing under the hood. |
| 36 | + try { |
| 37 | + auto safe_port = current::net::AcquireLocalPort(FLAGS_port); |
| 38 | + auto& http = HTTP(std::move(safe_port)); |
| 39 | + |
| 40 | + // Of course, the commented line below would also work. The above two are just for illustrative purposes. |
| 41 | + // auto& http = HTTP(current::net::AcquireLocalPort(FLAGS_port)); |
| 42 | + |
| 43 | + auto scope = http.Register("/", [](Request r) { r("safe ok\n"); }); |
| 44 | + std::cerr << "safe ok, listening" << std::endl; |
| 45 | + http.Join(); |
| 46 | + } catch (current::net::SocketBindException const&) { |
| 47 | + // NOTE(dkorolev): This `catch` block will *not* be executed! |
| 48 | + // TODO(dkorolev): Maybe even remove this old `HTTP()` constructor, or at least mark it as deprecated. |
| 49 | + std::cerr << "safe can not listen, the local port is taken" << std::endl; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +int main(int argc, char** argv) { |
| 54 | + ParseDFlags(&argc, &argv); |
| 55 | + |
| 56 | + if (!FLAGS_unsafe) { |
| 57 | + RunSafe(); |
| 58 | + } else { |
| 59 | + RunUnsafe(); |
| 60 | + } |
| 61 | +} |
0 commit comments