Add an example to test server concurrency.
This commit is contained in:
@@ -4,24 +4,47 @@
|
||||
|
||||
class HelloView : public webcc::View {
|
||||
public:
|
||||
HelloView(int sleep_seconds) : sleep_seconds_(sleep_seconds) {
|
||||
}
|
||||
|
||||
webcc::ResponsePtr Handle(webcc::RequestPtr request) override {
|
||||
if (sleep_seconds_ > 0) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds_));
|
||||
}
|
||||
|
||||
if (request->method() == "GET") {
|
||||
return webcc::ResponseBuilder{}.OK().Body("Hello, World!")();
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
private:
|
||||
int sleep_seconds_;
|
||||
};
|
||||
|
||||
int main() {
|
||||
int main(int argc, const char* argv[]) {
|
||||
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
|
||||
|
||||
int workers = 1;
|
||||
int sleep_seconds = 0;
|
||||
|
||||
if (argc > 1) {
|
||||
workers = std::stoi(argv[1]);
|
||||
if (argc > 2) {
|
||||
sleep_seconds = std::stoi(argv[2]);
|
||||
}
|
||||
}
|
||||
|
||||
LOG_USER("Workers: %d", workers);
|
||||
LOG_USER("Sleep seconds: %d", sleep_seconds);
|
||||
|
||||
try {
|
||||
webcc::Server server(8080);
|
||||
|
||||
server.Route("/", std::make_shared<HelloView>());
|
||||
server.Route("/", std::make_shared<HelloView>(sleep_seconds));
|
||||
|
||||
server.Run();
|
||||
server.Run(workers);
|
||||
|
||||
} catch (const std::exception&) {
|
||||
return 1;
|
||||
|
||||
Reference in New Issue
Block a user