MEDIUM: ring/sink: use applet_append_line()/syslog_applet_append_event() for readers

The rink reader code was duplicated as-is in 2.2 for the ring forwarding
code in commits 494c505703 ("MEDIUM: ring: add server statement to forward
messages from a ring") and 975564784f ("MEDIUM: ring: add new srv statement
to support octet counting forward") (which only differs by using a prefix
instead of a suffix to delimit messages).

Unfortunately, that makes it almost impossible to rework the core ring
code because all these parts rely on it. This first commit aims at
restoring a common structure for the core loop by just calling a distinct
function based on the use case. The functions are either
applet_append_line() when a whole line is to be emitted followed by an LF
character, or syslog_applet_appent_event() when trying to send a TCP
syslog line prepended with its size in decimal.

There is no functional change beyond this.
This commit is contained in:
Willy Tarreau 2024-02-27 15:55:26 +01:00
parent 201c706330
commit 8022ae326c
2 changed files with 15 additions and 29 deletions

View File

@ -347,6 +347,7 @@ int cli_io_handler_show_ring(struct appctx *appctx)
size_t last_ofs; size_t last_ofs;
uint64_t msg_len; uint64_t msg_len;
size_t len, cnt; size_t len, cnt;
ssize_t copied;
int ret; int ret;
/* FIXME: Don't watch the other side !*/ /* FIXME: Don't watch the other side !*/
@ -396,18 +397,14 @@ int cli_io_handler_show_ring(struct appctx *appctx)
cnt += len; cnt += len;
BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf)); BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
if (unlikely(msg_len + 1 > b_size(&trash))) { copied = applet_append_line(appctx, buf, ofs + cnt, msg_len);
if (copied == -2) {
/* too large a message to ever fit, let's skip it */ /* too large a message to ever fit, let's skip it */
ofs += cnt + msg_len; ofs += cnt + msg_len;
continue; continue;
} }
else if (copied == -1) {
chunk_reset(&trash); /* output full */
len = b_getblk(buf, trash.area, msg_len, ofs + cnt);
trash.data += len;
trash.area[trash.data++] = '\n';
if (applet_putchk(appctx, &trash) == -1) {
ret = 0; ret = 0;
break; break;
} }

View File

@ -358,6 +358,7 @@ static void sink_forward_io_handler(struct appctx *appctx)
struct buffer *buf = &ring->buf; struct buffer *buf = &ring->buf;
uint64_t msg_len; uint64_t msg_len;
size_t len, cnt, ofs, last_ofs; size_t len, cnt, ofs, last_ofs;
ssize_t copied;
int ret = 0; int ret = 0;
if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR)))) { if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR)))) {
@ -425,18 +426,14 @@ static void sink_forward_io_handler(struct appctx *appctx)
cnt += len; cnt += len;
BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf)); BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
if (unlikely(msg_len + 1 > b_size(&trash))) { copied = applet_append_line(appctx, buf, ofs + cnt, msg_len);
if (copied == -2) {
/* too large a message to ever fit, let's skip it */ /* too large a message to ever fit, let's skip it */
ofs += cnt + msg_len; ofs += cnt + msg_len;
continue; continue;
} }
else if (copied == -1) {
chunk_reset(&trash); /* output full */
len = b_getblk(buf, trash.area, msg_len, ofs + cnt);
trash.data += len;
trash.area[trash.data++] = '\n';
if (applet_putchk(appctx, &trash) == -1) {
ret = 0; ret = 0;
break; break;
} }
@ -490,7 +487,7 @@ static void sink_forward_oc_io_handler(struct appctx *appctx)
uint64_t msg_len; uint64_t msg_len;
size_t len, cnt, ofs, last_ofs; size_t len, cnt, ofs, last_ofs;
int ret = 0; int ret = 0;
char *p; ssize_t copied;
if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW))))
goto out; goto out;
@ -556,22 +553,14 @@ static void sink_forward_oc_io_handler(struct appctx *appctx)
cnt += len; cnt += len;
BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf)); BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
chunk_reset(&trash); copied = syslog_applet_append_event(appctx, buf, ofs + cnt, msg_len);
p = ulltoa(msg_len, trash.area, b_size(&trash)); if (copied == -2) {
if (p) {
trash.data = (p - trash.area) + 1;
*p = ' ';
}
if (!p || (trash.data + msg_len > b_size(&trash))) {
/* too large a message to ever fit, let's skip it */ /* too large a message to ever fit, let's skip it */
ofs += cnt + msg_len; ofs += cnt + msg_len;
continue; continue;
} }
else if (copied == -1) {
trash.data += b_getblk(buf, p + 1, msg_len, ofs + cnt); /* output full */
if (applet_putchk(appctx, &trash) == -1) {
ret = 0; ret = 0;
break; break;
} }