added: gc for sessions (another thread)
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@693 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
@@ -17,6 +17,7 @@ SessionContainer::SessionContainer()
|
||||
{
|
||||
request = 0;
|
||||
table_size = 0;
|
||||
work_mode = 1; // threading work mode
|
||||
}
|
||||
|
||||
|
||||
@@ -28,27 +29,27 @@ void SessionContainer::SetRequest(Request * prequest)
|
||||
}
|
||||
|
||||
|
||||
void SessionContainer::SetConfig(Config * pconfig)
|
||||
{
|
||||
config = pconfig;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void SessionContainer::Clear()
|
||||
{
|
||||
Table::iterator inext, i = table.begin();
|
||||
Table::iterator i = table.begin();
|
||||
|
||||
// don't use table.clear();
|
||||
// because plugins session data would not be erased
|
||||
while( i != table.end() )
|
||||
{
|
||||
inext = i;
|
||||
++inext;
|
||||
|
||||
request->session = &(*i);
|
||||
table.erase(i);
|
||||
|
||||
i = inext;
|
||||
table.erase(i++);
|
||||
}
|
||||
|
||||
// erasing all indexes
|
||||
// erasing indexes
|
||||
index_id.clear();
|
||||
index_time.clear();
|
||||
table_size = 0;
|
||||
request->session = 0;
|
||||
}
|
||||
@@ -60,7 +61,7 @@ void SessionContainer::SetLastContainer(LastContainer * plast_container)
|
||||
}
|
||||
|
||||
|
||||
SessionContainer::TableSize SessionContainer::Size()
|
||||
size_t SessionContainer::Size()
|
||||
{
|
||||
// don't use table.size() as it has O(n) complexity on FreeBSD
|
||||
return table_size;
|
||||
@@ -88,13 +89,16 @@ Session & SessionContainer::Back()
|
||||
|
||||
bool SessionContainer::PushBack(const Session & session)
|
||||
{
|
||||
if( index_id.find(session.id) != index_id.end() )
|
||||
// that element already exists
|
||||
std::pair<IndexId::iterator, bool> index_id_res = index_id.insert( std::make_pair(session.id, table.end()) );
|
||||
|
||||
if( !index_id_res.second )
|
||||
{
|
||||
// that element already exists (was not inserted now)
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
Iterator last = table.insert(table.end(), session);
|
||||
index_id.insert( std::make_pair(session.id, last) );
|
||||
index_time.insert( std::make_pair(session.last_time, last) );
|
||||
index_id_res.first->second = last;
|
||||
table_size += 1;
|
||||
|
||||
log << log3 << "SC: added session, id: " << session.id << logend;
|
||||
@@ -119,93 +123,92 @@ return i->second;
|
||||
|
||||
|
||||
|
||||
size_t SessionContainer::DelFirstByTimeInterval(time_t interval, size_t how_many_max, bool skip_remember_flag)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
*
|
||||
* sessions gc (another thread)
|
||||
*
|
||||
*
|
||||
*/
|
||||
void SessionContainer::Work()
|
||||
{
|
||||
IndexTime::iterator i = index_time.begin();
|
||||
IndexTime::iterator iold;
|
||||
time_t limit = std::time(0) - interval;
|
||||
Session * old_session = request->session;
|
||||
size_t deleted = 0;
|
||||
bool exit = false;
|
||||
IndexId::iterator i;
|
||||
|
||||
while( i != index_time.end() && i->second->last_time < limit && deleted < how_many_max )
|
||||
Lock();
|
||||
i = index_id.begin();
|
||||
Unlock();
|
||||
|
||||
while( !exit )
|
||||
{
|
||||
//long id = i->second->id; // !! for logs (not used now)
|
||||
iold = i;
|
||||
++i; // incrementing before deleting old one
|
||||
|
||||
if( skip_remember_flag && iold->second->puser && iold->second->remember_me )
|
||||
// don't delete sessions which have 'remember_me' flag (and a user is logged)
|
||||
continue;
|
||||
Lock();
|
||||
|
||||
if( iold->second->puser )
|
||||
last_container->UserLogout(iold->second->puser->id, iold->second->id);
|
||||
|
||||
// we're logging session.id (call this before table.erase())
|
||||
DelFromIdIndex(iold->second);
|
||||
if( i == index_id.end() )
|
||||
{
|
||||
i = index_id.begin();
|
||||
WaitForSignalSleep(30);
|
||||
}
|
||||
else
|
||||
{
|
||||
if( IsSessionOutdated(*i->second) )
|
||||
DeleteSession(i++);
|
||||
else
|
||||
++i;
|
||||
}
|
||||
|
||||
request->session = &(*iold->second);
|
||||
table.erase(iold->second);
|
||||
index_time.erase(iold);
|
||||
table_size -= 1;
|
||||
request->session = 0;
|
||||
deleted += 1;
|
||||
|
||||
//log << log3 << "SC: deleted index_time for session id: " << id << logend;
|
||||
//log << log3 << "SC: deleted session, id: " << id << logend;
|
||||
exit = synchro->was_stop_signal;
|
||||
Unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// it's called from the other thread (with Lock and Unlock)
|
||||
bool SessionContainer::IsSessionOutdated(const Session & s) const
|
||||
{
|
||||
bool outdated;
|
||||
|
||||
if( s.remember_me )
|
||||
outdated = s.last_time < std::time(0) - config->session_remember_max_idle;
|
||||
else
|
||||
outdated = s.last_time < std::time(0) - config->session_max_idle;
|
||||
|
||||
return outdated;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// it's called from the other thread (with Lock and Unlock)
|
||||
void SessionContainer::DeleteSession(SessionContainer::IndexId::iterator i)
|
||||
{
|
||||
Session * old_session = request->session;
|
||||
|
||||
request->session = &(*i->second);
|
||||
|
||||
//log << log3 << "SessionContainer: deleting outdated session, id: " << i->second->id << logend;
|
||||
|
||||
if( i->second->puser )
|
||||
last_container->UserLogout(i->second->puser->id, i->second->id);
|
||||
|
||||
table.erase(i->second);
|
||||
index_id.erase(i);
|
||||
table_size -= 1;
|
||||
|
||||
request->session = old_session;
|
||||
|
||||
return deleted;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void SessionContainer::DelFromIdIndex(SessionContainer::Iterator iter)
|
||||
{
|
||||
IndexId::iterator i;
|
||||
|
||||
for(i = index_id.begin() ; i!=index_id.end() ; ++i)
|
||||
{
|
||||
if( i->second == iter )
|
||||
{
|
||||
index_id.erase(i);
|
||||
//log << log3 << "SC: deleted index_id for session id: " << iter->id << logend;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
*
|
||||
*
|
||||
* end of sessions gc
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
void SessionContainer::UpdateLastTime(SessionContainer::Iterator iter, time_t new_time)
|
||||
{
|
||||
IndexTime::iterator i = index_time.lower_bound(iter->last_time);
|
||||
bool found = false;
|
||||
|
||||
for( ; i != index_time.end() ; ++i)
|
||||
{
|
||||
if( i->second == iter )
|
||||
{
|
||||
index_time.erase(i);
|
||||
index_time.insert( std::make_pair(new_time, iter) );
|
||||
|
||||
iter->last_time = new_time;
|
||||
iter->tm_last_time = Time(new_time);
|
||||
|
||||
//log << log3 << "SC: last time and the time index for session id: " << iter->id << " updated" << logend;
|
||||
log << log3 << "SC: last time for session id: " << iter->id << " updated" << logend;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if( !found )
|
||||
log << log1 << "SC: cannot update the time, time index not found for session id: " << iter->id << logend;
|
||||
}
|
||||
|
Reference in New Issue
Block a user