Skip to content

Commit

Permalink
Enable Session locking for file driver
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelfolaron committed Jun 6, 2024
1 parent 2d82668 commit 76cb99d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
6 changes: 5 additions & 1 deletion app/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,13 @@ protected function registerCoreBindings(): void
'expire_on_close' => false,
'encrypt' => false,
'files' => APP_ROOT . '/cache/sessions',
'store' => "instance",
'block_store' => 'instance',
'block_lock_seconds' => 10,
'block_wait_seconds' => 10,
'lottery' => [2, 100],
'cookie' => "ltid",
'path' => '/',
'path' => "/",
'domain' => is_array(parse_url(BASE_URL)) ? parse_url(BASE_URL)['host'] : null,
'secure' => true,
'http_only' => true,
Expand Down
40 changes: 32 additions & 8 deletions app/Core/Middleware/StartSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Routing\Route;
use Illuminate\Session\SessionManager;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Date;
use Leantime\Core\Eventhelpers;
use Leantime\Core\IncomingRequest;
Expand Down Expand Up @@ -63,15 +64,38 @@ public function handle(IncomingRequest $request, Closure $next)

self::dispatch_event('session_initialized');

/* Handle blocking sessions
if (
$this->manager->shouldBlock() ||
($request->route() instanceof Route && $request->route()->locksFor())
) {
return $this->handleRequestWhileBlocking($request, $session, $next);
//Enable session locking by default
//We have too many async requests with session write requests creating all sorts of odd behavior
//if session locking is not enabled
return $this->handleRequestWhileBlocking($request, $session, $next);
}

/**
* Handle the given request within session state.
*
* @param IncomingRequest $request
* @param \Illuminate\Contracts\Session\Session $session
* @param \Closure $next
* @return mixed
*/
protected function handleRequestWhileBlocking(IncomingRequest $request, $session, Closure $next) {


$lockFor = $this->manager->defaultRouteBlockLockSeconds();

$lock = Cache::store($this->manager->blockDriver())
->lock('session:'.$session->getId(), $lockFor)
->betweenBlockedAttemptsSleepFor(50);

try {
$lock->block(
$this->manager->defaultRouteBlockWaitSeconds()
);

return $this->handleStatefulRequest($request, $session, $next);
} finally {
$lock?->release();
}
*/
return $this->handleStatefulRequest($request, $session, $next);
}


Expand Down

0 comments on commit 76cb99d

Please sign in to comment.