sustrik / libdill

Structured concurrency in C
MIT License
1.68k stars 155 forks source link

Something wrong in libdill Tutorial - http://libdill.org/tutorial.html #79

Closed winimi closed 7 years ago

winimi commented 7 years ago

Step 4: Deadlines

File descriptors can be a scarce resource. If a client connects to greetserver and lets the dialogue hang without entering the name, one file descriptor on the server side is, for all practical purposes, wasted.

To deal with the problem we are going to timeout the whole client/server dialogue. If it takes more than 10 seconds, server will kill the connection straight away.

One thing to note is that libdill uses deadlines rather than more conventional timeouts. In other words, you specify the time instant when you want the operation to finish rather than maximum time it should take to run. To construct deadlines easily, libdill provides now() function. The deadline is expressed in milliseconds so you can create a deadline 10 seconds in the future like this:

int64_t deadline = now() + 10000; Further, you have to modify all the potentially blocking function calls in the program to take the deadline parameter. For example:

int rc = msend(s, "What's your name?", 17, deadline); if(rc != 0) goto cleanup; Note that errno is set to ETIMEDOUT in case the deadline is reached. However, we treat all the errors in the same way (by closing the connection) and thus we don't have to do any specific provisions for the deadline case.


If a client connects to greetserver and lets the dialogue hang without entering the name,

I think it should set deadline here, in mrecv not msend in the tutorial.

sz = **mrecv**(s, inbuf, sizeof(inbuf), **deadline**);
if(sz < 0)       goto cleanup;
sustrik commented 7 years ago

It was meant just as an example. In fact, you need to add deadline to both send and receive. I've fixed the tutorial. Thanks for the report!