From 4ff619cabe92850416561947246ddca0a4d4cad9 Mon Sep 17 00:00:00 2001 From: BtbN Date: Tue, 8 Aug 2017 15:37:11 +0200 Subject: [PATCH] Fix high CPU and disk load See https://github.com/jpnurmi/znc-clientbuffer/issues/9 Adapted from https://github.com/MAGICCC/znc-clientbuffer/commit/dd848d4c50d94723546d8df4cd723a5022350018 --- clientbuffer.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/clientbuffer.cpp b/clientbuffer.cpp index 8f6fa7e..0810e94 100644 --- a/clientbuffer.cpp +++ b/clientbuffer.cpp @@ -34,6 +34,19 @@ #define ZNC17 0 #endif +class CClientBufferCacheJob : public CTimer +{ +public: + CClientBufferCacheJob(CModule* pModule, unsigned int uInterval, unsigned int uCycles, const CString& sLabel, const CString& sDescription) + : CTimer(pModule, uInterval, uCycles, sLabel, sDescription) {} + virtual ~CClientBufferCacheJob() {} + +protected: + virtual void RunJob() { + GetModule()->SaveRegistry(); + } +}; + class CClientBufferMod : public CModule { public: @@ -43,6 +56,7 @@ class CClientBufferMod : public CModule AddCommand("AddClient", static_cast(&CClientBufferMod::OnAddClientCommand), "", "Add a client."); AddCommand("DelClient", static_cast(&CClientBufferMod::OnDelClientCommand), "", "Delete a client."); AddCommand("ListClients", static_cast(&CClientBufferMod::OnListClientsCommand), "", "List known clients."); + AddTimer(new CClientBufferCacheJob(this, 60 /* sec */, 0, "ClientBufferCache", "Periodically save ClientBuffer registry to disk")); } bool OnLoad(const CString& sArgs, CString& sErrorMsg) override { @@ -367,7 +381,7 @@ CModule::EModRet CClientBufferMod::OnPrivBufferPlayLine2(CClient& client, CStrin /// Returns true upon success. bool CClientBufferMod::AddClient(const CString& identifier) { - return SetNV(identifier, ""); + return SetNV(identifier, "", false); } /// Remove a client identifier. @@ -382,7 +396,7 @@ bool CClientBufferMod::DelClient(const CString& identifier) } bool success = true; for (const CString& key : keys) - success &= DelNV(key); + success &= DelNV(key, false); return success; } @@ -469,7 +483,7 @@ bool CClientBufferMod::SetTimestamp(const CString& identifier, const CString& ta { char timestamp[32]; std::snprintf(timestamp, 32, "%lld.%06ld", (long long)tv.tv_sec, (long)tv.tv_usec); - return SetNV(identifier + "/" + target, timestamp); + return SetNV(identifier + "/" + target, timestamp, false); } /// Returns true if the given timestamp is not greater than the "last