mirror of
https://git.haproxy.org/git/haproxy.git/
synced 2025-08-07 23:56:57 +02:00
MEDIUM: tree-wide: avoid manually initializing proxies
In this patch we try to use the proxy API init functions as much as possible to avoid code redundancy and prevent proxy initialization errors. As such, we prefer using alloc_new_proxy() and setup_new_proxy() instead of manually allocating the proxy pointer and performing the base init ourselves.
This commit is contained in:
parent
60f45564a1
commit
4194f756de
@ -556,19 +556,20 @@ static int init_peers_frontend(const char *file, int linenum,
|
|||||||
const char *id, struct peers *peers)
|
const char *id, struct peers *peers)
|
||||||
{
|
{
|
||||||
struct proxy *p;
|
struct proxy *p;
|
||||||
|
char *errmsg = NULL;
|
||||||
|
|
||||||
if (peers->peers_fe) {
|
if (peers->peers_fe) {
|
||||||
p = peers->peers_fe;
|
p = peers->peers_fe;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
p = calloc(1, sizeof *p);
|
p = alloc_new_proxy(NULL, PR_CAP_FE | PR_CAP_BE, &errmsg);
|
||||||
if (!p) {
|
if (!p) {
|
||||||
ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
|
ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
|
||||||
|
ha_free(&errmsg);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
init_new_proxy(p);
|
|
||||||
peers_setup_frontend(p);
|
peers_setup_frontend(p);
|
||||||
p->parent = peers;
|
p->parent = peers;
|
||||||
/* Finally store this frontend. */
|
/* Finally store this frontend. */
|
||||||
|
@ -1664,16 +1664,15 @@ static int start_checks()
|
|||||||
|
|
||||||
struct proxy *px;
|
struct proxy *px;
|
||||||
struct server *s;
|
struct server *s;
|
||||||
|
char *errmsg = NULL;
|
||||||
int nbcheck=0, mininter=0, srvpos=0;
|
int nbcheck=0, mininter=0, srvpos=0;
|
||||||
|
|
||||||
/* 0- init the dummy frontend used to create all checks sessions */
|
/* 0- init the dummy frontend used to create all checks sessions */
|
||||||
init_new_proxy(&checks_fe);
|
if (!setup_new_proxy(&checks_fe, "CHECKS-FE", PR_CAP_FE | PR_CAP_BE | PR_CAP_INT, &errmsg)) {
|
||||||
checks_fe.id = strdup("CHECKS-FE");
|
ha_alert("error during checks frontend creation: %s\n", errmsg);
|
||||||
if (!checks_fe.id) {
|
ha_free(&errmsg);
|
||||||
ha_alert("Out of memory creating the checks frontend.\n");
|
|
||||||
return ERR_ALERT | ERR_FATAL;
|
return ERR_ALERT | ERR_FATAL;
|
||||||
}
|
}
|
||||||
checks_fe.cap = PR_CAP_FE | PR_CAP_BE | PR_CAP_INT;
|
|
||||||
checks_fe.mode = PR_MODE_TCP;
|
checks_fe.mode = PR_MODE_TCP;
|
||||||
checks_fe.maxconn = 0;
|
checks_fe.maxconn = 0;
|
||||||
checks_fe.conn_retries = CONN_RETRIES;
|
checks_fe.conn_retries = CONN_RETRIES;
|
||||||
|
11
src/cli.c
11
src/cli.c
@ -448,17 +448,16 @@ void cli_list_keywords(void)
|
|||||||
static struct proxy *cli_alloc_fe(const char *name, const char *file, int line)
|
static struct proxy *cli_alloc_fe(const char *name, const char *file, int line)
|
||||||
{
|
{
|
||||||
struct proxy *fe;
|
struct proxy *fe;
|
||||||
|
char *errmsg = NULL;
|
||||||
|
|
||||||
fe = calloc(1, sizeof(*fe));
|
fe = alloc_new_proxy("GLOBAL", PR_CAP_FE|PR_CAP_INT, &errmsg);
|
||||||
if (!fe)
|
if (!fe) {
|
||||||
|
ha_free(&errmsg); // ignored
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
init_new_proxy(fe);
|
|
||||||
fe->next = proxies_list;
|
fe->next = proxies_list;
|
||||||
proxies_list = fe;
|
proxies_list = fe;
|
||||||
fe->fe_counters.last_change = ns_to_sec(now_ns);
|
|
||||||
fe->id = strdup("GLOBAL");
|
|
||||||
fe->cap = PR_CAP_FE|PR_CAP_INT;
|
|
||||||
fe->maxconn = 10; /* default to 10 concurrent connections */
|
fe->maxconn = 10; /* default to 10 concurrent connections */
|
||||||
fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
|
fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
|
||||||
fe->conf.file = copy_file_name(file);
|
fe->conf.file = copy_file_name(file);
|
||||||
|
@ -1204,8 +1204,6 @@ static int spoe_init(struct proxy *px, struct flt_conf *fconf)
|
|||||||
|
|
||||||
/* conf->agent->fe was already initialized during the config
|
/* conf->agent->fe was already initialized during the config
|
||||||
* parsing. Finish initialization. */
|
* parsing. Finish initialization. */
|
||||||
conf->agent->fe.fe_counters.last_change = ns_to_sec(now_ns);
|
|
||||||
conf->agent->fe.cap = PR_CAP_FE | PR_CAP_INT;
|
|
||||||
conf->agent->fe.mode = PR_MODE_SPOP;
|
conf->agent->fe.mode = PR_MODE_SPOP;
|
||||||
conf->agent->fe.maxconn = 0;
|
conf->agent->fe.maxconn = 0;
|
||||||
conf->agent->fe.options2 |= PR_O2_INDEPSTR;
|
conf->agent->fe.options2 |= PR_O2_INDEPSTR;
|
||||||
@ -2541,8 +2539,11 @@ static int parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
|
|||||||
/* Start agent's proxy initialization here. It will be finished during
|
/* Start agent's proxy initialization here. It will be finished during
|
||||||
* the filter init. */
|
* the filter init. */
|
||||||
memset(&conf->agent->fe, 0, sizeof(conf->agent->fe));
|
memset(&conf->agent->fe, 0, sizeof(conf->agent->fe));
|
||||||
init_new_proxy(&conf->agent->fe);
|
if (!setup_new_proxy(&conf->agent->fe, conf->agent->id, PR_CAP_FE | PR_CAP_INT, err)) {
|
||||||
conf->agent->fe.id = conf->agent->id;
|
memprintf(err, "SPOE agent '%s': %s",
|
||||||
|
curagent->id, *err);
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
conf->agent->fe.parent = conf->agent;
|
conf->agent->fe.parent = conf->agent;
|
||||||
conf->agent->fe.options |= curpxopts;
|
conf->agent->fe.options |= curpxopts;
|
||||||
conf->agent->fe.options2 |= curpxopts2;
|
conf->agent->fe.options2 |= curpxopts2;
|
||||||
|
@ -6060,25 +6060,22 @@ int cfg_parse_log_forward(const char *file, int linenum, char **args, int kwm)
|
|||||||
err_code |= ERR_WARN;
|
err_code |= ERR_WARN;
|
||||||
}
|
}
|
||||||
|
|
||||||
px = calloc(1, sizeof *px);
|
px = alloc_new_proxy(args[1], PR_CAP_FE, &errmsg);
|
||||||
if (!px) {
|
if (!px) {
|
||||||
|
ha_alert("Parsing [%s:%d]: %s\n", file, linenum, errmsg);
|
||||||
err_code |= ERR_ALERT | ERR_FATAL;
|
err_code |= ERR_ALERT | ERR_FATAL;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
init_new_proxy(px);
|
|
||||||
px->next = cfg_log_forward;
|
px->next = cfg_log_forward;
|
||||||
cfg_log_forward = px;
|
cfg_log_forward = px;
|
||||||
px->conf.file = copy_file_name(file);
|
px->conf.file = copy_file_name(file);
|
||||||
px->conf.line = linenum;
|
px->conf.line = linenum;
|
||||||
px->mode = PR_MODE_SYSLOG;
|
px->mode = PR_MODE_SYSLOG;
|
||||||
px->fe_counters.last_change = ns_to_sec(now_ns);
|
|
||||||
px->cap = PR_CAP_FE;
|
|
||||||
px->maxconn = 10;
|
px->maxconn = 10;
|
||||||
px->timeout.client = TICK_ETERNITY;
|
px->timeout.client = TICK_ETERNITY;
|
||||||
px->accept = frontend_accept;
|
px->accept = frontend_accept;
|
||||||
px->default_target = &syslog_applet.obj_type;
|
px->default_target = &syslog_applet.obj_type;
|
||||||
px->id = strdup(args[1]);
|
|
||||||
px->options3 |= PR_O3_LOGF_HOST_FILL;
|
px->options3 |= PR_O3_LOGF_HOST_FILL;
|
||||||
}
|
}
|
||||||
else if (strcmp(args[0], "maxconn") == 0) { /* maxconn */
|
else if (strcmp(args[0], "maxconn") == 0) { /* maxconn */
|
||||||
|
@ -3235,8 +3235,6 @@ static void peer_session_forceshutdown(struct peer *peer)
|
|||||||
/* Pre-configures a peers frontend to accept incoming connections */
|
/* Pre-configures a peers frontend to accept incoming connections */
|
||||||
void peers_setup_frontend(struct proxy *fe)
|
void peers_setup_frontend(struct proxy *fe)
|
||||||
{
|
{
|
||||||
fe->fe_counters.last_change = ns_to_sec(now_ns);
|
|
||||||
fe->cap = PR_CAP_FE | PR_CAP_BE;
|
|
||||||
fe->mode = PR_MODE_PEERS;
|
fe->mode = PR_MODE_PEERS;
|
||||||
fe->maxconn = 0;
|
fe->maxconn = 0;
|
||||||
fe->conn_retries = CONN_RETRIES;
|
fe->conn_retries = CONN_RETRIES;
|
||||||
|
@ -3327,8 +3327,6 @@ int check_action_do_resolve(struct act_rule *rule, struct proxy *px, char **err)
|
|||||||
|
|
||||||
void resolvers_setup_proxy(struct proxy *px)
|
void resolvers_setup_proxy(struct proxy *px)
|
||||||
{
|
{
|
||||||
px->fe_counters.last_change = px->be_counters.last_change = ns_to_sec(now_ns);
|
|
||||||
px->cap = PR_CAP_FE | PR_CAP_BE;
|
|
||||||
px->maxconn = 0;
|
px->maxconn = 0;
|
||||||
px->conn_retries = 1;
|
px->conn_retries = 1;
|
||||||
px->timeout.server = TICK_ETERNITY;
|
px->timeout.server = TICK_ETERNITY;
|
||||||
@ -3478,6 +3476,7 @@ static int resolvers_new(struct resolvers **resolvers, const char *id, const cha
|
|||||||
{
|
{
|
||||||
struct resolvers *r = NULL;
|
struct resolvers *r = NULL;
|
||||||
struct proxy *p = NULL;
|
struct proxy *p = NULL;
|
||||||
|
char *errmsg = NULL;
|
||||||
int err_code = 0;
|
int err_code = 0;
|
||||||
|
|
||||||
if ((r = calloc(1, sizeof(*r))) == NULL) {
|
if ((r = calloc(1, sizeof(*r))) == NULL) {
|
||||||
@ -3486,20 +3485,15 @@ static int resolvers_new(struct resolvers **resolvers, const char *id, const cha
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* allocate new proxy to tcp servers */
|
/* allocate new proxy to tcp servers */
|
||||||
p = calloc(1, sizeof *p);
|
p = alloc_new_proxy(id, PR_CAP_FE | PR_CAP_BE, &errmsg);
|
||||||
if (!p) {
|
if (!p) {
|
||||||
|
ha_free(&errmsg); // ignored
|
||||||
err_code |= ERR_ALERT | ERR_FATAL;
|
err_code |= ERR_ALERT | ERR_FATAL;
|
||||||
goto err_free_r;
|
goto err_free_r;
|
||||||
}
|
}
|
||||||
|
|
||||||
init_new_proxy(p);
|
|
||||||
resolvers_setup_proxy(p);
|
resolvers_setup_proxy(p);
|
||||||
p->parent = r;
|
p->parent = r;
|
||||||
p->id = strdup(id);
|
|
||||||
if (!p->id) {
|
|
||||||
err_code |= ERR_ALERT | ERR_FATAL;
|
|
||||||
goto err_free_p;
|
|
||||||
}
|
|
||||||
p->conf.args.file = p->conf.file = copy_file_name(file);
|
p->conf.args.file = p->conf.file = copy_file_name(file);
|
||||||
p->conf.args.line = p->conf.line = linenum;
|
p->conf.args.line = p->conf.line = linenum;
|
||||||
r->px = p;
|
r->px = p;
|
||||||
@ -3509,7 +3503,7 @@ static int resolvers_new(struct resolvers **resolvers, const char *id, const cha
|
|||||||
r->conf.file = strdup(file);
|
r->conf.file = strdup(file);
|
||||||
if (!r->conf.file) {
|
if (!r->conf.file) {
|
||||||
err_code |= ERR_ALERT | ERR_FATAL;
|
err_code |= ERR_ALERT | ERR_FATAL;
|
||||||
goto err_free_p_id;
|
goto err_free_p;
|
||||||
}
|
}
|
||||||
r->conf.line = linenum;
|
r->conf.line = linenum;
|
||||||
r->id = strdup(id);
|
r->id = strdup(id);
|
||||||
@ -3545,10 +3539,8 @@ static int resolvers_new(struct resolvers **resolvers, const char *id, const cha
|
|||||||
/* free all allocated stuff and return err_code */
|
/* free all allocated stuff and return err_code */
|
||||||
err_free_conf_file:
|
err_free_conf_file:
|
||||||
ha_free((void **)&r->conf.file);
|
ha_free((void **)&r->conf.file);
|
||||||
err_free_p_id:
|
|
||||||
ha_free(&p->id);
|
|
||||||
err_free_p:
|
err_free_p:
|
||||||
ha_free(&p);
|
free_proxy(p);
|
||||||
err_free_r:
|
err_free_r:
|
||||||
ha_free(&r);
|
ha_free(&r);
|
||||||
return err_code;
|
return err_code;
|
||||||
|
10
src/sink.c
10
src/sink.c
@ -400,8 +400,6 @@ static int cli_parse_show_events(char **args, char *payload, struct appctx *appc
|
|||||||
/* Pre-configures a ring proxy to emit connections */
|
/* Pre-configures a ring proxy to emit connections */
|
||||||
void sink_setup_proxy(struct proxy *px)
|
void sink_setup_proxy(struct proxy *px)
|
||||||
{
|
{
|
||||||
px->be_counters.last_change = ns_to_sec(now_ns);
|
|
||||||
px->cap = PR_CAP_BE;
|
|
||||||
px->maxconn = 0;
|
px->maxconn = 0;
|
||||||
px->conn_retries = 1;
|
px->conn_retries = 1;
|
||||||
px->timeout.server = TICK_ETERNITY;
|
px->timeout.server = TICK_ETERNITY;
|
||||||
@ -828,15 +826,11 @@ static struct sink *sink_new_ringbuf(const char *id, const char *description,
|
|||||||
struct proxy *p = NULL; // forward_px
|
struct proxy *p = NULL; // forward_px
|
||||||
|
|
||||||
/* allocate new proxy to handle forwards */
|
/* allocate new proxy to handle forwards */
|
||||||
p = calloc(1, sizeof(*p));
|
p = alloc_new_proxy(id, PR_CAP_BE, err_msg);
|
||||||
if (!p) {
|
if (!p)
|
||||||
memprintf(err_msg, "out of memory");
|
|
||||||
goto err;
|
goto err;
|
||||||
}
|
|
||||||
|
|
||||||
init_new_proxy(p);
|
|
||||||
sink_setup_proxy(p);
|
sink_setup_proxy(p);
|
||||||
p->id = strdup(id);
|
|
||||||
p->conf.args.file = p->conf.file = copy_file_name(file);
|
p->conf.args.file = p->conf.file = copy_file_name(file);
|
||||||
p->conf.args.line = p->conf.line = linenum;
|
p->conf.args.line = p->conf.line = linenum;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user