CLEANUP: vars: factor out common code from vars_get_by_{desc,name}
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.
diff --git a/src/vars.c b/src/vars.c
index 6208093..236466b 100644
--- a/src/vars.c
+++ b/src/vars.c
@@ -569,6 +569,34 @@
}
+/* 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_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. */
@@ -592,21 +619,8 @@
vars = get_vars(smp->sess, smp->strm, scope);
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_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 @@
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. */