www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Socket - handling large numbers of incoming connections

reply Jakob Jenkov <jakob jenkov.com> writes:
What is the fastest / most scalable way to implement a server 
(using a Socket) which can handle large numbers of incoming 
connections? Like, at least 10K, but probably up to 1 million 
connections.

More specifically:

1) How do I efficiently select the connections (client Socket 
instances) which have data which is ready to read?

2) How do I efficiently select the connection that are ready to 
accept data sent to them?
(which are write ready - in other words) ?


I read in the D Cookbook that using the SocketSet is not the 
fastest way to do this, as it has to iterate through all Socket 
instances in it, and check a flag on each Socket.
Dec 21 2015
parent reply Stefan <dl ng.rocks> writes:
How about https://github.com/dcarp/asynchronous ? Asyncio Socket 
handling is sometimes quite nice. It's performance is okay for 
nearly no effort and the code looks clean.
Details here: 
http://dcarp.github.io/asynchronous/asynchronous/streams/startServer.html

vibe.d also offers a fiber based asyncio way of dealing with 
sockets.
http://vibed.org/docs#tcp-server

Maybe it fits your needs.
Dec 21 2015
parent reply Jakob Jenkov <jakob jenkov.com> writes:
On Monday, 21 December 2015 at 20:20:44 UTC, Stefan wrote:
 How about https://github.com/dcarp/asynchronous ? Asyncio 
 Socket handling is sometimes quite nice. It's performance is 
 okay for nearly no effort and the code looks clean.
 Details here: 
 http://dcarp.github.io/asynchronous/asynchronous/streams/startServer.html

 vibe.d also offers a fiber based asyncio way of dealing with 
 sockets.
 http://vibed.org/docs#tcp-server

 Maybe it fits your needs.
Thanks - but I am primarily looking for a solution without external frameworks. Frameworks have a way of bloating over time.
Dec 21 2015
next sibling parent reply tcak <1ltkrs+3wyh1ow7kzn1k sharklasers.com> writes:
On Monday, 21 December 2015 at 20:53:14 UTC, Jakob Jenkov wrote:
 On Monday, 21 December 2015 at 20:20:44 UTC, Stefan wrote:
 How about https://github.com/dcarp/asynchronous ? Asyncio 
 Socket handling is sometimes quite nice. It's performance is 
 okay for nearly no effort and the code looks clean.
 Details here: 
 http://dcarp.github.io/asynchronous/asynchronous/streams/startServer.html

 vibe.d also offers a fiber based asyncio way of dealing with 
 sockets.
 http://vibed.org/docs#tcp-server

 Maybe it fits your needs.
Thanks - but I am primarily looking for a solution without external frameworks. Frameworks have a way of bloating over time.
My server uses "poll" for that.
Dec 21 2015
parent reply Jakob Jenkov <jakob jenkov.com> writes:
 My server uses "poll" for that.
Okay, how does that work? How do I use "poll" in D? Link? Code example?
Dec 21 2015
parent reply Adrian Matoga <epi atari8.info> writes:
On Monday, 21 December 2015 at 21:32:55 UTC, Jakob Jenkov wrote:
 My server uses "poll" for that.
Okay, how does that work? How do I use "poll" in D? Link? Code example?
The same as in C [1]. Just change #include <poll.h> to import core.sys.posix.poll; [1] http://linux.die.net/man/2/poll
Dec 21 2015
parent Jakob Jenkov <jakob jenkov.com> writes:
 The same as in C [1].
 Just change
 #include <poll.h>
 to
 import core.sys.posix.poll;

 [1] http://linux.die.net/man/2/poll
I have a background in Java, so I am a bit handicapped :-)
Dec 22 2015
prev sibling parent reply Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn writes:
V Mon, 21 Dec 2015 20:53:14 +0000
Jakob Jenkov via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> napsáno:

 On Monday, 21 December 2015 at 20:20:44 UTC, Stefan wrote:
 How about https://github.com/dcarp/asynchronous ? Asyncio 
 Socket handling is sometimes quite nice. It's performance is 
 okay for nearly no effort and the code looks clean.
 Details here: 
 http://dcarp.github.io/asynchronous/asynchronous/streams/startServer.html

 vibe.d also offers a fiber based asyncio way of dealing with 
 sockets.
 http://vibed.org/docs#tcp-server

 Maybe it fits your needs.  
Thanks - but I am primarily looking for a solution without external frameworks. Frameworks have a way of bloating over time.
Use vibe.d or other library for async io (libasync). If you want to reinvent the wheel you can use kqueue(https://github.com/D-Programming-Language/druntime/blob/1f957372e5dadb92ab1d621d68232dbf8a2dbccf/src/core/sys/freebsd/sys/event.d) for bsd, epoll(https://github.com/D-Programming-Language/druntime/blob/master/src/core/sys/linux/epoll.d) for linux and "I do not use windows" for windows.
Dec 21 2015
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 21 December 2015 at 23:17:45 UTC, Daniel Kozák wrote:
 If you want to reinvent the wheel you can use
It isn't really reinventing the wheel to just use an alternate library... it isn't like the bundled functions with the OS are hard to use and you really should understand how they work anyway to write efficient programs.
Dec 21 2015
next sibling parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
V Mon, 21 Dec 2015 23:29:14 +0000
"Adam D. Ruppe via Digitalmars-d-learn"
<digitalmars-d-learn puremagic.com> napsáno:

 On Monday, 21 December 2015 at 23:17:45 UTC, Daniel Kozák wrote:
 If you want to reinvent the wheel you can use  
It isn't really reinventing the wheel to just use an alternate library...
I guess you are right
 it isn't like the bundled functions with the OS are 
 hard to use and you really should understand how they work anyway 
I agree, if something go wrong it is a nice to know why
 to write efficient programs.
I can write efficient programs with vibe.d(libevent,libuv,libasync...) too :).
Dec 21 2015
prev sibling next sibling parent Johannes Pfau <nospam example.com> writes:
Am Mon, 21 Dec 2015 23:29:14 +0000
schrieb Adam D. Ruppe <destructionator gmail.com>:

 On Monday, 21 December 2015 at 23:17:45 UTC, Daniel Koz=C3=A1k wrote:
 If you want to reinvent the wheel you can use =20
=20 [...] it isn't like the bundled functions with the OS are=20 hard to use [...] =20
epoll and similar interfaces are not difficult to use. But you need to be careful to handle all error conditions caused by low level posix io calls (read/write) correctly. (Partial reads/writes, How do you handle EINTR? How do you handle error codes returned by the close function*? ...) * http://lwn.net/Articles/576478/
Dec 22 2015
prev sibling parent Jakob Jenkov <jakob jenkov.com> writes:
Thanks, everyone, I have looked a bit at different frameworks, 
and it seems that libasync might have a decently narrow scope to 
fit what I need.

I have a background in Java, so a lot of this OS-specific stuff 
is new to me (EPoll etc.). In Java that stuff is used under the 
hood for you, without you knowing anything about it. Java just 
chooses the best option for the given OS. This is easy to use, 
but of course gives you less control.
Dec 22 2015