From 9b96e857ac618fef58bfb3816040134ef5508580 Mon Sep 17 00:00:00 2001 From: Liviu Chircu Date: Wed, 20 Jan 2021 11:44:46 +0200 Subject: [PATCH] fraud_detection: Optimize time-recurrences which always match Rather than always (re)evaluating a time rec which matches 100% of the time, just set it to NULL, so drouting quickly skips the check. (cherry picked from commit 2a3a1050e8e04f40d0500a8eb5d5e1429344077f) (cherry picked from commit 3fd880ab52b79bab5147403c4cfe994ae5114f4b) --- modules/fraud_detection/frd_load.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/modules/fraud_detection/frd_load.c b/modules/fraud_detection/frd_load.c index 1e8de6e7041..1fb1839421a 100644 --- a/modules/fraud_detection/frd_load.c +++ b/modules/fraud_detection/frd_load.c @@ -225,12 +225,22 @@ static int parse_week_days(const str *week_days, unsigned short *day_set) } static int create_time_rec(const str *time_start, const str *time_end, - const str *week_days, tmrec_p trec) + const str *week_days, tmrec_p trec, tmrec_p *out_rec) { int end_h, end_m; memset(trec, 0, sizeof(tmrec_t)); + /* the default, "catch-all" time rec - using NULL is optimal */ + if (str_match(time_start, _str("00:00")) && + str_match(time_end, _str("23:59")) && + str_match(week_days, _str("Mon-Sun"))) { + *out_rec = NULL; + return 0; + } else { + *out_rec = trec; + } + if (strtime(time_start, &trec->ts.tm_hour, &trec->ts.tm_min) != 0 || strtime(time_end, &end_h, &end_m) != 0) return -1; @@ -344,6 +354,8 @@ static int frd_load_data(dr_head_p drp, free_list_t **fl) fl_it->n = row_count; for (i = 0; i < row_count; ++i) { + tmrec_p trec; + values = ROW_VALUES(rows + i); fl_it->trec[i].byday = NULL; @@ -366,7 +378,8 @@ static int frd_load_data(dr_head_p drp, free_list_t **fl) get_str_from_dbval(end_h_col.s, values + 4, 1, 1, end_time, null_val); get_str_from_dbval(days_col.s, values + 5, 1, 1, days, null_val); - if (create_time_rec(&start_time, &end_time, &days, fl_it->trec + i) != 0) + if (create_time_rec(&start_time, &end_time, &days, fl_it->trec + i, + &trec) != 0) goto null_val; /* Now load the thresholds */ @@ -379,7 +392,7 @@ static int frd_load_data(dr_head_p drp, free_list_t **fl) } /* Rule OK, time to put it in DR */ - if (drb.add_rule(drp, rid, &prefix, pid, 0, fl_it->trec + i, + if (drb.add_rule(drp, rid, &prefix, pid, 0, trec, (void*)(&fl_it->thr[i])) != 0) { LM_ERR("Cannot add rule in dr <%u>. Skipping...\n", rid);