Browse code

dispatcher: ds_manage_routes refactoring

break large function

Julien Chavanton authored on 07/10/2020 16:31:21
Showing 1 changed files
... ...
@@ -2066,6 +2066,65 @@ int ds_select_dst_limit(sip_msg_t *msg, int set, int alg, uint32_t limit,
2066 2066
 	return ret;
2067 2067
 }
2068 2068
 
2069
+int ds_manage_routes_fill_xavp(unsigned int hash, ds_set_t *idx, ds_select_state_t *rstate)
2070
+{
2071
+	int i;
2072
+
2073
+	LM_DBG("using first entry [%d/%d]\n", rstate->setid, hash);
2074
+	if(ds_add_xavp_record(idx, hash, rstate->setid, rstate->alg,
2075
+				&rstate->lxavp)<0) {
2076
+		LM_ERR("failed to add destination in the xavp (%d/%d)\n",
2077
+				hash, rstate->setid);
2078
+		return -1;
2079
+	}
2080
+	rstate->cnt++;
2081
+
2082
+	/* add to xavp the destinations after the selected one */
2083
+	for(i = hash + 1; i < idx->nr && rstate->cnt < rstate->limit; i++) {
2084
+		if(ds_skip_dst(idx->dlist[i].flags)
2085
+				|| (ds_use_default != 0 && i == (idx->nr - 1))) {
2086
+			continue;
2087
+		}
2088
+		/* max load exceeded per destination */
2089
+		if(rstate->alg == DS_ALG_CALLLOAD
2090
+				&& idx->dlist[i].attrs.maxload != 0
2091
+				&& idx->dlist[i].dload >= idx->dlist[i].attrs.maxload) {
2092
+			continue;
2093
+		}
2094
+		LM_DBG("using entry [%d/%d]\n", rstate->setid, i);
2095
+		if(ds_add_xavp_record(idx, i, rstate->setid, rstate->alg,
2096
+					&rstate->lxavp)<0) {
2097
+			LM_ERR("failed to add destination in the xavp (%d/%d)\n",
2098
+					i, rstate->setid);
2099
+			return -1;
2100
+		}
2101
+		rstate->cnt++;
2102
+	}
2103
+
2104
+	/* add to xavp the destinations before the selected one */
2105
+	for(i = 0; i < hash && rstate->cnt < rstate->limit; i++) {
2106
+		if(ds_skip_dst(idx->dlist[i].flags)
2107
+				|| (ds_use_default != 0 && i == (idx->nr - 1))) {
2108
+			continue;
2109
+		}
2110
+		/* max load exceeded per destination */
2111
+		if(rstate->alg == DS_ALG_CALLLOAD
2112
+				&& idx->dlist[i].attrs.maxload != 0
2113
+				&& idx->dlist[i].dload >= idx->dlist[i].attrs.maxload) {
2114
+			continue;
2115
+		}
2116
+		LM_DBG("using entry [%d/%d]\n", rstate->setid, i);
2117
+		if(ds_add_xavp_record(idx, i, rstate->setid, rstate->alg,
2118
+					&rstate->lxavp)<0) {
2119
+			LM_ERR("failed to add destination in the xavp (%d/%d)\n",
2120
+					i, rstate->setid);
2121
+			return -1;
2122
+		}
2123
+		rstate->cnt++;
2124
+	}
2125
+	return 0;
2126
+}
2127
+
2069 2128
 /**
2070 2129
  *
2071 2130
  */
... ...
@@ -2285,58 +2344,8 @@ int ds_manage_routes(sip_msg_t *msg, ds_select_state_t *rstate)
2285 2344
 		return 1;
2286 2345
 	}
2287 2346
 
2288
-	LM_DBG("using first entry [%d/%d]\n", rstate->setid, hash);
2289
-	if(ds_add_xavp_record(idx, hash, rstate->setid, rstate->alg,
2290
-				&rstate->lxavp)<0) {
2291
-		LM_ERR("failed to add destination in the xavp (%d/%d)\n",
2292
-				hash, rstate->setid);
2347
+	if (ds_manage_routes_fill_xavp(hash, idx, rstate) == -1)
2293 2348
 		return -1;
2294
-	}
2295
-	rstate->cnt++;
2296
-
2297
-	/* add to xavp the destinations after the selected one */
2298
-	for(i = hash + 1; i < idx->nr && rstate->cnt < rstate->limit; i++) {
2299
-		if(ds_skip_dst(idx->dlist[i].flags)
2300
-				|| (ds_use_default != 0 && i == (idx->nr - 1))) {
2301
-			continue;
2302
-		}
2303
-		/* max load exceeded per destination */
2304
-		if(rstate->alg == DS_ALG_CALLLOAD
2305
-				&& idx->dlist[i].attrs.maxload != 0
2306
-				&& idx->dlist[i].dload >= idx->dlist[i].attrs.maxload) {
2307
-			continue;
2308
-		}
2309
-		LM_DBG("using entry [%d/%d]\n", rstate->setid, i);
2310
-		if(ds_add_xavp_record(idx, i, rstate->setid, rstate->alg,
2311
-					&rstate->lxavp)<0) {
2312
-			LM_ERR("failed to add destination in the xavp (%d/%d)\n",
2313
-					i, rstate->setid);
2314
-			return -1;
2315
-		}
2316
-		rstate->cnt++;
2317
-	}
2318
-
2319
-	/* add to xavp the destinations before the selected one */
2320
-	for(i = 0; i < hash && rstate->cnt < rstate->limit; i++) {
2321
-		if(ds_skip_dst(idx->dlist[i].flags)
2322
-				|| (ds_use_default != 0 && i == (idx->nr - 1))) {
2323
-			continue;
2324
-		}
2325
-		/* max load exceeded per destination */
2326
-		if(rstate->alg == DS_ALG_CALLLOAD
2327
-				&& idx->dlist[i].attrs.maxload != 0
2328
-				&& idx->dlist[i].dload >= idx->dlist[i].attrs.maxload) {
2329
-			continue;
2330
-		}
2331
-		LM_DBG("using entry [%d/%d]\n", rstate->setid, i);
2332
-		if(ds_add_xavp_record(idx, i, rstate->setid, rstate->alg,
2333
-					&rstate->lxavp)<0) {
2334
-			LM_ERR("failed to add destination in the xavp (%d/%d)\n",
2335
-					i, rstate->setid);
2336
-			return -1;
2337
-		}
2338
-		rstate->cnt++;
2339
-	}
2340 2349
 
2341 2350
 	/* add default dst to last position in XAVP list */
2342 2351
 	if(ds_use_default != 0 && hash != idx->nr - 1