]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: applet: Add a function to finalize frontend appctx startup
authorChristopher Faulet <cfaulet@haproxy.com>
Thu, 12 May 2022 13:15:53 +0000 (15:15 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Tue, 17 May 2022 14:13:21 +0000 (16:13 +0200)
appctx_finalize_startup() may be used to finalize the frontend appctx
startup. It is responsible to create the appctx's session and the frontend
conn-stream. On error, it is the caller responsibility to release the
appctx. However, the session is released if it was created. On success, if
an error is encountered in the caller function, the stream must be released
instead of the appctx.

This function should ease the init stage when new appctx is created.

include/haproxy/applet.h
src/applet.c

index bef5aa5e603181258e3cde497f105e5d15313870..b9c581d369c384c42be54757162fb4ca87c90c5e 100644 (file)
@@ -41,6 +41,7 @@ void *applet_reserve_svcctx(struct appctx *appctx, size_t size);
 void appctx_shut(struct appctx *appctx);
 
 struct appctx *appctx_new(struct applet *applet, struct cs_endpoint *endp);
+int appctx_finalize_startup(struct appctx *appctx, struct proxy *px, struct buffer *input);
 
 /* Helper function to call .init applet callback function, if it exists. Returns 0
  * on success and -1 on error.
index b7711b1bdda5459e15bed7df893c446c7b966433..4bb345bcbffa4ca9ab8c778644a1b04e93565e93 100644 (file)
@@ -74,6 +74,32 @@ struct appctx *appctx_new(struct applet *applet, struct cs_endpoint *endp)
        return NULL;
 }
 
+/* Finalize the frontend appctx startup. It must not be called for a backend
+ * appctx. This function is responsible to create the appctx's session and the
+ * frontend conn-stream. By transitivity, the stream is also created.
+ *
+ * It returns 0 on success and -1 on error. In this case, it is the caller
+ * responsibility to release the appctx. However, the session is released if it
+ * was created. On success, if an error is encountered in the caller function,
+ * the stream must be released instead of the appctx.
+ */
+int appctx_finalize_startup(struct appctx *appctx, struct proxy *px, struct buffer *input)
+{
+       struct session *sess;
+
+       BUG_ON(appctx->sess || !(appctx->endp->flags & CS_EP_ORPHAN));
+
+       sess = session_new(px, NULL, &appctx->obj_type);
+       if (!sess)
+               return -1;
+       if (!cs_new_from_endp(appctx->endp, sess, input)) {
+               session_free(sess);
+               return -1;
+       }
+       appctx->sess = sess;
+       return 0;
+}
+
 /* reserves a command context of at least <size> bytes in the <appctx>, for
  * use by a CLI command or any regular applet. The pointer to this context is
  * stored in ctx.svcctx and is returned. The caller doesn't need to release