MINOR: applet: Add function to release appctx on error during init stage

appctx_free_on_early_error() must be used to release a freshly created
frontend appctx if an error occurred during the init stage. It takes care to
release the stream instead of the appctx if it exists. For a backend appctx,
it just calls appctx_free().
This commit is contained in:
Christopher Faulet 2022-05-12 15:18:48 +02:00
parent 8718c95c0a
commit d0c4ec04b8
2 changed files with 18 additions and 1 deletions

View File

@ -42,6 +42,7 @@ 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);
void appctx_free_on_early_error(struct appctx *appctx);
/* Helper function to call .init applet callback function, if it exists. Returns 0
* on success and -1 on error.

View File

@ -81,7 +81,8 @@ struct appctx *appctx_new(struct applet *applet, struct cs_endpoint *endp)
* 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.
* the stream must be released instead of the appctx. To be sure,
* appctx_free_on_early_error() must be called in this case.
*/
int appctx_finalize_startup(struct appctx *appctx, struct proxy *px, struct buffer *input)
{
@ -100,6 +101,21 @@ int appctx_finalize_startup(struct appctx *appctx, struct proxy *px, struct buff
return 0;
}
/* Release function to call when an error occurred during init stage of a
* frontend appctx. For a backend appctx, it just calls appctx_free()
*/
void appctx_free_on_early_error(struct appctx *appctx)
{
/* If a frontend apctx is attached to a conn-stream, release the stream
* instead of the appctx.
*/
if (!(appctx->endp->flags & CS_EP_ORPHAN) && !(appctx_cs(appctx)->flags & CS_FL_ISBACK)) {
stream_free(appctx_strm(appctx));
return;
}
appctx_free(appctx);
}
/* 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