Commit 8c6bb4d4 authored by jan.koester's avatar jan.koester
Browse files

test

parent 3cc475a8
Loading
Loading
Loading
Loading
+27 −0
Changes for src/auth.cpp: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -38,6 +38,9 @@
#include <httppp/exception.h>

#include "auth.h"

std::mutex blogi::Auth::_authCacheMutex;
std::unordered_map<std::string, std::chrono::steady_clock::time_point> blogi::Auth::_authCache;
#include "conf.h"
#include "theme.h"
#include "session.h"
@@ -182,6 +185,19 @@ void blogi::Auth::Apilogin(const int tid,const char *username,const char *passwo
}

bool blogi::Auth::isLoggedIn(const int tid,const uuid::uuid &authid){
      std::string key = authid.c_str();

      // Check cache first — avoids network round-trip to authdb
      {
          std::lock_guard<std::mutex> lk(_authCacheMutex);
          auto it = _authCache.find(key);
          if (it != _authCache.end()) {
              if (std::chrono::steady_clock::now() <= it->second)
                  return true;
              _authCache.erase(it);
          }
      }

      for(size_t i = 0; i < _config.getAuthSourceCount(); ++i){
          const AuthSource &src = _config.getAuthSource(i);
          try {
@@ -200,6 +216,17 @@ bool blogi::Auth::isLoggedIn(const int tid,const uuid::uuid &authid){
              }

              if(clt->ClientAuth() && clt->GPOcheck("e7d3b8b3-4825-11f0-ae2b-3cecefce9cb6")){
                    // Cache successful auth
                    std::lock_guard<std::mutex> lk(_authCacheMutex);
                    _authCache[key] = std::chrono::steady_clock::now()
                                    + std::chrono::seconds(AUTH_CACHE_TTL_SECONDS);
                    if (_authCache.size() > 10000) {
                        auto now = std::chrono::steady_clock::now();
                        for (auto ci = _authCache.begin(); ci != _authCache.end(); ) {
                            if (now > ci->second) ci = _authCache.erase(ci);
                            else ++ci;
                        }
                    }
                    return true;
              }
          } catch(...) {
+8 −0
Changes for src/auth.h: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -26,6 +26,9 @@
 *******************************************************************************/

#include <cstdio>
#include <chrono>
#include <mutex>
#include <unordered_map>

#include <authdb/client.h>
#include <uuidp.h>
@@ -54,5 +57,10 @@ namespace blogi {
                      uuid::uuid &authid, const int tid, const std::string *ssid);
        std::vector<std::unique_ptr<dbpp::Database>> &_dbconn;
        const blogi::Config                                                &_config;

        // Cache auth results to avoid network round-trips on every check
        static constexpr int AUTH_CACHE_TTL_SECONDS = 300;
        static std::mutex _authCacheMutex;
        static std::unordered_map<std::string, std::chrono::steady_clock::time_point> _authCache;
    };
};
+36 −1
Changes for src/session.cpp: 36 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -36,6 +36,9 @@
#include "session.h"
#include <time.h>

std::mutex blogi::Session::_cacheMutex;
std::unordered_map<std::string, std::chrono::steady_clock::time_point> blogi::Session::_sessionCache;

blogi::Session::Session(const dbpp::Database &db) : _sessiondb(db){
}

@@ -88,6 +91,14 @@ const std::string &blogi::Session::createSession(std::string &buf,const std::str
        excep[libhttppp::HTTPException::Error] << "createSession DbError: " << err;
        throw excep;
    }

    // Pre-populate session cache
    {
        std::lock_guard<std::mutex> lk(_cacheMutex);
        _sessionCache[buf] = std::chrono::steady_clock::now()
                           + std::chrono::seconds(CACHE_TTL_SECONDS);
    }

    return buf;
};

@@ -166,14 +177,38 @@ bool blogi::Session::exists(const std::string& sessionid){
        throw excep;
    }

    // Check in-memory cache first
    {
        std::lock_guard<std::mutex> lk(_cacheMutex);
        auto it = _sessionCache.find(sessionid);
        if (it != _sessionCache.end()) {
            if (std::chrono::steady_clock::now() <= it->second)
                return true;
            _sessionCache.erase(it);
        }
    }

    dbpp::DBResult res;
    std::vector<char> sbuf;

    dbpp::SQL sql;
    sql << "Select id from session where id='" <<  dbpp::SQL::escaped(sbuf,sessionid.c_str()) <<"'";

    if(_sessiondb.exec(sql,res)>0)
    if(_sessiondb.exec(sql,res)>0) {
        // Cache the result
        std::lock_guard<std::mutex> lk(_cacheMutex);
        _sessionCache[sessionid] = std::chrono::steady_clock::now()
                                 + std::chrono::seconds(CACHE_TTL_SECONDS);
        // Evict expired entries if map grows too large
        if (_sessionCache.size() > 10000) {
            auto now = std::chrono::steady_clock::now();
            for (auto ci = _sessionCache.begin(); ci != _sessionCache.end(); ) {
                if (now > ci->second) ci = _sessionCache.erase(ci);
                else ++ci;
            }
        }
        return true;
    }

    return false;
}
+9 −0
Changes for src/session.h: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -26,6 +26,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/

#include <string>
#include <chrono>
#include <mutex>
#include <unordered_map>

#include <uuidp.h>

@@ -51,6 +54,12 @@ namespace blogi {
    void            _initSession();

    const dbpp::Database &_sessiondb;

    // In-memory session cache to avoid DB round-trip on every request
    static constexpr int CACHE_TTL_SECONDS = 120;
    static std::mutex _cacheMutex;
    static std::unordered_map<std::string, std::chrono::steady_clock::time_point> _sessionCache;

    friend class Blogi;
  };
};