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
  */
... ...
@@ -2271,58 +2330,8 @@ int ds_manage_routes(sip_msg_t *msg, ds_select_state_t *rstate)
2271 2330
 		return 1;
2272 2331
 	}
2273 2332
 
2274
-	LM_DBG("using first entry [%d/%d]\n", rstate->setid, hash);
2275
-	if(ds_add_xavp_record(idx, hash, rstate->setid, rstate->alg,
2276
-				&rstate->lxavp)<0) {
2277
-		LM_ERR("failed to add destination in the xavp (%d/%d)\n",
2278
-				hash, rstate->setid);
2333
+	if (ds_manage_routes_fill_xavp(hash, idx, rstate) == -1)
2279 2334
 		return -1;
2280
-	}
2281
-	rstate->cnt++;
2282
-
2283
-	/* add to xavp the destinations after the selected one */
2284
-	for(i = hash + 1; i < idx->nr && rstate->cnt < rstate->limit; i++) {
2285
-		if(ds_skip_dst(idx->dlist[i].flags)
2286
-				|| (ds_use_default != 0 && i == (idx->nr - 1))) {
2287
-			continue;
2288
-		}
2289
-		/* max load exceeded per destination */
2290
-		if(rstate->alg == DS_ALG_CALLLOAD
2291
-				&& idx->dlist[i].attrs.maxload != 0
2292
-				&& idx->dlist[i].dload >= idx->dlist[i].attrs.maxload) {
2293
-			continue;
2294
-		}
2295
-		LM_DBG("using entry [%d/%d]\n", rstate->setid, i);
2296
-		if(ds_add_xavp_record(idx, i, rstate->setid, rstate->alg,
2297
-					&rstate->lxavp)<0) {
2298
-			LM_ERR("failed to add destination in the xavp (%d/%d)\n",
2299
-					i, rstate->setid);
2300
-			return -1;
2301
-		}
2302
-		rstate->cnt++;
2303
-	}
2304
-
2305
-	/* add to xavp the destinations before the selected one */
2306
-	for(i = 0; i < hash && rstate->cnt < rstate->limit; i++) {
2307
-		if(ds_skip_dst(idx->dlist[i].flags)
2308
-				|| (ds_use_default != 0 && i == (idx->nr - 1))) {
2309
-			continue;
2310
-		}
2311
-		/* max load exceeded per destination */
2312
-		if(rstate->alg == DS_ALG_CALLLOAD
2313
-				&& idx->dlist[i].attrs.maxload != 0
2314
-				&& idx->dlist[i].dload >= idx->dlist[i].attrs.maxload) {
2315
-			continue;
2316
-		}
2317
-		LM_DBG("using entry [%d/%d]\n", rstate->setid, i);
2318
-		if(ds_add_xavp_record(idx, i, rstate->setid, rstate->alg,
2319
-					&rstate->lxavp)<0) {
2320
-			LM_ERR("failed to add destination in the xavp (%d/%d)\n",
2321
-					i, rstate->setid);
2322
-			return -1;
2323
-		}
2324
-		rstate->cnt++;
2325
-	}
2326 2335
 
2327 2336
 	/* add default dst to last position in XAVP list */
2328 2337
 	if(ds_use_default != 0 && hash != idx->nr - 1