Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some interface for sessions #1942

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,8 @@ set(DROGON_MONITORING_HEADERS
lib/inc/drogon/utils/monitoring/Collector.h
lib/inc/drogon/utils/monitoring/Sample.h
lib/inc/drogon/utils/monitoring/Gauge.h
lib/inc/drogon/utils/monitoring/Histogram.h)
lib/inc/drogon/utils/monitoring/Histogram.h
lib/inc/drogon/utils/monitoring/StopWatch.h)

install(FILES ${DROGON_MONITORING_HEADERS}
DESTINATION ${INSTALL_INCLUDE_DIR}/drogon/utils/monitoring)
Expand Down
14 changes: 14 additions & 0 deletions lib/inc/drogon/CacheMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,20 @@ class CacheMap
fnOnInsert_(key);
}

/**
* @brief Get the all values in the cache.
* */
std::vector<T2> getAllValues()
{
std::vector<T2> ret;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A reserve here would be beneficial, after the lock to make sure the map size is locked in place as well

std::lock_guard<std::mutex> lock(mtx_);
for (auto &pair : map_)
{
ret.push_back(pair.second.value_);
}
return ret;
}

/**
* @brief Return the value of the keyword.
*
Expand Down
18 changes: 17 additions & 1 deletion lib/inc/drogon/HttpAppFramework.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class DROGON_EXPORT HttpAppFramework : public trantor::NonCopyable
virtual void run() = 0;

/// Return true if the framework is running
virtual bool isRunning() = 0;
virtual bool isRunning() const = 0;

/// Quit the event loop
/**
Expand Down Expand Up @@ -844,6 +844,22 @@ class DROGON_EXPORT HttpAppFramework : public trantor::NonCopyable
int maxAge = -1,
std::function<std::string()> idGeneratorCallback = nullptr) = 0;

/**
* @brief Insert a session into the session manager within the framework.
*
* @note This method should be called after the framework runs. Users
* usually don't need to call this method because all sessions are managed
* by the framework.
* */
virtual void insertSession(const SessionPtr &session) = 0;

/**
* @brief Get all sessions in the session manager within the framework.
*
* @note This method should be called after the framework runs.
* */
virtual std::vector<SessionPtr> getAllSessions() const = 0;

/// A wrapper of the above method.
/**
* Example: Users can set the timeout value as follows:
Expand Down
40 changes: 40 additions & 0 deletions lib/src/HttpAppFrameworkImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1259,3 +1259,43 @@ HttpAppFramework &HttpAppFrameworkImpl::registerPreSendingAdvice(
AopAdvice::instance().registerPreSendingAdvice(advice);
return *this;
}

void HttpAppFrameworkImpl::insertSession(const SessionPtr &session)
{
if (useSession_)
{
if (isRunning() && sessionManagerPtr_)
{
sessionManagerPtr_->insertSession(session);
}
else
{
LOG_ERROR << "Session manager is not initialized!";
}
}
else
{
LOG_ERROR << "Session is not enabled!";
}
}

std::vector<SessionPtr> HttpAppFrameworkImpl::getAllSessions() const
{
if (useSession_)
{
if (isRunning() && sessionManagerPtr_)
{
return sessionManagerPtr_->getAllSessions();
}
else
{
LOG_ERROR << "Session manager is not initialized!";
return {};
}
}
else
{
LOG_ERROR << "Session is not enabled!";
return {};
}
}
6 changes: 5 additions & 1 deletion lib/src/HttpAppFrameworkImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ class HttpAppFrameworkImpl final : public HttpAppFramework
return setSessionIdGenerator(idGeneratorCallback);
}

void insertSession(const SessionPtr &session) override;

std::vector<SessionPtr> getAllSessions() const override;

HttpAppFramework &setSessionIdGenerator(
SessionManager::IdGeneratorCallback idGeneratorCallback = nullptr)
{
Expand Down Expand Up @@ -448,7 +452,7 @@ class HttpAppFrameworkImpl final : public HttpAppFramework

~HttpAppFrameworkImpl() noexcept override;

bool isRunning() override
bool isRunning() const override
{
return running_;
}
Expand Down
11 changes: 11 additions & 0 deletions lib/src/SessionManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ SessionPtr SessionManager::getSession(const std::string &sessionID,
return sessionPtr;
}

void SessionManager::insertSession(const SessionPtr &sessionPtr)
{
assert(!sessionPtr->sessionId().empty());
sessionMapPtr_->insert(sessionPtr->sessionId(), sessionPtr, timeout_);
}

std::vector<SessionPtr> SessionManager::getAllSessions() const
{
return sessionMapPtr_->getAllValues();
}

void SessionManager::changeSessionId(const SessionPtr &sessionPtr)
{
auto oldId = sessionPtr->sessionId();
Expand Down
2 changes: 2 additions & 0 deletions lib/src/SessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class SessionManager : public trantor::NonCopyable

SessionPtr getSession(const std::string &sessionID, bool needToSet);
void changeSessionId(const SessionPtr &sessionPtr);
void insertSession(const SessionPtr &sessionPtr);
std::vector<SessionPtr> getAllSessions() const;

private:
std::unique_ptr<CacheMap<std::string, SessionPtr>> sessionMapPtr_;
Expand Down
Loading