CLEANUP: server: a separate function for initializing the per_thr field

To avoid repeating the same source code, allocating memory and initializing
the per_thr field from the server structure is transferred to a separate
function.
This commit is contained in:
Miroslav Zagorac 2021-06-15 15:33:20 +02:00 committed by Willy Tarreau
parent cbc0c232bf
commit 8a8f270f6a
4 changed files with 26 additions and 29 deletions

View File

@ -60,6 +60,7 @@ int srv_init_addr(void);
struct server *cli_find_server(struct appctx *appctx, char *arg);
struct server *new_server(struct proxy *proxy);
void free_server(struct server *srv);
int srv_init_per_thr(struct server *srv);
/* functions related to server name resolution */
int srv_prepare_for_resolution(struct server *srv, const char *hostname);

View File

@ -3832,21 +3832,13 @@ out_uri_auth_compat:
list_for_each_entry(newsrv, &servers_list, global_list) {
/* initialize idle conns lists */
newsrv->per_thr = calloc(global.nbthread, sizeof(*newsrv->per_thr));
if (!newsrv->per_thr) {
if (srv_init_per_thr(newsrv) == -1) {
ha_alert("parsing [%s:%d] : failed to allocate per-thread lists for server '%s'.\n",
newsrv->conf.file, newsrv->conf.line, newsrv->id);
cfgerr++;
continue;
}
for (i = 0; i < global.nbthread; i++) {
newsrv->per_thr[i].idle_conns = EB_ROOT;
newsrv->per_thr[i].safe_conns = EB_ROOT;
newsrv->per_thr[i].avail_conns = EB_ROOT;
MT_LIST_INIT(&newsrv->per_thr[i].streams);
}
if (newsrv->max_idle_conns != 0) {
newsrv->curr_idle_thr = calloc(global.nbthread, sizeof(*newsrv->curr_idle_thr));
if (!newsrv->curr_idle_thr) {

View File

@ -4326,6 +4326,27 @@ static int srv_alloc_lb(struct server *sv, struct proxy *be)
return 1;
}
/* Memory allocation and initialization of the per_thr field.
* Returns 0 if the field has been successfully initialized, -1 on failure.
*/
int srv_init_per_thr(struct server *srv)
{
int i;
srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
if (!srv->per_thr)
return -1;
for (i = 0; i < global.nbthread; i++) {
srv->per_thr[i].idle_conns = EB_ROOT;
srv->per_thr[i].safe_conns = EB_ROOT;
srv->per_thr[i].avail_conns = EB_ROOT;
MT_LIST_INIT(&srv->per_thr[i].streams);
}
return 0;
}
/* Parse a "add server" command
* Returns 0 if the server has been successfully initialized, 1 on failure.
*/
@ -4335,7 +4356,7 @@ static int cli_parse_add_server(char **args, char *payload, struct appctx *appct
struct server *srv;
char *be_name, *sv_name;
int errcode, argc;
int next_id, i;
int next_id;
const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR;
usermsgs_clr("CLI");
@ -4405,19 +4426,11 @@ static int cli_parse_add_server(char **args, char *payload, struct appctx *appct
}
}
srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
if (!srv->per_thr) {
if (srv_init_per_thr(srv) == -1) {
ha_alert("failed to allocate per-thread lists for server.\n");
goto out;
}
for (i = 0; i < global.nbthread; i++) {
srv->per_thr[i].idle_conns = EB_ROOT;
srv->per_thr[i].safe_conns = EB_ROOT;
srv->per_thr[i].avail_conns = EB_ROOT;
MT_LIST_INIT(&srv->per_thr[i].streams);
}
if (srv->max_idle_conns != 0) {
srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr));
if (!srv->curr_idle_thr) {

View File

@ -939,7 +939,6 @@ struct sink *sink_new_from_logsrv(struct logsrv *logsrv)
struct sink *sink = NULL;
struct server *srv = NULL;
struct sink_forward_target *sft = NULL;
int i;
/* allocate new proxy to handle
* forward to a stream server
@ -971,17 +970,9 @@ struct sink *sink_new_from_logsrv(struct logsrv *logsrv)
HA_SPIN_INIT(&srv->lock);
/* process per thread init */
srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
if (!srv->per_thr)
if (srv_init_per_thr(srv) == -1)
goto error;
for (i = 0; i < global.nbthread; i++) {
srv->per_thr[i].idle_conns = EB_ROOT;
srv->per_thr[i].safe_conns = EB_ROOT;
srv->per_thr[i].avail_conns = EB_ROOT;
MT_LIST_INIT(&srv->per_thr[i].streams);
}
/* the servers are linked backwards
* first into proxy
*/