Skip to content

Commit

Permalink
Merge pull request #172 from pi-hole/new/DBinterval
Browse files Browse the repository at this point in the history
Make DB interval adjustable
  • Loading branch information
DL6ER authored Dec 13, 2017
2 parents 95bdd62 + 5951e5b commit 8ab56e5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
5 changes: 1 addition & 4 deletions FTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@
// Default -60 (one minute before a full hour)
#define GCdelay (-60)

// How often do we dump into FTL's database?
// Default: 60 (once per minute)
#define DBinterval 60

// Static structs
typedef struct {
const char* conf;
Expand Down Expand Up @@ -125,6 +121,7 @@ typedef struct {
int maxDBdays;
bool resolveIPv6;
bool resolveIPv4;
int DBinterval;
} ConfigStruct;

// Dynamic structs
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Possible settings (**the option shown first is the default**):
- `MAXDBDAYS=365` (How long should queries be stored in the database? Setting this to `0` disables the database altogether)
- `RESOLVE_IPV6=yes|no` (Should `FTL` try to resolve IPv6 addresses to host names?)
- `RESOLVE_IPV4=yes|no` (Should `FTL` try to resolve IPv4 addresses to host names?)
- `DBINTERVAL=1.0` (How often do we store queries in FTL's database [minutes]?)
- `DBFILE=/etc/pihole/pihole-FTL.db` (Specify path and filename of FTL's SQLite long-term database. Setting this to `DBFILE=` disables the database altogether)

### Implemented keywords (starting with `>`, subject to change):
Expand Down
21 changes: 21 additions & 0 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,26 @@ void read_FTLconf(void)
else
logg(" RESOLVE_IPV4: Don\'t resolve IPv4 addresses");

// DBINTERVAL
// How often do we store queries in FTL's database [minutes]?
// this value can be a floating point number, e.g. "DBINTERVAL=0.5"
// defaults to: once per minute
config.DBinterval = 60;
buffer = parse_FTLconf(fp, "DBINTERVAL");

float fvalue = 0;
if(buffer != NULL && sscanf(buffer, "%f", &fvalue))
// check if the read value is
// - larger than 0.1min (6sec), and
// - smaller than 1440.0min (once a day)
if(fvalue >= 0.1 && fvalue <= 1440.0)
config.DBinterval = (int)(60.*fvalue);

if(config.DBinterval == 60)
logg(" DBINTERVAL: saving to DB file every minute");
else
logg(" DBINTERVAL: saving to DB file every %i seconds", config.DBinterval);

// DBFILE
// defaults to: "/etc/pihole/pihole-FTL.db"
buffer = parse_FTLconf(fp, "DBFILE");
Expand All @@ -153,6 +173,7 @@ void read_FTLconf(void)
else
logg(" DBFILE: Not using database due to empty filename");


logg("Finished config file parsing");

// Release memory
Expand Down
19 changes: 9 additions & 10 deletions database.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,18 +445,17 @@ void *DB_thread(void *val)
// Set thread name
prctl(PR_SET_NAME,"DB",0,0,0);

if(!DBdeleteoldqueries)
{
// Lock FTL's data structure, since it is likely that it will be changed here
enable_thread_lock("DB_thread");
// Lock FTL's data structure, since it is likely that it will be changed here
enable_thread_lock("DB_thread");

// Save data to database
save_to_DB();
// Save data to database
save_to_DB();

// Release thread lock
disable_thread_lock("DB_thread");
}
else
// Release thread lock
disable_thread_lock("DB_thread");

// Check if GC should be done on the database
if(DBdeleteoldqueries)
{
// No thread locks needed
delete_old_queries_in_DB();
Expand Down
4 changes: 2 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ int main (int argc, char* argv[]) {
if(((time(NULL) - GCdelay)%GCinterval) == 0)
runGCthread = true;

if(database && ((time(NULL)%DBinterval) == 0))
if(database && ((time(NULL)%config.DBinterval) == 0))
runDBthread = true;

// Garbadge collect in regular interval, but don't do it if the threadlocks is set
Expand Down Expand Up @@ -134,7 +134,7 @@ int main (int argc, char* argv[]) {
}

// Avoid immediate re-run of DB thread
while(((time(NULL)%DBinterval) == 0))
while(((time(NULL)%config.DBinterval) == 0))
sleepms(100);
}

Expand Down

0 comments on commit 8ab56e5

Please sign in to comment.