From 2fe4636fa44ad48b801d0e38b892c0e47998327d Mon Sep 17 00:00:00 2001 From: brain Date: Mon, 8 Jan 2007 18:42:13 +0000 Subject: Add repeating timers, and make an hourly prune of the dns cache, otherwise a cache entry might not be cleared until a user with that ip comes back! git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@6264 e03df62e-2008-0410-955e-edbf42e46eb7 --- src/dns.cpp | 47 +++++++++++++++++++++++++++++++++++++++-------- src/timer.cpp | 32 ++++++++++++++++++++++++++------ 2 files changed, 65 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/dns.cpp b/src/dns.cpp index cea1c4a43..625951ee5 100644 --- a/src/dns.cpp +++ b/src/dns.cpp @@ -103,6 +103,21 @@ class DNSRequest int SendRequests(const DNSHeader *header, const int length, QueryType qt); }; +class CacheTimer : public InspTimer +{ + private: + InspIRCd* ServerInstance; + DNS* dns; + public: + CacheTimer(InspIRCd* Instance, DNS* thisdns) + : InspTimer(3600, Instance->Time(), true), ServerInstance(Instance), dns(thisdns) { } + + virtual void Tick(time_t TIME) + { + dns->PruneCache(); + } +}; + class RequestTimeout : public InspTimer { InspIRCd* ServerInstance; @@ -262,6 +277,23 @@ int DNS::ClearCache() return rv; } +int DNS::PruneCache() +{ + int n = 0; + dnscache* newcache = new dnscache(); + for (dnscache::iterator i = this->cache->begin(); i != this->cache->end(); i++) + /* Dont include expired items (theres no point) */ + if (i->second.CalcTTLRemaining()) + newcache->insert(*i); + else + n++; + + delete this->cache; + this->cache = newcache; + ServerInstance->Log(DEBUG,"Prune %d expired cache items", n); + return n; +} + void DNS::Rehash() { insp_inaddr addr; @@ -276,14 +308,7 @@ void DNS::Rehash() this->SetFd(-1); /* Rehash the cache */ - dnscache* newcache = new dnscache(); - for (dnscache::iterator i = this->cache->begin(); i != this->cache->end(); i++) - /* Dont include expired items (theres no point) */ - if (i->second.CalcTTLRemaining()) - newcache->insert(*i); - - delete this->cache; - this->cache = newcache; + this->PruneCache(); } else { @@ -397,6 +422,10 @@ DNS::DNS(InspIRCd* Instance) : ServerInstance(Instance) /* Actually read the settings */ this->Rehash(); + + this->PruneTimer = new CacheTimer(ServerInstance, this); + + ServerInstance->Timers->AddTimer(this->PruneTimer); } /** Build a payload to be placed after the header, based upon input data, a resource type, a class and a pointer to a buffer */ @@ -906,6 +935,8 @@ DNS::~DNS() { shutdown(this->GetFd(), 2); close(this->GetFd()); + ServerInstance->Timers->DelTimer(this->PruneTimer); + delete this->PruneTimer; } CachedQuery* DNS::GetCache(const std::string &source) diff --git a/src/timer.cpp b/src/timer.cpp index 04ff1c15b..6be7aa06c 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -28,7 +28,14 @@ void TimerManager::TickTimers(time_t TIME) { InspTimer* n = *y; n->Tick(TIME); - DELETE(n); + if (n->GetRepeat()) + { + AddTimer(n, n->GetSecs()); + } + else + { + DELETE(n); + } } Timers.erase(found); @@ -78,7 +85,14 @@ void TimerManager::TickMissedTimers(time_t TIME) { InspTimer* z = *y; z->Tick(TIME); - DELETE(z); + if (z->GetRepeat()) + { + AddTimer(z, z->GetSecs()); + } + else + { + DELETE(z); + } } Timers.erase(found); @@ -87,12 +101,18 @@ void TimerManager::TickMissedTimers(time_t TIME) } } -void TimerManager::AddTimer(InspTimer* T) +void TimerManager::AddTimer(InspTimer* T, long secs_from_now) { timergroup* x = NULL; - timerlist::iterator found = Timers.find(T->GetTimer()); - + int time_to_trigger = 0; + if (!secs_from_now) + time_to_trigger = T->GetTimer(); + else + time_to_trigger = secs_from_now + time(NULL); + + timerlist::iterator found = Timers.find(time_to_trigger); + if (found != Timers.end()) { x = found->second; @@ -100,7 +120,7 @@ void TimerManager::AddTimer(InspTimer* T) else { x = new timergroup; - Timers[T->GetTimer()] = x; + Timers[time_to_trigger] = x; } x->push_back(T); -- cgit v1.2.3