]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: pattern: support purging arbitrary ranges of generations
authorWilly Tarreau <w@1wt.eu>
Fri, 30 Apr 2021 11:19:37 +0000 (13:19 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 30 Apr 2021 13:36:31 +0000 (15:36 +0200)
Instead of being able to purge only values older than a specific value,
let's support arbitrary ranges and make pat_ref_purge_older() just be
one special case of this one.

include/haproxy/pattern.h
src/pattern.c

index aaa8964ff57588f027b7a3997330d6ccf98f6d52..7f785095d69e7c1caa8418b10166f2ec3417f0ca 100644 (file)
@@ -192,7 +192,7 @@ void pat_ref_delete_by_ptr(struct pat_ref *ref, struct pat_ref_elt *elt);
 int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt);
 int pat_ref_prune(struct pat_ref *ref);
 int pat_ref_commit_elt(struct pat_ref *ref, struct pat_ref_elt *elt, char **err);
-int pat_ref_purge_older(struct pat_ref *ref, unsigned int oldest, int budget);
+int pat_ref_purge_range(struct pat_ref *ref, uint from, uint to, int budget);
 void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace);
 
 /* Create a new generation number for next pattern updates and returns it. This
@@ -237,6 +237,20 @@ static inline int pat_ref_commit(struct pat_ref *ref, unsigned int gen)
        return gen - ref->curr_gen;
 }
 
+/* This function purges all elements from <ref> that are older than generation
+ * <oldest>. It will not purge more than <budget> entries at once, in order to
+ * remain responsive. If budget is negative, no limit is applied.
+ * The caller must already hold the PATREF_LOCK on <ref>. The function will
+ * take the PATEXP_LOCK on all expressions of the pattern as needed. It returns
+ * non-zero on completion, or zero if it had to stop before the end after
+ * <budget> was depleted.
+ */
+static inline int pat_ref_purge_older(struct pat_ref *ref, uint oldest, int budget)
+{
+       return pat_ref_purge_range(ref, oldest + 1, oldest - 1, budget);
+}
+
+
 /*
  * pattern_head manipulation.
  */
index 80ff0259b3c5d66ab22afbe46deb1890cd245c84..5ec7316b128faa2736fc517bad5a759b57aa5474 100644 (file)
@@ -2016,15 +2016,16 @@ int pat_ref_add(struct pat_ref *ref,
        return !!pat_ref_load(ref, ref->curr_gen, pattern, sample, -1, err);
 }
 
-/* This function purges all elements from <ref> that are older than generation
- * <oldest>. It will not purge more than <budget> entries at once, in order to
- * remain responsive. If budget is negative, no limit is applied.
+/* This function purges all elements from <ref> whose generation is included in
+ * the range of <from> to <to> (inclusive), taking wrapping into consideration.
+ * It will not purge more than <budget> entries at once, in order to remain
+ * responsive. If budget is negative, no limit is applied.
  * The caller must already hold the PATREF_LOCK on <ref>. The function will
  * take the PATEXP_LOCK on all expressions of the pattern as needed. It returns
  * non-zero on completion, or zero if it had to stop before the end after
  * <budget> was depleted.
  */
-int pat_ref_purge_older(struct pat_ref *ref, unsigned int oldest, int budget)
+int pat_ref_purge_range(struct pat_ref *ref, uint from, uint to, int budget)
 {
        struct pat_ref_elt *elt, *elt_bck;
        struct bref *bref, *bref_bck;
@@ -2039,7 +2040,7 @@ int pat_ref_purge_older(struct pat_ref *ref, unsigned int oldest, int budget)
        /* assume completion for e.g. empty lists */
        done = 1;
        list_for_each_entry_safe(elt, elt_bck, &ref->head, list) {
-               if ((int)(elt->gen_id - oldest) >= 0)
+               if (elt->gen_id - from > to - from)
                        continue;
 
                if (budget >= 0 && !budget--) {
@@ -2178,8 +2179,7 @@ void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
  */
 int pat_ref_prune(struct pat_ref *ref)
 {
-       return pat_ref_purge_older(ref, ref->curr_gen + 1, 100) &&
-              pat_ref_purge_older(ref, ref->next_gen + 1, 100);
+       return pat_ref_purge_range(ref, 0, ~0, 100);
 }
 
 /* This function looks up any existing reference <ref> in pattern_head <head>, and