Browse Source

delete client cached values after timeout.

JSON-156
Roker 2 years ago
parent
commit
9e13fcc6d5
4 changed files with 37 additions and 4 deletions
  1. +1
    -1
      server/json-adapter.cc
  2. +2
    -1
      server/server_version.cc
  3. +23
    -0
      server/session_registry.cc
  4. +11
    -2
      server/session_registry.hh

+ 1
- 1
server/json-adapter.cc View File

@ -258,7 +258,7 @@ void JsonAdapter::prepare_run(const std::string& address, unsigned start_port, u
{
check_guard();
// delayed after constructor, so virtual functions are working:
i->session_registry.reset(new SessionRegistry(messageToSend ? messageToSend : this->getMessageToSend(), this->getInjectSyncEvent()));
i->session_registry.reset(new SessionRegistry(messageToSend ? messageToSend : this->getMessageToSend(), this->getInjectSyncEvent(), i->client_session_timeout));
for(unsigned short port = start_port; port<=end_port; ++port)
{


+ 2
- 1
server/server_version.cc View File

@ -76,7 +76,8 @@ static const std::string VersionName =
// "(42) Gotha"; // JSON-152: 2-parameter version of pollForEvents().
// "(43) Wandersleben"; // JSON-153 passphrase support. *sigh*
// "(44) Neudietendorf"; // replace my own sync thread code by libpEpAdapter's implementation.
"(45) Kreuz Erfurt"; // fix of context-saved function parameters that would cause trouble when >1 request is processed in parallel.
// "(45) Kreuz Erfurt"; // fix of context-saved function parameters that would cause trouble when >1 request is processed in parallel.
"(46) Erfurt-West"; // JSON-156: delete client cached values after timeout.
} // end of anonymous namespace
////////////////////////////////////////////////////////////////////////////


+ 23
- 0
server/session_registry.cc View File

@ -10,6 +10,8 @@ PEP_SESSION SessionRegistry::get(std::thread::id tid, const std::string& client_
{
Lock L(_mtx);
update_last_use(client_id);
auto q = m.find(tid);
if(q != m.end())
{
@ -69,6 +71,7 @@ void SessionRegistry::add_to_cache(const std::string& client_id, const std::stri
Lock L(_mtx);
Log.debug("add_to_cache(\"%s\", \"%s\")", client_id.c_str(), fn_name.c_str());
cache[client_id][fn_name] = func;
update_last_use(client_id);
}
@ -83,3 +86,23 @@ std::string SessionRegistry::to_string() const
}
return ss.str();
}
void SessionRegistry::update_last_use(const std::string& client_id)
{
const auto now = std::chrono::system_clock::now();
const auto too_old = now - std::chrono::seconds(client_timeout);
last_use[client_id] = now;
// TODO: replace by C++20 std::erase_if()
for(auto q = last_use.begin(); q != last_use.end(); /* no increment here */ )
{
if(q->second < too_old)
{
cache.erase( q->first );
q = last_use.erase(q);
}else{
++q;
}
}
}

+ 11
- 2
server/session_registry.hh View File

@ -1,6 +1,7 @@
#ifndef JSON_ADAPTER_SESSION_REGISTRY_HH
#define JSON_ADAPTER_SESSION_REGISTRY_HH
#include <chrono>
#include <map>
#include <mutex>
#include <thread>
@ -11,10 +12,11 @@
class SessionRegistry
{
public:
SessionRegistry(messageToSend_t _mts, inject_sync_event_t _ise)
SessionRegistry(messageToSend_t _mts, inject_sync_event_t _ise, int _client_timeout)
: mts{_mts}
, ise{_ise}
, Log{"SR"}
, client_timeout{_client_timeout} // in seconds
{}
// calls "init" for the given thread if no PEP_SESSION exists, yet for the given thread
@ -36,16 +38,23 @@ private:
std::map<std::thread::id, PEP_SESSION> m;
messageToSend_t mts;
inject_sync_event_t ise;
Logger Log;
Logger Log;
int client_timeout; // in seconds
// function name -> functor
typedef
std::map<std::string, std::function<void(PEP_SESSION)>> cache_per_client_t;
// key=client_id
std::map<std::string, cache_per_client_t> cache;
std::map<std::string, std::chrono::time_point<std::chrono::system_clock> > last_use;
typedef std::recursive_mutex Mutex;
typedef std::unique_lock<Mutex> Lock;
mutable Mutex _mtx;
void update_last_use(const std::string& client_id);
};
#endif // JSON_ADAPTER_SESSION_REGISTRY_HH

Loading…
Cancel
Save