CodeIgniter - ses_destroy() uses a LOT of CPU
I have a web-application written in CodeIgniter, and for the most part everything seems fairly reasonable. However, I've noticed extremely high CPU usage when a user logs out. My logout function in my auth controller is as follows:
function logout()
{
$goto = $SERVER['HTTP-REFERER'];
$this->session->sess_destroy();
if (!$goto)
$goto = "/";
header('location: '.$goto);
}
Normally, this is perfectly fine and fast. The strange thing is that when linked to from a particular sub-page开发者_StackOverflow this function takes about 5-6 seconds my of mysqld running at 100% cpu. How can I see what it's doing, and why?
Taking a shot in the dark (if you're sure this function is causing the slowness):
First, you could turn on MySQL's slow query log:
http://dev.mysql.com/doc/refman/5.1/en/slow-query-log.html
Then, if $sess_use_database
is TRUE
you might try optimizing your session table. You could have some overhead causing issues.
Apart from that, the only other thing I can think of is that there is an issue with your DB server. You might try running the MySQL Tuner to see if you can improve things a bit:
https://github.com/rackerhacker/MySQLTuner-perl
Hope that helps!
FYI
Here is the code that is run when the OP runs sess_destroy()
(from v2.0.2):
/**
* Destroy the current session
*
* @access public
* @return void
*/
function sess_destroy()
{
// Kill the session DB row
if ($this->sess_use_database === TRUE AND isset($this->userdata['session_id']))
{
$this->CI->db->where('session_id', $this->userdata['session_id']);
$this->CI->db->delete($this->sess_table_name);
}
// Kill the cookie
setcookie(
$this->sess_cookie_name,
addslashes(serialize(array())),
($this->now - 31500000),
$this->cookie_path,
$this->cookie_domain,
0
);
}
精彩评论