scheduler canceling, sessiion query, didn't test yet!

This commit is contained in:
Blue 2024-01-03 19:20:01 -03:00
parent 544db92b6e
commit 5d765958e5
Signed by: blue
GPG key ID: 9B203B252A63EE38
15 changed files with 166 additions and 21 deletions

View file

@ -192,6 +192,22 @@ Session& Server::openSession(const std::string& login) {
if (sessionId == 0)
throw std::runtime_error("Couldn't create session, ran out of attempts");
std::unique_ptr<Session>& session = sessions[accessToken] = std::make_unique<Session>(sessionId, accessToken, renewToken);
std::unique_ptr<Session>& session = sessions[accessToken] = std::make_unique<Session>(scheduler, sessionId, accessToken, renewToken);
return *session.get();
}
Session& Server::getSession (const std::string& accessToken) {
Sessions::const_iterator itr = sessions.find(accessToken);
if (itr != sessions.end())
return *(itr->second);
DB::Resource db = pool->request();
DB::Session s = db->findSession(accessToken);
std::unique_ptr<Session>& session = sessions[accessToken] = std::make_unique<Session>(
scheduler,
s.id,
s.accessToken,
s.renewToken
);
return *session.get();
}

View file

@ -15,9 +15,17 @@ Session::Session(
id(id),
access(access),
renew(renew),
polling(nullptr)
polling(nullptr),
timeoutId(TM::Scheduler::none)
{}
Session::~Session () {
if (timeoutId != TM::Scheduler::none) {
if (std::shared_ptr<TM::Scheduler> sch = scheduler.lock())
sch->cancel(timeoutId);
}
}
std::string Session::getAccessToken() const {
return access;
}
@ -27,18 +35,30 @@ std::string Session::getRenewToken() const {
}
void Session::accept(std::unique_ptr<Request> request) {
std::shared_ptr<TM::Scheduler> sch = scheduler.lock();
if (polling) {
Handler::Poll::error(*request.get(), Handler::Poll::Result::replace, Response::Status::ok);
if (timeoutId != TM::Scheduler::none) {
if (sch)
sch->cancel(timeoutId);
timeoutId = TM::Scheduler::none;
}
//TODO unschedule
}
std::shared_ptr<TM::Scheduler> sch = scheduler.lock();
if (!sch) {
std::cerr << "Was unable to schedule polling timeout, replying with an error" << std::endl;
Handler::Poll::error(*request.get(), Handler::Poll::Result::unknownError, Response::Status::internalError);
return;
}
sch->schedule(std::bind(&Session::onTimeout, this), TM::Scheduler::Delay(5000));
timeoutId = sch->schedule(std::bind(&Session::onTimeout, this), TM::Scheduler::Delay(5000));
polling = std::move(request);
}
void Session::onTimeout () {
timeoutId = TM::Scheduler::none;
Handler::Poll::error(*polling.get(), Handler::Poll::Result::timeout, Response::Status::ok);
polling.reset();
}

View file

@ -18,6 +18,7 @@ public:
);
Session(const Session&) = delete;
Session(Session&& other);
~Session();
Session& operator = (const Session&) = delete;
Session& operator = (Session&& other);
@ -34,4 +35,5 @@ private:
std::string access;
std::string renew;
std::unique_ptr<Request> polling;
TM::Record::ID timeoutId;
};