Skip to content

Commit

Permalink
Add setDieOnError(bool) and allow errors to be non-fatal. Fix break_a…
Browse files Browse the repository at this point in the history
…fter config should not depend on session_name() function.
  • Loading branch information
colinmollenhour committed Mar 20, 2023
1 parent be7c732 commit 3e6ff3b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
37 changes: 26 additions & 11 deletions app/code/community/Cm/RedisSession/Model/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ class Cm_RedisSession_Model_Session implements \Zend_Session_SaveHandler_Interfa
* @var \Cm\RedisSession\Handler
*/
protected $sessionHandler;
protected bool $dieOnError = true;

public function __construct()
public function __construct($config = array())
{
try {
$this->sessionHandler = new \Cm\RedisSession\Handler(
new Cm_RedisSession_Model_Session_Config(),
new Cm_RedisSession_Model_Session_Config($config['session_name'] ?? 'default'),
new Cm_RedisSession_Model_Session_Logger(),
Mage::registry('controller')
&& Mage::app()->getFrontController()->getAction()
Expand All @@ -59,10 +60,15 @@ public function __construct()
}
}

public function setDieOnError(bool $flag): void
{
$this->dieOnError = $flag;
}

/**
* Setup save handler
*
* @return Mage_Core_Model_Resource_Session
* @return $this
*/
public function setSaveHandler()
{
Expand Down Expand Up @@ -96,14 +102,19 @@ public static function setStaticSaveHandler()
*/
public function open($savePath, $sessionName)
{
return $this->sessionHandler->open($savePath, $sessionName);
try {
return $this->sessionHandler->open($savePath, $sessionName);
} catch (Throwable $e) {
$this->handleException($e);
return false;
}
}

/**
* Fetch session data
*
* @param string $sessionId
* @return string
* @return string|bool
*/
public function read($sessionId)
{
Expand All @@ -114,6 +125,7 @@ public function read($sessionId)
} catch (\Cm\RedisSession\ConcurrentConnectionsExceededException $e) {
self::$failedLockAttempts = $this->sessionHandler->getFailedLockAttempts();
$this->handleException($e);
return false;
}
}

Expand Down Expand Up @@ -167,14 +179,17 @@ public function gc($maxLifeTime)
*/
protected function handleException(\Exception $e)
{
if (Mage::getConfig()->getNode('global/redis_session')->is('log_exceptions')) {
Mage::logException($e);
}
if ($e instanceof \Cm\RedisSession\ConcurrentConnectionsExceededException) {
require_once Mage::getBaseDir() . DS . 'errors' . DS . '503.php';
} else {
Mage::register('concurrent_connections_exceeded', true);
if (Mage::getConfig()->getNode('global/redis_session')->is('log_exceptions')) {
Mage::logException($e);
}
if ($this->dieOnError) {
require_once Mage::getBaseDir() . DS . 'errors' . DS . '503.php';
die();
}
} else if ($this->dieOnError) {
Mage::printException($e);
}
exit;
}
}
8 changes: 4 additions & 4 deletions app/code/community/Cm/RedisSession/Model/Session/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class Cm_RedisSession_Model_Session_Config implements \Cm\RedisSession\Handler\C
*/
protected $config = null;

public function __construct($path = 'global/redis_session')
public function __construct(string $sessionName)
{
$config = Mage::getConfig()->getNode($path) ?: new Mage_Core_Model_Config_Element('<root></root>');
$config = Mage::getConfig()->getNode('global/redis_session') ?: new Mage_Core_Model_Config_Element('<root></root>');

// Clone the XML Config data to varien object to fix: https://github.com/colinmollenhour/Cm_RedisSession/issues/155
$this->config = new Varien_Object([
Expand All @@ -63,7 +63,7 @@ public function __construct($path = 'global/redis_session')
'sentinel_master' => (string) $config->descend('sentinel_master'),
'sentinel_verify_master' => $config->is('sentinel_verify_master'),
'sentinel_connect_retries' => (int) $config->descend('sentinel_connect_retries'),
'break_after_' . session_name() => (int) $config->descend('break_after_' . session_name()),
'break_after' => (int) $config->descend('break_after_' . $sessionName),
]);
}

Expand Down Expand Up @@ -210,7 +210,7 @@ public function getFirstLifetime()
*/
public function getBreakAfter()
{
return $this->config->getData('break_after_' . session_name());
return $this->config->getData('break_after');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Cm_RedisSession_Model_Session_Handler extends \Cm\RedisSession\Handler
public function __construct()
{
parent::__construct(
new Cm_RedisSession_Model_Session_Config(),
new Cm_RedisSession_Model_Session_Config('adminhtml'),
new Cm_RedisSession_Model_Session_Logger()
);
}
Expand Down

0 comments on commit 3e6ff3b

Please sign in to comment.