]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: stream/cli: remove the unneeded init state from "show sess"
authorWilly Tarreau <w@1wt.eu>
Tue, 3 May 2022 09:05:39 +0000 (11:05 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 6 May 2022 16:13:35 +0000 (18:13 +0200)
This state was only used to preset the list element. Now that we can
guarantee that the context can be properly preset during the parsing
we don't need this state anymore. The first pointer has to be set to
point to the first stream during the initial call which is detected
by the pointer not yet being set (null). Thanks to this we can also
remove one state check on the abort path.

src/stream.c

index 8409863066b6377e5b146532b7e8874aeb31703e..f8f9eefaebe4bf51ce2c9f4b42871e8380de9242 100644 (file)
@@ -3155,8 +3155,7 @@ struct show_sess_ctx {
        int section;            /* section of the session being dumped */
        int pos;                /* last position of the current session's buffer */
        enum {
-               STATE_INIT = 0,
-               STATE_LIST,
+               STATE_LIST = 0,
                STATE_FIN,
        } state;                /* dump state */
 };
@@ -3521,6 +3520,11 @@ static int cli_parse_show_sess(char **args, char *payload, struct appctx *appctx
        ctx->pos = 0;
        ctx->thr = 0;
 
+       /* The back-ref must be reset, it will be detected and set by
+        * the dump code upon first invocation.
+        */
+       LIST_INIT(&ctx->bref.users);
+
        /* let's set our own stream's epoch to the current one and increment
         * it so that we know which streams were already there before us.
         */
@@ -3545,11 +3549,9 @@ static int cli_io_handler_dump_sess(struct appctx *appctx)
                /* If we're forced to shut down, we might have to remove our
                 * reference to the last stream being dumped.
                 */
-               if (ctx->state == STATE_LIST) {
-                       if (!LIST_ISEMPTY(&ctx->bref.users)) {
-                               LIST_DELETE(&ctx->bref.users);
-                               LIST_INIT(&ctx->bref.users);
-                       }
+               if (!LIST_ISEMPTY(&ctx->bref.users)) {
+                       LIST_DELETE(&ctx->bref.users);
+                       LIST_INIT(&ctx->bref.users);
                }
                goto done;
        }
@@ -3557,24 +3559,14 @@ static int cli_io_handler_dump_sess(struct appctx *appctx)
        chunk_reset(&trash);
 
        switch (ctx->state) {
-       case STATE_INIT:
-               /* the function had not been called yet, let's prepare the
-                * buffer for a response. We initialize the current stream
-                * pointer to the first in the global list. When a target
-                * stream is being destroyed, it is responsible for updating
-                * this pointer. We know we have reached the end when this
-                * pointer points back to the head of the streams list.
-                */
-               LIST_INIT(&ctx->bref.users);
-               ctx->bref.ref = ha_thread_ctx[ctx->thr].streams.n;
-               ctx->state = STATE_LIST;
-               /* fall through */
-
        case STATE_LIST:
                /* first, let's detach the back-ref from a possible previous stream */
                if (!LIST_ISEMPTY(&ctx->bref.users)) {
                        LIST_DELETE(&ctx->bref.users);
                        LIST_INIT(&ctx->bref.users);
+               } else if (!ctx->bref.ref) {
+                       /* first call, start with first stream */
+                       ctx->bref.ref = ha_thread_ctx[ctx->thr].streams.n;
                }
 
                /* and start from where we stopped */