Skip to content

Instantly share code, notes, and snippets.

@alexcohn
Last active April 25, 2020 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexcohn/c7f1d215fd2f1f904dc864e1ba881f2f to your computer and use it in GitHub Desktop.
Save alexcohn/c7f1d215fd2f1f904dc864e1ba881f2f to your computer and use it in GitHub Desktop.
try to shut down the Loop with uWebSockets v.0.17.4 (see https://github.com/uNetworking/uWebSockets/issues/809#issuecomment-619246807)
#include "App.h"
using namespace std::chrono_literals;
using namespace std::chrono;
#include "../uSockets/src/internal/eventing/epoll_kqueue.h"
/* Note that uWS::SSLApp({options}) is the same as uWS::App() when compiled without SSL support */
int main() {
us_listen_socket_t * listenSocket = nullptr;
auto before = steady_clock::now();
/* Overly simple hello world app */
uWS::SSLApp({
.key_file_name = "../misc/key.pem",
.cert_file_name = "../misc/cert.pem",
.passphrase = "1234"
}).get("/*", [&listenSocket, &before](auto *res, auto *req) {
res->end("hi there");
std::cout << req->getUrl() << std::endl;
if (req->getUrl()[1] == 'q') {
auto loop = reinterpret_cast<us_loop_t*>(uWS::Loop::get());
std::cout << "will shutdown " << loop << std::endl;
std::thread shutdown([&listenSocket, &before, loop] {
std::cout << "yes, will shutdown" << std::endl;
std::this_thread::sleep_for(200ms);
std::cout << "shutting down " << listenSocket << " " << loop->num_polls << std::endl;
before = steady_clock::now();
loop->num_polls = 0;
us_wakeup_loop(loop);
});
shutdown.detach();
}
}).listen(3000, [&listenSocket](auto *token) {
if (token) {
std::cout << "token " << token << std::endl;
std::cout << "Listening on port " << 3000 << std::endl;
}
listenSocket = token;
}).run();
std::cout << "finished in " << duration_cast<milliseconds>(steady_clock::now() - before).count() << "ms" << std::endl;
}
#include "App.h"
using namespace std::chrono_literals;
using namespace std::chrono;
/* Note that uWS::SSLApp({options}) is the same as uWS::App() when compiled without SSL support */
int main() {
us_listen_socket_t * listenSocket = nullptr;
auto before = steady_clock::now();
/* Overly simple hello world app */
uWS::SSLApp({
.key_file_name = "../misc/key.pem",
.cert_file_name = "../misc/cert.pem",
.passphrase = "1234"
}).get("/*", [&listenSocket, &before](auto *res, auto *req) {
res->end("hi there");
std::cout << req->getUrl() << std::endl;
if (req->getUrl()[1] == 'q') {
std::cout << "will shutdown" << std::endl;
std::thread shutdown([&listenSocket, &before] {
std::cout << "yes, will shutdown" << std::endl;
std::this_thread::sleep_for(200ms);
std::cout << "shutting down " << listenSocket << std::endl;
before = steady_clock::now();
us_listen_socket_close(1, listenSocket);
std::cout << "closed in " << duration_cast<milliseconds>(steady_clock::now() - before).count() << "ms" << std::endl;
});
shutdown.detach();
}
}).listen(3000, [&listenSocket](auto *token) {
if (token) {
std::cout << "token " << token << std::endl;
std::cout << "Listening on port " << 3000 << std::endl;
}
listenSocket = token;
}).run();
std::cout << "finished in " << duration_cast<milliseconds>(steady_clock::now() - before).count() << "ms" << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment