]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: ring/sink: use applet_append_line()/syslog_applet_append_event() for readers
authorWilly Tarreau <w@1wt.eu>
Tue, 27 Feb 2024 14:55:26 +0000 (15:55 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 25 Mar 2024 17:34:19 +0000 (17:34 +0000)
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.

src/ring.c
src/sink.c

index 849221ee4502ab949c17e9b09de4afc72d6bc25e..682bce8191e9811994e908939ae8227e2a3df4a6 100644 (file)
@@ -347,6 +347,7 @@ int cli_io_handler_show_ring(struct appctx *appctx)
        size_t last_ofs;
        uint64_t msg_len;
        size_t len, cnt;
+       ssize_t copied;
        int ret;
 
        /* FIXME: Don't watch the other side !*/
@@ -396,18 +397,14 @@ int cli_io_handler_show_ring(struct appctx *appctx)
                cnt += len;
                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 */
                        ofs += cnt + msg_len;
                        continue;
                }
-
-               chunk_reset(&trash);
-               len = b_getblk(buf, trash.area, msg_len, ofs + cnt);
-               trash.data += len;
-               trash.area[trash.data++] = '\n';
-
-               if (applet_putchk(appctx, &trash) == -1) {
+               else if (copied == -1) {
+                       /* output full */
                        ret = 0;
                        break;
                }
index f5347269ac164e3dff4a7b4f246ba0ccb409b75a..dcbe61e38ef0ebac8356e6dd5637ec8d9b8b887b 100644 (file)
@@ -358,6 +358,7 @@ static void sink_forward_io_handler(struct appctx *appctx)
        struct buffer *buf = &ring->buf;
        uint64_t msg_len;
        size_t len, cnt, ofs, last_ofs;
+       ssize_t copied;
        int ret = 0;
 
        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;
                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 */
                        ofs += cnt + msg_len;
                        continue;
                }
-
-               chunk_reset(&trash);
-               len = b_getblk(buf, trash.area, msg_len, ofs + cnt);
-               trash.data += len;
-               trash.area[trash.data++] = '\n';
-
-               if (applet_putchk(appctx, &trash) == -1) {
+               else if (copied == -1) {
+                       /* output full */
                        ret = 0;
                        break;
                }
@@ -490,7 +487,7 @@ static void sink_forward_oc_io_handler(struct appctx *appctx)
        uint64_t msg_len;
        size_t len, cnt, ofs, last_ofs;
        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))))
                goto out;
@@ -556,22 +553,14 @@ static void sink_forward_oc_io_handler(struct appctx *appctx)
                cnt += len;
                BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
 
-               chunk_reset(&trash);
-               p = ulltoa(msg_len, trash.area, b_size(&trash));
-               if (p) {
-                       trash.data = (p - trash.area) + 1;
-                       *p = ' ';
-               }
-
-               if (!p || (trash.data + msg_len > b_size(&trash))) {
+               copied = syslog_applet_append_event(appctx, buf, ofs + cnt, msg_len);
+               if (copied == -2) {
                        /* too large a message to ever fit, let's skip it */
                        ofs += cnt + msg_len;
                        continue;
                }
-
-               trash.data += b_getblk(buf, p + 1, msg_len, ofs + cnt);
-
-               if (applet_putchk(appctx, &trash) == -1) {
+               else if (copied == -1) {
+                       /* output full */
                        ret = 0;
                        break;
                }