-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
SessionManager: Allow backend storage to be configurable #481
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,50 +16,92 @@ | |
|
||
using namespace drogon; | ||
|
||
SessionManager::SessionManager(trantor::EventLoop *loop, size_t timeout) | ||
: loop_(loop), timeout_(timeout) | ||
class CacheMapSessionStorageProvider : public SessionStorageProvider | ||
{ | ||
assert(timeout_ >= 0); | ||
if (timeout_ > 0) | ||
public: | ||
CacheMapSessionStorageProvider(trantor::EventLoop *loop, | ||
float tickInterval, | ||
size_t wheelsNum, | ||
size_t bucketsNumPerWheel) | ||
: data_(std::make_unique<CacheMap<std::string, SessionPtr>>( | ||
loop, | ||
tickInterval, | ||
wheelsNum, | ||
bucketsNumPerWheel)) | ||
{ | ||
} | ||
|
||
bool findAndFetch(const std::string &key, SessionPtr &value) override | ||
{ | ||
return data_->findAndFetch(key, value); | ||
} | ||
|
||
void insert(const std::string &key, | ||
const SessionPtr &value, | ||
size_t timeout) override | ||
{ | ||
data_->insert(key, value, timeout); | ||
} | ||
|
||
private: | ||
std::unique_ptr<CacheMap<std::string, SessionPtr>> data_; | ||
}; | ||
|
||
SessionManager::SessionManager(std::unique_ptr<SessionStorageProvider> provider, | ||
size_t timeout) | ||
: provider_(std::move(provider)), timeout_(timeout) | ||
{ | ||
} | ||
|
||
SessionPtr SessionManager::getSession(const std::string &sessionID, | ||
bool needToSet) | ||
{ | ||
assert(!sessionID.empty()); | ||
SessionPtr sessionPtr; | ||
std::lock_guard<std::mutex> lock(mapMutex_); | ||
if (!provider_->findAndFetch(sessionID, sessionPtr)) | ||
{ | ||
sessionPtr = std::make_shared<Session>(sessionID, needToSet); | ||
provider_->insert(sessionID, sessionPtr, timeout_); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the provider_ is created for redis, how do we store the sessionPtr to the redis server? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we have to make the Session class as a pure virtual class for different implementations of memory storage or redis storage. But how to deal with the template and the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Protobuf for session fields serialization/deserialization? |
||
return sessionPtr; | ||
} | ||
return sessionPtr; | ||
} | ||
|
||
SessionStorageProvider::~SessionStorageProvider() = default; | ||
|
||
std::unique_ptr<SessionStorageProvider> drogon::createCacheMapProvider( | ||
trantor::EventLoop *loop, | ||
size_t timeout) | ||
{ | ||
assert(timeout >= 0); | ||
if (timeout > 0) | ||
{ | ||
size_t wheelNum = 1; | ||
size_t bucketNum = 0; | ||
if (timeout_ < 500) | ||
if (timeout < 500) | ||
{ | ||
bucketNum = timeout_ + 1; | ||
bucketNum = timeout + 1; | ||
} | ||
else | ||
{ | ||
auto tmpTimeout = timeout_; | ||
auto tmpTimeout = timeout; | ||
bucketNum = 100; | ||
while (tmpTimeout > 100) | ||
{ | ||
++wheelNum; | ||
tmpTimeout = tmpTimeout / 100; | ||
} | ||
} | ||
sessionMapPtr_ = std::unique_ptr<CacheMap<std::string, SessionPtr>>( | ||
new CacheMap<std::string, SessionPtr>( | ||
loop_, 1.0, wheelNum, bucketNum)); | ||
return std::make_unique<CacheMapSessionStorageProvider>(loop, | ||
1.0, | ||
wheelNum, | ||
bucketNum); | ||
} | ||
else if (timeout_ == 0) | ||
else if (timeout == 0) | ||
{ | ||
sessionMapPtr_ = std::unique_ptr<CacheMap<std::string, SessionPtr>>( | ||
new CacheMap<std::string, SessionPtr>(loop_, 0, 0, 0)); | ||
return std::make_unique<CacheMapSessionStorageProvider>(loop, 0, 0, 0); | ||
} | ||
} | ||
|
||
SessionPtr SessionManager::getSession(const std::string &sessionID, | ||
bool needToSet) | ||
{ | ||
assert(!sessionID.empty()); | ||
SessionPtr sessionPtr; | ||
std::lock_guard<std::mutex> lock(mapMutex_); | ||
if (sessionMapPtr_->findAndFetch(sessionID, sessionPtr) == false) | ||
{ | ||
sessionPtr = std::make_shared<Session>(sessionID, needToSet); | ||
sessionMapPtr_->insert(sessionID, sessionPtr, timeout_); | ||
return sessionPtr; | ||
} | ||
return sessionPtr; | ||
} | ||
return nullptr; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.