NIO+Multithreaded TCPSocket listener, very low cpu utilisation

Daniel Kozak kozzi11 at gmail.com
Wed Nov 15 14:22:51 UTC 2017


And this one
https://paste.ofcode.org/KNqxcrmACLZLseB45MvwC

Here you can test if threads makes difference
when compile with:
    dmd -O -release -version=SINGLE_THREAD xxx.d
it will use only one thread
when compile with:
    dmd -O -release xxx.d
it will use thread pool

On Wed, Nov 15, 2017 at 2:31 PM, Daniel Kozak <kozzi11 at gmail.com> wrote:

> This one works ok for me, but I am on linux:
> https://dpaste.dzfl.pl/f54decee45bc
>
> On Wed, Nov 15, 2017 at 12:46 PM, Daniel Kozak <kozzi11 at gmail.com> wrote:
>
>> Do not use your own taskPool, just use global taskPool proerty (import
>> std.parallelism: taskPool).
>>
>> You should not set blocking to false. And dont use Thread here.  There is
>> no reason to do that.  Just move that code into the main
>>
>> Dne 15. 11. 2017 12:15 odp. napsal uživatel "ade90036 via
>> Digitalmars-d-learn" <digitalmars-d-learn at puremagic.com>:
>>
>> So thanks for the suggestions,  i have fixed HTTP response not postman
>> cal also parse the headers correctly!! happy days.
>>
>> I have removed the duration from the Socket.select but the application
>> seems to process a bunch or requests and then it stalls for several seconds
>> (3/5) and then it resumes.
>>
>> The httpclinet which i'm using to test the application is reporting:
>> "connection timeout".
>>
>> Could this be caused by the GC?
>>
>> ```updated code
>>
>> import std.algorithm : remove;
>> import std.conv : to;
>> import core.thread: Thread;
>> import std.socket : InternetAddress, Socket, SocketException, SocketSet,
>> TcpSocket, SocketShutdown;
>> import core.time : Duration, dur;
>> import std.stdio : writeln, writefln;
>> import std.parallelism : task, TaskPool;
>>
>> string to_retlf (string s)
>> {
>>    import std.algorithm;
>>    import std.string;
>>    return s
>>       .lineSplitter
>>       .map!(a => chomp (a))
>>       .join ("\r\n");
>> }
>>
>> void main(string[] args)
>> {
>>     ushort port;
>>
>>     if (args.length >= 2)
>>         port = to!ushort(args[1]);
>>     else
>>         port = 4444;
>>
>>
>>     auto listener = new TcpSocket();
>>     assert(listener.isAlive);
>>     listener.blocking = false;
>>     listener.bind(new InternetAddress(port));
>>     listener.listen(100);
>>     writefln("Listening on port %d.", port);
>>
>>     auto taskPool = new TaskPool(8);
>>
>>     string response = "HTTP/1.1 200 OK
>>
>> Server: dland:v2.076.1
>> Date: Tue, 11 Nov 2017 15:56:02 GMT
>> Content-Type: text/html; charset=UTF-8
>> Content-Length: 51
>>
>> <html><head></head><body>Hello World!</body></html>".to_retlf;
>>
>>
>>     new Thread({
>>         auto listeningSet = new SocketSet();
>>         while(true) {
>>             listeningSet.add(listener);
>>             if (Socket.select(listeningSet, null, null)) {
>>
>>                 if (listeningSet.isSet(listener))        // connection
>> request
>>                 {
>>                     Socket socket = listener.accept();
>>                     assert(socket.isAlive);
>>
>>                     //writefln("Connection from %s established.",
>> socket.remoteAddress().toString());
>>                     auto task = task!handle_socket(socket, response);
>>                     taskPool.put(task);
>>                 }
>>             }
>>             listeningSet.reset();
>>         }
>>     }).start();
>> }
>>
>>
>>
>> void handle_socket(Socket socket, string response) {
>>
>>     auto socketSet = new SocketSet();
>>     while(true) {
>>         socketSet.add(socket);
>>         if (Socket.select(socketSet, null, null)) {
>>
>>             char[1024] buf;
>>             auto datLength = socket.receive(buf[]);
>>
>>             if (datLength == Socket.ERROR)
>>                 writeln("Connection error.");
>>             else if (datLength != 0)
>>             {
>>                 //writefln("Received %d bytes from %s: \"%s\"",
>> datLength, socket.remoteAddress().toString(), buf[0..datLength]);
>>                 //writefln("Writing response");
>>                 socket.send(response);
>>
>>             }
>>             // release socket resources now
>>
>>             socket.close();
>>
>>             break;
>>
>>         }
>>         socketSet.reset();
>>     }
>> ```
>>
>> Regards
>>
>>
>>
>>
>>
>>
>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20171115/c6bddd06/attachment.html>


More information about the Digitalmars-d-learn mailing list