dispatcher: sync on load management operations
authorDaniel-Constantin Mierla <miconda@gmail.com>
Tue, 19 May 2020 11:43:10 +0000 (13:43 +0200)
committerDaniel-Constantin Mierla <miconda@gmail.com>
Tue, 19 May 2020 11:43:10 +0000 (13:43 +0200)
- use locks to increment/decrement call load and get least loaded dst
- GH #2322

src/modules/dispatcher/dispatch.c

index 589b54f..b51b5b4 100644 (file)
 #define DS_ALG_RELWEIGHT 11
 #define DS_ALG_PARALLEL 12
 
+/* increment call load */
+#define DS_LOAD_INC(dgrp, didx) do { \
+               lock_get(&(dgrp)->lock); \
+               (dgrp)->dlist[didx].dload++; \
+               lock_release(&(dgrp)->lock); \
+       } while(0)
+
+/* decrement call load */
+#define DS_LOAD_DEC(dgrp, didx) do { \
+               lock_get(&(dgrp)->lock); \
+               if(likely((dgrp)->dlist[didx].dload > 0)) { \
+                       (dgrp)->dlist[didx].dload--; \
+               } \
+               lock_release(&(dgrp)->lock); \
+       } while(0)
+
 static int _ds_table_version = DS_TABLE_VERSION;
 
 static ds_ht_t *_dsht_load = NULL;
@@ -1514,6 +1530,7 @@ int ds_get_leastloaded(ds_set_t *dset)
 
        k = -1;
        t = 0x7fffffff; /* high load */
+       lock_get(&dset->lock); \
        for(j = 0; j < dset->nr; j++) {
                if(!ds_skip_dst(dset->dlist[j].flags)
                                && (dset->dlist[j].attrs.maxload == 0
@@ -1525,6 +1542,7 @@ int ds_get_leastloaded(ds_set_t *dset)
                        }
                }
        }
+       lock_release(&dset->lock); \
        return k;
 }
 
@@ -1546,7 +1564,7 @@ int ds_load_add(struct sip_msg *msg, ds_set_t *dset, int setid, int dst)
                                msg->callid->body.s);
                return -1;
        }
-       dset->dlist[dst].dload++;
+       DS_LOAD_INC(dset, dst);
        return 0;
 }
 
@@ -1614,8 +1632,7 @@ int ds_load_replace(struct sip_msg *msg, str *duid)
 
        ds_unlock_cell(_dsht_load, &msg->callid->body);
        ds_del_cell(_dsht_load, &msg->callid->body);
-       if(idx->dlist[olddst].dload > 0)
-               idx->dlist[olddst].dload--;
+       DS_LOAD_DEC(idx, olddst);
 
        if(ds_load_add(msg, idx, set, newdst) < 0) {
                LM_ERR("unable to replace destination load [%.*s / %.*s]\n", duid->len,
@@ -1654,8 +1671,7 @@ int ds_load_remove_byid(int set, str *duid)
                return -1;
        }
 
-       if(idx->dlist[olddst].dload > 0)
-               idx->dlist[olddst].dload--;
+       DS_LOAD_DEC(idx, olddst);
 
        return 0;
 }