From 2aefad5df77e8f345a6e7eafbe470c984492b194 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Mon, 20 Jan 2014 11:41:52 +0100 Subject: [PATCH] MINOR: connection: add a new conn_drain() function Till now there was no way to know from a connection if a previous call to drain() had done any change. This function is used to drain incoming data and to update the connection's flags at the same time. It also correctly sets the polling flags on the connection if the drain function indicates inability to receive. This function will be used preferably over ctrl->drain() when a connection is used. --- include/proto/connection.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/include/proto/connection.h b/include/proto/connection.h index e172cfe78..7c969eb4c 100644 --- a/include/proto/connection.h +++ b/include/proto/connection.h @@ -560,6 +560,40 @@ static inline void conn_attach(struct connection *conn, void *owner, const struc conn->owner = owner; } +/* Drains possibly pending incoming data on the file descriptor attached to the + * connection and update the connection's flags accordingly. This is used to + * know whether we need to disable lingering on close. Returns non-zero if it + * is safe to close without disabling lingering, otherwise zero. The SOCK_RD_SH + * flag may also be updated if the incoming shutdown was reported by the drain() + * function. + */ +static inline int conn_drain(struct connection *conn) +{ + int ret; + + if (!conn_ctrl_ready(conn)) + return 1; + + if (conn->flags & CO_FL_SOCK_RD_SH) + return 1; + + if (conn->flags & CO_FL_WAIT_RD) + return 0; + + if (!conn->ctrl->drain) + return 0; + + ret = conn->ctrl->drain(conn->t.sock.fd); + if (ret < 0) + __conn_data_poll_recv(conn); + + if (ret <= 0) + return 0; + + conn->flags |= CO_FL_SOCK_RD_SH; + return 1; +} + /* returns a human-readable error code for conn->err_code, or NULL if the code * is unknown. */