MDL-58018 core: Add support to open sessions without a lock

This commit is contained in:
Adam Eijdenberg
2020-04-20 14:13:34 +02:00
committed by Mark Nelson
parent 788dfb9c7d
commit 1c3b89b170
3 changed files with 114 additions and 21 deletions
+46
View File
@@ -34,6 +34,9 @@ defined('MOODLE_INTERNAL') || die();
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class handler {
/** @var boolean $haslock does the session need and/or have a lock? */
protected $haslock = false;
/**
* Start the session.
* @return bool success
@@ -42,6 +45,49 @@ abstract class handler {
return session_start();
}
/**
* Write the session and release lock. If the session was not intentionally opened
* with a write lock, then we will abort the session instead if able.
*/
public function write_close() {
if ($this->has_writelock()) {
session_write_close();
$this->haslock = false;
} else {
$this->abort();
}
}
/**
* Release lock on the session without writing it.
* May not be possible in older versions of PHP. If so, session may be written anyway
* so that any locks are released.
*/
public function abort() {
session_abort();
$this->haslock = false;
}
/**
* This is called after init() and before start() to indicate whether the session
* opened should be writable or not. This is intentionally captured even if your
* handler doesn't support non-locking sessions, so that behavior (upon session close)
* matches closely between handlers.
* @param bool $needslock true if needs to be open for writing
*/
public function set_needslock($needslock) {
$this->haslock = $needslock;
}
/**
* Has this session been opened with a writelock? Your handler should call this during
* start() if you support read-only sessions.
* @return bool true if session is intended to have a write lock.
*/
public function has_writelock() {
return $this->haslock;
}
/**
* Init session handler.
*/