]> jfr.im git - irc/rizon/znc.git/commitdiff
Write config files less often with chansaver
authorAdam <redacted>
Tue, 8 Mar 2016 21:42:44 +0000 (16:42 -0500)
committerAdam <redacted>
Tue, 8 Mar 2016 21:42:44 +0000 (16:42 -0500)
main.cpp
modules/chansaver.cpp
znc.cpp
znc.h

index 17bbbd2f6aae4fe7873e95b04a3a3d1cdf93389e..2f875611482e5290a8223694f43ed54ae5b7a2de 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -79,7 +79,7 @@ static void signalHandler(int sig) {
                break;
        case SIGUSR1:
                CUtils::PrintMessage("Caught SIGUSR1");
-               CZNC::Get().SetConfigState(CZNC::ECONFIG_NEED_WRITE);
+               CZNC::Get().SetConfigState(CZNC::ECONFIG_NEED_VERBOSE_WRITE);
                break;
        default:
                CUtils::PrintMessage("WTF? Signal handler called for a signal it doesn't know?");
index 70250206234f245b6ef106ba803709aeba979ae5..78b95481068bece64fc63ec8c5c88bc9b815a4f1 100644 (file)
@@ -17,8 +17,6 @@ public:
                vector<CChan*>::const_iterator it = vChans.begin();
                vector<CChan*>::const_iterator end = vChans.end();
 
-               m_bWriteConf = false;
-
                for (; it != end; ++it) {
                        CChan *pChan = *it;
 
@@ -26,7 +24,7 @@ public:
                        // we'll have to add it...
                        if (!pChan->InConfig()) {
                                pChan->SetInConfig(true);
-                               m_bWriteConf = true;
+                               CZNC::Get().SetConfigState(CZNC::ECONFIG_WANT_WRITE);
                        }
                }
        }
@@ -34,15 +32,6 @@ public:
        virtual ~CChanSaverMod() {
        }
 
-       virtual EModRet OnRaw(CString& sLine) {
-               if (m_bWriteConf) {
-                       CZNC::Get().WriteConfig();
-                       m_bWriteConf = false;
-               }
-
-               return CONTINUE;
-       }
-
        virtual void OnMode(const CNick& OpNick, CChan& Channel, char uMode, const CString& sArg, bool bAdded, bool bNoChange) {
                // This is called when we join (ZNC requests the channel modes
                // on join) *and* when someone changes the channel keys.
@@ -51,20 +40,20 @@ public:
                        return;
 
                Channel.SetKey(sArg);
-               m_bWriteConf = true;
+               CZNC::Get().SetConfigState(CZNC::ECONFIG_WANT_WRITE);
        }
 
        virtual void OnJoin(const CNick& Nick, CChan& Channel) {
                if (Nick.GetNick() == m_pUser->GetIRCNick().GetNick() && !Channel.InConfig()) {
                        Channel.SetInConfig(true);
-                       CZNC::Get().WriteConfig();
+                       CZNC::Get().SetConfigState(CZNC::ECONFIG_WANT_WRITE);
                }
        }
 
        virtual void OnPart(const CNick& Nick, CChan& Channel, const CString& sMessage) {
                if (Nick.GetNick() == m_pUser->GetIRCNick().GetNick() && Channel.InConfig()) {
                        Channel.SetInConfig(false);
-                       CZNC::Get().WriteConfig();
+                       CZNC::Get().SetConfigState(CZNC::ECONFIG_WANT_WRITE);
                }
        }
 
@@ -73,12 +62,9 @@ public:
                if (sKickedNick == m_pUser->GetIRCNick().GetNick() && Channel.InConfig())
                {
                        Channel.SetInConfig(false);
-                       CZNC::Get().WriteConfig();
+                       CZNC::Get().SetConfigState(CZNC::ECONFIG_WANT_WRITE);
                }
        }
