]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MEDIUM] stick_table: don't overwrite data when storing an entry
authorWilly Tarreau <w@1wt.eu>
Sun, 6 Jun 2010 13:38:59 +0000 (15:38 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 14 Jun 2010 13:10:24 +0000 (15:10 +0200)
Till now sticky sessions only held server IDs. Now there are other
data types so it is not acceptable anymore to overwrite the server ID
when writing something. The server ID must then only be written from
the caller when appropriate. Doing this has also led to separate
lookup and storage.

include/proto/stick_table.h
src/session.c
src/stick_table.c

index 01cd391ffabab0486c7d9a48cfde377f2099191f..4200d97faa91f353d44270ddb49ac331bf3f25f2 100644 (file)
@@ -31,8 +31,9 @@ void stksess_free(struct stktable *t, struct stksess *ts);
 
 int stktable_init(struct stktable *t);
 int stktable_parse_type(char **args, int *idx, unsigned long *type, size_t *key_size);
-int stktable_store(struct stktable *t, struct stksess *ts, int sid);
-struct stksess *stktable_lookup(struct stktable *t, struct stktable_key *key);
+struct stksess *stktable_store(struct stktable *t, struct stksess *ts);
+struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts);
+struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key);
 struct stktable_key *stktable_fetch_key(struct proxy *px, struct session *l4,
                                        void *l7, int dir, struct pattern_expr *expr,
                                        unsigned long table_type);
index 63184f389abe8b498b5d5b27d699d6b6cfdbe5c8..873201c68ddfedae8d88a657003b7b41b230d310 100644 (file)
@@ -942,7 +942,7 @@ int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
                        if (rule->flags & STK_IS_MATCH) {
                                struct stksess *ts;
 
-                               if ((ts = stktable_lookup(rule->table.t, key)) != NULL) {
+                               if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
                                        if (!(s->flags & SN_ASSIGNED)) {
                                                struct eb32_node *node;
 
@@ -1049,10 +1049,18 @@ int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
 
        /* process store request and store response */
        for (i = 0; i < s->store_count; i++) {
-               if (stktable_store(s->store[i].table, s->store[i].ts, s->srv->puid) > 0) {
+               struct stksess *ts;
+
+               ts = stktable_lookup(s->store[i].table, s->store[i].ts);
+               if (ts) {
+                       /* the entry already existed, we can free ours */
                        stksess_free(s->store[i].table, s->store[i].ts);
-                       s->store[i].ts = NULL;
                }
+               else
+                       ts = stktable_store(s->store[i].table, s->store[i].ts);
+
+               s->store[i].ts = NULL;
+               ts->sid = s->srv->puid;
        }
 
        rep->analysers &= ~an_bit;
index 4cd6c75962c16f486ec1ab791356bccbdd4662ce..a86f48f41a587f63f614b024094888c447ee9a3d 100644 (file)
@@ -152,10 +152,10 @@ struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
 }
 
 /*
- * Looks in table <t> for a sticky session matching <key>.
+ * Looks in table <t> for a sticky session matching key <key>.
  * Returns pointer on requested sticky session or NULL if none was found.
  */
-struct stksess *stktable_lookup(struct stktable *t, struct stktable_key *key)
+struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
 {
        struct ebmb_node *eb;
 
@@ -172,14 +172,11 @@ struct stksess *stktable_lookup(struct stktable *t, struct stktable_key *key)
        return ebmb_entry(eb, struct stksess, key);
 }
 
-/* Try to store sticky session <ts> in the table. If another entry already
- * exists with the same key, its server ID is updated with <sid> and a non
- * zero value is returned so that the caller knows it can release its stksess.
- * If no similar entry was present, <ts> is inserted into the tree and assigned
- * server ID <sid>. Zero is returned in this case, and the caller must not
- * release the stksess.
+/*
+ * Looks in table <t> for a sticky session with same key as <ts>.
+ * Returns pointer on requested sticky session or NULL if none was found.
  */
-int stktable_store(struct stktable *t, struct stksess *ts, int sid)
+struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
 {
        struct ebmb_node *eb;
 
@@ -188,26 +185,28 @@ int stktable_store(struct stktable *t, struct stksess *ts, int sid)
        else
                eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
 
-       if (unlikely(!eb)) {
-               /* no existing session, insert ours */
-               ts->sid = sid;
-               ebmb_insert(&t->keys, &ts->key, t->key_size);
+       if (unlikely(!eb))
+               return NULL;
 
-               ts->exp.key = ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
-               eb32_insert(&t->exps, &ts->exp);
+       return ebmb_entry(eb, struct stksess, key);
+}
 
-               if (t->expire) {
-                       t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
-                       task_queue(t->exp_task);
-               }
-               return 0;
-       }
+/* Insert new sticky session <ts> in the table. It is assumed that it does not
+ * yet exist (the caller must check this). The table's timeout is updated if it
+ * is set. <ts> is returned.
+ */
+struct stksess *stktable_store(struct stktable *t, struct stksess *ts)
+{
+       ebmb_insert(&t->keys, &ts->key, t->key_size);
 
-       ts = ebmb_entry(eb, struct stksess, key);
+       ts->exp.key = ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
+       eb32_insert(&t->exps, &ts->exp);
 
-       if ( ts->sid != sid )
-               ts->sid = sid;
-       return 1;
+       if (t->expire) {
+               t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
+               task_queue(t->exp_task);
+       }
+       return ts;
 }
 
 /*