MINOR: proxy: support storing defaults sections into their own tree

Now we'll have a tree of named defaults sections. The regular insertion
and lookup functions take care of the capability in order to select the
appropriate tree. A new function proxy_destroy_defaults() removes a
proxy from this tree and frees it entirely.
This commit is contained in:
Willy Tarreau 2021-02-12 13:52:11 +01:00
parent c02ab03142
commit 0a0f6a7e4f
2 changed files with 30 additions and 6 deletions

View File

@ -58,6 +58,7 @@ int proxy_cfg_ensure_no_http(struct proxy *curproxy);
void init_new_proxy(struct proxy *p);
void proxy_preset_defaults(struct proxy *defproxy);
void proxy_free_defaults(struct proxy *defproxy);
void proxy_destroy_defaults(struct proxy *px);
struct proxy *alloc_new_proxy(const char *name, unsigned int cap, const char *file, int linenum,
const struct proxy *defproxy, char **errmsg);
int get_backend_server(const char *bk_name, const char *sv_name,

View File

@ -53,6 +53,7 @@ int listeners; /* # of proxy listeners, set by cfgparse */
struct proxy *proxies_list = NULL; /* list of all existing proxies */
struct eb_root used_proxy_id = EB_ROOT; /* list of proxy IDs in use */
struct eb_root proxy_by_name = EB_ROOT; /* tree of proxies sorted by name */
struct eb_root defproxy_by_name = EB_ROOT; /* tree of default proxies sorted by name (dups possible) */
unsigned int error_snapshot_id = 0; /* global ID assigned to each error then incremented */
/* proxy->options */
@ -738,13 +739,16 @@ static int proxy_parse_tcpka_intvl(char **args, int section, struct proxy *proxy
}
#endif
/* This function inserts proxy <px> into the tree of known proxies. The proxy's
* name is used as the storing key so it must already have been initialized.
/* This function inserts proxy <px> into the tree of known proxies (regular
* ones or defaults depending on px->cap & PR_CAP_DEF). The proxy's name is
* used as the storing key so it must already have been initialized.
*/
void proxy_store_name(struct proxy *px)
{
struct eb_root *root = (px->cap & PR_CAP_DEF) ? &defproxy_by_name : &proxy_by_name;
px->conf.by_name.key = px->id;
ebis_insert(&proxy_by_name, &px->conf.by_name);
ebis_insert(root, &px->conf.by_name);
}
/* Returns a pointer to the first proxy matching capabilities <cap> and id
@ -774,21 +778,25 @@ struct proxy *proxy_find_by_id(int id, int cap, int table)
/* Returns a pointer to the first proxy matching either name <name>, or id
* <name> if <name> begins with a '#'. NULL is returned if no match is found.
* If <table> is non-zero, it only considers proxies having a table.
* If <table> is non-zero, it only considers proxies having a table. The search
* is made into the regular proxies, unless <cap> has PR_CAP_DEF set in which
* case it's searched into the defproxy tree.
*/
struct proxy *proxy_find_by_name(const char *name, int cap, int table)
{
struct proxy *curproxy;
if (*name == '#') {
if (*name == '#' && !(cap & PR_CAP_DEF)) {
curproxy = proxy_find_by_id(atoi(name + 1), cap, table);
if (curproxy)
return curproxy;
}
else {
struct eb_root *root;
struct ebpt_node *node;
for (node = ebis_lookup(&proxy_by_name, name); node; node = ebpt_next(node)) {
root = (cap & PR_CAP_DEF) ? &defproxy_by_name : &proxy_by_name;
for (node = ebis_lookup(root, name); node; node = ebpt_next(node)) {
curproxy = container_of(node, struct proxy, conf.by_name);
if (strcmp(curproxy->id, name) != 0)
@ -1156,6 +1164,21 @@ void proxy_free_defaults(struct proxy *defproxy)
*/
}
/* delete a defproxy from the tree if still in it, frees its content and its
* storage. Nothing is done if <px> is NULL or if it doesn't have PR_CAP_DEF
* set, allowing to pass it the direct result of a lookup function.
*/
void proxy_destroy_defaults(struct proxy *px)
{
if (!px)
return;
if (!(px->cap & PR_CAP_DEF))
return;
ebpt_delete(&px->conf.by_name);
proxy_free_defaults(px);
free(px);
}
/* Allocates a new proxy <name> of type <cap> found at position <file:linenum>,
* preset it from the defaults of <defproxy> and returns it. Un case of error,
* an alert is printed and NULL is returned. If <errmsg> is not NULL, an error