On Monday, 30 October 2017 at 17:23:02 UTC, Daniel Kozak wrote:
Maybe this one:
import vibe.d;
import std.regex;
import std.array : appender;
static reg = ctRegex!"^/greeting/([a-z]+)$";
void main()
{
setupWorkerThreads(logicalProcessorCount);
runWorkerTaskDist(&runServer);
runApplication();
}
void runServer()
{
auto settings = new HTTPServerSettings;
settings.options |= HTTPServerOption.reusePort;
settings.port = 3000;
settings.serverString = null;
listenHTTP(settings, &handleRequest);
}
void handleRequest(HTTPServerRequest req,
HTTPServerResponse res)
{
switch(req.path)
{
case "/": res.writeBody("Hello World", "text/plain");
break;
default:
auto m = matchFirst(req.path, reg);
string message = "Hello, ";
auto app = appender(message);
app.reserve(32);
app ~= m[1];
res.writeBody(app.data, "text/plain");
}
}
On Mon, Oct 30, 2017 at 5:41 PM, ade90036 via Digitalmars-d < digitalmars-d@puremagic.com> wrote:
On Thursday, 21 September 2017 at 13:09:33 UTC, Daniel Kozak wrote:
wrong version, this is my letest version: https://paste.ofcode.org/qWsQi kdhKiAywgBpKwANFRHi, would it be possible to re-share the example of vibe.d woth multithreaded support.
On Thu, Sep 21, 2017 at 3:01 PM, Daniel Kozak <kozzi11@gmail.com> wrote:
my version: https://paste.ofcode.org/RLX7GM6SHh3DjBBHd7wshj
On Thu, Sep 21, 2017 at 2:50 PM, Sönke Ludwig via Digitalmars-d < digitalmars-d@puremagic.com> wrote:
Am 21.09.2017 um 14:41 schrieb Vadim Lopatin:
[...]
Oh, sorry, I forgot the reusePort option, so that multiple sockets can listen on the same port:
auto settings = new HTTPServerSettings("0.0.0.0:3000 ");
settings.options |= HTTPServerOption.reusePort;
listenHTTP(settings, &handleRequest);
The pastebin link has expired and the pull request doesnt have the latest version.
Thanks
Ade
With vibe.d 0.8.2, even when multiple worker threads are setup, only one thread handles the requests:
```
import core.thread;
import vibe.d;
import std.experimental.all;
auto reg = ctRegex!"^/greeting/([a-z]+)$";
void main()
{
writefln("Master %d is running", getpid());
setupWorkerThreads(logicalProcessorCount + 1);
runWorkerTaskDist(&runServer);
runApplication();
}
void runServer()
{
auto settings = new HTTPServerSettings;
settings.options |= HTTPServerOption.reusePort;
settings.port = 8080;
settings.bindAddresses = ["127.0.0.1"];
listenHTTP(settings, &handleRequest);
}
void handleRequest(HTTPServerRequest req,
HTTPServerResponse res)
{
writeln("My Thread Id: ", to!string(thisThreadID));
// simulate long runnig task
Thread.sleep(dur!("seconds")(3));
if (req.path == "/")
res.writeBody("Hello, World! from " ~ to!string(thisThreadID), "text/plain");
else if (auto m = matchFirst(req.path, reg))
res.writeBody("Hello, " ~ m[1] ~ " from " ~ to!string(thisThreadID), "text/plain");
}
```
That could be the reason for slowness.