MINOR: ring: rename totlen vs msglen in ring_write()

The ring_write() function uses confusing variable names: totlen is in
fact the length of the message, not the total length that is going to
be written. Let's rename it msglen and have a real "needed" that
corresponds to the total size we're going to write. We also add a
BUG_ON_HOT() to catch mistakes causing discrepancies.
This commit is contained in:
Willy Tarreau 2024-02-26 20:03:20 +01:00
parent c222cb8389
commit ee1c92cf10

View File

@ -175,8 +175,9 @@ ssize_t ring_write(struct ring *ring, size_t maxlen, const struct ist pfx[], siz
{ {
struct buffer *buf = &ring->buf; struct buffer *buf = &ring->buf;
struct appctx *appctx; struct appctx *appctx;
size_t totlen = 0; size_t msglen = 0;
size_t lenlen; size_t lenlen;
size_t needed;
uint64_t dellen; uint64_t dellen;
int dellenlen; int dellenlen;
ssize_t sent = 0; ssize_t sent = 0;
@ -191,20 +192,27 @@ ssize_t ring_write(struct ring *ring, size_t maxlen, const struct ist pfx[], siz
* copying due to the varint encoding of the length. * copying due to the varint encoding of the length.
*/ */
for (i = 0; i < npfx; i++) for (i = 0; i < npfx; i++)
totlen += pfx[i].len; msglen += pfx[i].len;
for (i = 0; i < nmsg; i++) for (i = 0; i < nmsg; i++)
totlen += msg[i].len; msglen += msg[i].len;
if (totlen > maxlen) if (msglen > maxlen)
totlen = maxlen; msglen = maxlen;
lenlen = varint_bytes(totlen); lenlen = varint_bytes(msglen);
/* We need:
* - lenlen bytes for the size encoding
* - msglen for the message
* - one byte for the new marker
*/
needed = lenlen + msglen + 1;
HA_RWLOCK_WRLOCK(RING_LOCK, &ring->lock); HA_RWLOCK_WRLOCK(RING_LOCK, &ring->lock);
if (lenlen + totlen + 1 + 1 > b_size(buf)) if (needed + 1 > b_size(buf))
goto done_buf; goto done_buf;
while (b_room(buf) < lenlen + totlen + 1) { while (b_room(buf) < needed) {
/* we need to delete the oldest message (from the end), /* we need to delete the oldest message (from the end),
* and we have to stop if there's a reader stuck there. * and we have to stop if there's a reader stuck there.
* Unless there's corruption in the buffer it's guaranteed * Unless there's corruption in the buffer it's guaranteed
@ -223,31 +231,32 @@ ssize_t ring_write(struct ring *ring, size_t maxlen, const struct ist pfx[], siz
} }
/* OK now we do have room */ /* OK now we do have room */
__b_put_varint(buf, totlen); __b_put_varint(buf, msglen);
totlen = 0; msglen = 0;
for (i = 0; i < npfx; i++) { for (i = 0; i < npfx; i++) {
size_t len = pfx[i].len; size_t len = pfx[i].len;
if (len + totlen > maxlen) if (len + msglen > maxlen)
len = maxlen - totlen; len = maxlen - msglen;
if (len) if (len)
__b_putblk(buf, pfx[i].ptr, len); __b_putblk(buf, pfx[i].ptr, len);
totlen += len; msglen += len;
} }
for (i = 0; i < nmsg; i++) { for (i = 0; i < nmsg; i++) {
size_t len = msg[i].len; size_t len = msg[i].len;
if (len + totlen > maxlen) if (len + msglen > maxlen)
len = maxlen - totlen; len = maxlen - msglen;
if (len) if (len)
__b_putblk(buf, msg[i].ptr, len); __b_putblk(buf, msg[i].ptr, len);
totlen += len; msglen += len;
} }
*b_tail(buf) = 0; buf->data++; // new read counter *b_tail(buf) = 0; buf->data++; // new read counter
sent = lenlen + totlen + 1; sent = lenlen + msglen + 1;
BUG_ON_HOT(sent != needed);
/* notify potential readers */ /* notify potential readers */
list_for_each_entry(appctx, &ring->waiters, wait_entry) list_for_each_entry(appctx, &ring->waiters, wait_entry)