Skip to content
This repository was archived by the owner on Aug 4, 2019. It is now read-only.

Commit 45fc5d4

Browse files
committed
making cache expiry configurable
1 parent 591b88b commit 45fc5d4

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

src/blacklist.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414

1515
int initial_blacklist_size = 100;
1616

17+
/**
18+
* externally settable parameter
19+
*
20+
* @param new_size initial size of expandable blacklist cache
21+
*/
22+
1723
void set_initial_blacklist_size(int new_size)
1824
{
1925
initial_blacklist_size = new_size;
2026
}
21-
22-
2327
/**
2428
* Adds the actor to the Repsheet blacklist
2529
*

src/check_cidr.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,23 @@
77
#include "vector.h"
88

99

10-
int CACHED_FOR_SECONDS = 60;
10+
unsigned int cache_expiry = 60;
11+
12+
/**
13+
* externally settable parameter
14+
*
15+
* @param new_expiry number of seconds for caching CIDR blocks before hitting redis again
16+
*/
17+
void set_cache_expiry(unsigned int new_expiry)
18+
{
19+
cache_expiry = new_expiry;
20+
}
1121

1222
int check_and_update_cache(redisContext *context, const char *actor, char *reason, char *list, expanding_vector *ev, time_t *last_update_time)
1323
{
1424
long current_seconds = time(NULL);
1525

16-
if (*last_update_time + CACHED_FOR_SECONDS < current_seconds) {
26+
if (*last_update_time + cache_expiry < current_seconds) {
1727
clear_expanding_vector(ev);
1828
redisReply *listed = redisCommand(context, "SMEMBERS repsheet:cidr:%s", list);
1929
if (listed) {

src/check_cidr.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#ifndef CHECK_CIDR
22
#define CHECK_CIDR
33

4-
extern int CACHED_FOR_SECONDS;
5-
64
#include "vector.h"
75
int checkCIDR(redisContext *context, const char *actor, char *reason, char *list, expanding_vector *ev, long *cache_update_time);
8-
96
#endif

src/repsheet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ int is_user_marked(redisContext *context, const char *actor, char *reason);
3838

3939
void set_initial_whitelist_size(int new_size);
4040
void set_initial_blacklist_size(int new_size);
41+
void set_cache_expiry(unsigned int new_expiry);
4142

4243
#endif

test/cidr_test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ START_TEST(class_c)
55
{
66
char *block = "10.0.0.0/24";
77
int in = ip_address_to_integer("10.0.0.50");
8+
int out = ip_address_to_integer("10.0.1.50");
89
int lower = ip_address_to_integer("10.0.0.0");
910
int upper = ip_address_to_integer("10.0.0.255");
1011

1112
ck_assert_int_eq(1, cidr_contains(block, in));
13+
ck_assert_int_eq(0, cidr_contains(block, out));
1214
ck_assert_int_eq(1, cidr_contains(block, lower));
1315
ck_assert_int_eq(1, cidr_contains(block, upper));
1416
}

0 commit comments

Comments
 (0)