]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: vars: factor out common code from vars_get_by_{desc,name}
authorWilly Tarreau <w@1wt.eu>
Fri, 3 Sep 2021 09:40:58 +0000 (11:40 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 3 Sep 2021 09:43:35 +0000 (11:43 +0200)
The two functions vars_get_by_name() and vars_get_by_scope() perform
almost the same operations except that they differ from the way the
name and scope are retrieved. The second part in common is more
complex and involves locking, so better factor this one out into a
new function.

There is no other change than refactoring.

src/vars.c

index 6208093d27b305b4129e9cce8b3afe95cb165c0b..236466b217994af0499ca7fcb314676511c1f547 100644 (file)
@@ -569,6 +569,34 @@ int vars_unset_by_name_ifexist(const char *name, size_t len, struct sample *smp)
 }
 
 
+/* This retrieves variable <name> from variables <vars>, and if found,
+ * duplicates the result into sample <smp>. smp_dup() is used in order to
+ * release the variables lock ASAP (so a pre-allocated chunk is obtained
+ * via get_trash_shunk()). The variables' lock is used for reads.
+ *
+ * The function returns 0 if the variable was not found, otherwise 1
+ * with the sample filled.
+ */
+static int var_to_smp(struct vars *vars, const char *name, struct sample *smp)
+{
+       struct var *var;
+
+       /* Get the variable entry. */
+       HA_RWLOCK_RDLOCK(VARS_LOCK, &vars->rwlock);
+       var = var_get(vars, name);
+       if (!var) {
+               HA_RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock);
+               return 0;
+       }
+
+       /* Copy sample. */
+       smp->data = var->data;
+       smp_dup(smp);
+
+       HA_RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock);
+       return 1;
+}
+
 /* This function fills a sample with the variable content.
  *
  * Keep in mind that a sample content is duplicated by using smp_dup()
@@ -580,7 +608,6 @@ int vars_unset_by_name_ifexist(const char *name, size_t len, struct sample *smp)
 int vars_get_by_name(const char *name, size_t len, struct sample *smp)
 {
        struct vars *vars;
-       struct var *var;
        enum vars_scope scope;
 
        /* Resolve name and scope. */
@@ -593,20 +620,7 @@ int vars_get_by_name(const char *name, size_t len, struct sample *smp)
        if (!vars || vars->scope != scope)
                return 0;
 
-       /* Get the variable entry. */
-       HA_RWLOCK_RDLOCK(VARS_LOCK, &vars->rwlock);
-       var = var_get(vars, name);
-       if (!var) {
-               HA_RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock);
-               return 0;
-       }
-
-       /* Copy sample. */
-       smp->data = var->data;
-       smp_dup(smp);
-
-       HA_RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock);
-       return 1;
+       return var_to_smp(vars, name, smp);
 }
 
 /* This function fills a sample with the content of the variable described
@@ -621,7 +635,6 @@ int vars_get_by_name(const char *name, size_t len, struct sample *smp)
 int vars_get_by_desc(const struct var_desc *var_desc, struct sample *smp)
 {
        struct vars *vars;
-       struct var *var;
 
        /* Select "vars" pool according with the scope. */
        vars = get_vars(smp->sess, smp->strm, var_desc->scope);
@@ -630,20 +643,7 @@ int vars_get_by_desc(const struct var_desc *var_desc, struct sample *smp)
        if (!vars || vars->scope != var_desc->scope)
                return 0;
 
-       /* Get the variable entry. */
-       HA_RWLOCK_RDLOCK(VARS_LOCK, &vars->rwlock);
-       var = var_get(vars, var_desc->name);
-       if (!var) {
-               HA_RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock);
-               return 0;
-       }
-
-       /* Copy sample. */
-       smp->data = var->data;
-       smp_dup(smp);
-
-       HA_RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock);
-       return 1;
+       return var_to_smp(vars, var_desc->name, smp);
 }
 
 /* Always returns ACT_RET_CONT even if an error occurs. */