MEDIUM: connection: centralize handling of nolinger in fd management
Right now we see many places doing their own setsockopt(SO_LINGER).
Better only do it just before the close() in fd_delete(). For this
we add a new flag on the file descriptor, indicating if it's safe or
not to linger. If not (eg: after a connect()), then the setsockopt()
call is automatically performed before a close().
The flag automatically turns to safe when receiving a read0.
diff --git a/src/fd.c b/src/fd.c
index 5df0b45..7e69114 100644
--- a/src/fd.c
+++ b/src/fd.c
@@ -116,6 +116,11 @@
*/
void fd_delete(int fd)
{
+ if (fdtab[fd].linger_risk) {
+ /* this is generally set when connecting to servers */
+ setsockopt(fd, SOL_SOCKET, SO_LINGER,
+ (struct linger *) &nolinger, sizeof(struct linger));
+ }
if (cur_poller.clo)
cur_poller.clo(fd);