From cb78acb865dbda84a34261bfd926bede522c489e Mon Sep 17 00:00:00 2001 From: zhenwei pi Date: Sun, 28 May 2023 13:35:27 +0800 Subject: [PATCH] Support maxiov per connection type (#12234) Rather than a fixed iovcnt for connWritev, support maxiov per connection type instead. A minor change to reduce memory for struct connection. Signed-off-by: zhenwei pi --- src/connection.h | 5 +++-- src/networking.c | 5 +++-- src/socket.c | 1 + src/tls.c | 1 + src/unix.c | 1 + 5 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/connection.h b/src/connection.h index 7c26ac635..470327c66 100644 --- a/src/connection.h +++ b/src/connection.h @@ -114,14 +114,15 @@ typedef struct ConnectionType { struct connection { ConnectionType *type; ConnectionState state; + int last_errno; + int fd; short int flags; short int refs; - int last_errno; + unsigned short int iovcnt; void *private_data; ConnectionCallbackFunc conn_handler; ConnectionCallbackFunc write_handler; ConnectionCallbackFunc read_handler; - int fd; }; #define CONFIG_BINDADDR_MAX 16 diff --git a/src/networking.c b/src/networking.c index 25d2b4c34..89bc56056 100644 --- a/src/networking.c +++ b/src/networking.c @@ -1787,8 +1787,9 @@ client *lookupClientByID(uint64_t id) { * and 'nwritten' is an output parameter, it means how many bytes server write * to client. */ static int _writevToClient(client *c, ssize_t *nwritten) { - struct iovec iov[IOV_MAX]; int iovcnt = 0; + int iovmax = min(IOV_MAX, c->conn->iovcnt); + struct iovec iov[iovmax]; size_t iov_bytes_len = 0; /* If the static reply buffer is not empty, * add it to the iov array for writev() as well. */ @@ -1804,7 +1805,7 @@ static int _writevToClient(client *c, ssize_t *nwritten) { listNode *next; clientReplyBlock *o; listRewind(c->reply, &iter); - while ((next = listNext(&iter)) && iovcnt < IOV_MAX && iov_bytes_len < NET_MAX_WRITES_PER_EVENT) { + while ((next = listNext(&iter)) && iovcnt < iovmax && iov_bytes_len < NET_MAX_WRITES_PER_EVENT) { o = listNodeValue(next); if (o->used == 0) { /* empty node, just release it and skip. */ c->reply_bytes -= o->size; diff --git a/src/socket.c b/src/socket.c index 0cfb2e4ec..dad8e93cc 100644 --- a/src/socket.c +++ b/src/socket.c @@ -78,6 +78,7 @@ static connection *connCreateSocket(void) { connection *conn = zcalloc(sizeof(connection)); conn->type = &CT_Socket; conn->fd = -1; + conn->iovcnt = IOV_MAX; return conn; } diff --git a/src/tls.c b/src/tls.c index 94cba63f1..e709c9930 100644 --- a/src/tls.c +++ b/src/tls.c @@ -462,6 +462,7 @@ static connection *createTLSConnection(int client_side) { tls_connection *conn = zcalloc(sizeof(tls_connection)); conn->c.type = &CT_TLS; conn->c.fd = -1; + conn->c.iovcnt = IOV_MAX; conn->ssl = SSL_new(ctx); return (connection *) conn; } diff --git a/src/unix.c b/src/unix.c index 73e56d4a0..bd146d024 100644 --- a/src/unix.c +++ b/src/unix.c @@ -78,6 +78,7 @@ static connection *connCreateUnix(void) { connection *conn = zcalloc(sizeof(connection)); conn->type = &CT_Unix; conn->fd = -1; + conn->iovcnt = IOV_MAX; return conn; }