-
-private:
-       bool m_bWriteConf;
 };
 
 template<> void TModInfo<CChanSaverMod>(CModInfo& Info) {
diff --git a/znc.cpp b/znc.cpp
index d2b99e0e034d12d187d9013d3b4ad8e89c1a900e..c6869b9b4ef2879e6d5259990047d0ec53bd4c09 100644 (file)
--- a/znc.cpp
+++ b/znc.cpp
@@ -16,6 +16,8 @@
 #include "Config.h"
 #include <list>
 
+#define CONFIG_WRITE_INTERVAL (60 * 5)
+
 static inline CString FormatBindError() {
        CString sError = (errno == 0 ? CString("unknown error, check the host name") : CString(strerror(errno)));
        return "Unable to bind [" + sError + "]";
@@ -39,6 +41,7 @@ CZNC::CZNC() {
        m_sConnectThrottle.SetTTL(30000);
        m_pLockFile = NULL;
        m_bProtectWebSessions = true;
+       m_pConfigTimer = NULL;
 }
 
 CZNC::~CZNC() {
@@ -187,11 +190,27 @@ bool CZNC::HandleUserDeletion()
        return true;
 }
 
+class CConfigWriteTimer : public CCron {
+ public:
+       CConfigWriteTimer(int iSecs) : CCron() {
+               SetName("Config write timer");
+               Start(iSecs);
+       }
+
+ protected:
+       void RunJob() {
+               CZNC::Get().SetConfigState(CZNC::ECONFIG_NEED_WRITE);
+
+               CZNC::Get().DisableConfigTimer();
+       }
+};
+
 void CZNC::Loop() {
        while (true) {
                CString sError;
 
-               switch (GetConfigState()) {
+               ConfigState eState = GetConfigState();
+               switch (eState) {
                case ECONFIG_NEED_REHASH:
                        SetConfigState(ECONFIG_NOTHING);
 
@@ -202,13 +221,22 @@ void CZNC::Loop() {
                                Broadcast("ZNC is in some possibly inconsistent state!", true);
                        }
                        break;
+               case ECONFIG_WANT_WRITE:
+                       SetConfigState(ECONFIG_NOTHING);
+
+                       if (m_pConfigTimer == NULL) {
+                               m_pConfigTimer = new CConfigWriteTimer(CONFIG_WRITE_INTERVAL);
+                               GetManager().AddCron(m_pConfigTimer);
+                       }
+                       break;
                case ECONFIG_NEED_WRITE:
+               case ECONFIG_NEED_VERBOSE_WRITE:
                        SetConfigState(ECONFIG_NOTHING);
 
-                       if (WriteConfig()) {
-                               Broadcast("Writing the config succeeded", true);
-                       } else {
+                       if (!WriteConfig()) {
                                Broadcast("Writing the config file failed", true);
+                       } else if (eState == ECONFIG_NEED_VERBOSE_WRITE) {
+                               Broadcast("Writing the config succeeded", true);
                        }
                        break;
                case ECONFIG_NOTHING:
@@ -1739,3 +1767,10 @@ void CZNC::LeakConnectUser(CConnectUserTimer *pTimer) {
 bool CZNC::WaitForChildLock() {
        return m_pLockFile && m_pLockFile->ExLock();
 }
+
+void CZNC::DisableConfigTimer() {
+       if (m_pConfigTimer) {
+               m_pConfigTimer->Stop();
+               m_pConfigTimer = NULL;
+       }
+}
diff --git a/znc.h b/znc.h
index fc56b8bda216898953938dad50ad69737757f42f..63f4020cb2ba9586fab6cb49da5d2ca824f13392 100644 (file)
--- a/znc.h
+++ b/znc.h
@@ -20,6 +20,7 @@ using std::map;
 class CListener;
 class CUser;
 class CConnectUserTimer;
+class CConfigWriteTimer;
 class CConfig;
 class CFile;
 
@@ -31,7 +32,9 @@ public:
        enum ConfigState {
                ECONFIG_NOTHING,
                ECONFIG_NEED_REHASH,
-               ECONFIG_NEED_WRITE
+               ECONFIG_NEED_WRITE,
+               ECONFIG_NEED_VERBOSE_WRITE,
+               ECONFIG_WANT_WRITE
        };
 
        void DeleteUsers();
@@ -145,6 +148,8 @@ public:
        // Never call this unless you are CConnectUserTimer::~CConnectUserTimer()
        void LeakConnectUser(CConnectUserTimer *pTimer);
 
+       void DisableConfigTimer();
+
        static void DumpConfig(const CConfig* Config);
 
 private:
@@ -184,6 +189,7 @@ protected:
        CConnectUserTimer     *m_pConnectUserTimer;
        TCacheMap<CString>     m_sConnectThrottle;
        bool                   m_bProtectWebSessions;
+       CConfigWriteTimer     *m_pConfigTimer;
 };
 
 #endif // !_ZNC_H