Skip to content

Commit 76cb99d

Browse files
committed
Enable Session locking for file driver
1 parent 2d82668 commit 76cb99d

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

app/Core/Application.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,13 @@ protected function registerCoreBindings(): void
152152
'expire_on_close' => false,
153153
'encrypt' => false,
154154
'files' => APP_ROOT . '/cache/sessions',
155+
'store' => "instance",
156+
'block_store' => 'instance',
157+
'block_lock_seconds' => 10,
158+
'block_wait_seconds' => 10,
155159
'lottery' => [2, 100],
156160
'cookie' => "ltid",
157-
'path' => '/',
161+
'path' => "/",
158162
'domain' => is_array(parse_url(BASE_URL)) ? parse_url(BASE_URL)['host'] : null,
159163
'secure' => true,
160164
'http_only' => true,

app/Core/Middleware/StartSession.php

+32-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Routing\Route;
99
use Illuminate\Session\SessionManager;
1010
use Illuminate\Support\Carbon;
11+
use Illuminate\Support\Facades\Cache;
1112
use Illuminate\Support\Facades\Date;
1213
use Leantime\Core\Eventhelpers;
1314
use Leantime\Core\IncomingRequest;
@@ -63,15 +64,38 @@ public function handle(IncomingRequest $request, Closure $next)
6364

6465
self::dispatch_event('session_initialized');
6566

66-
/* Handle blocking sessions
67-
if (
68-
$this->manager->shouldBlock() ||
69-
($request->route() instanceof Route && $request->route()->locksFor())
70-
) {
71-
return $this->handleRequestWhileBlocking($request, $session, $next);
67+
//Enable session locking by default
68+
//We have too many async requests with session write requests creating all sorts of odd behavior
69+
//if session locking is not enabled
70+
return $this->handleRequestWhileBlocking($request, $session, $next);
71+
}
72+
73+
/**
74+
* Handle the given request within session state.
75+
*
76+
* @param IncomingRequest $request
77+
* @param \Illuminate\Contracts\Session\Session $session
78+
* @param \Closure $next
79+
* @return mixed
80+
*/
81+
protected function handleRequestWhileBlocking(IncomingRequest $request, $session, Closure $next) {
82+
83+
84+
$lockFor = $this->manager->defaultRouteBlockLockSeconds();
85+
86+
$lock = Cache::store($this->manager->blockDriver())
87+
->lock('session:'.$session->getId(), $lockFor)
88+
->betweenBlockedAttemptsSleepFor(50);
89+
90+
try {
91+
$lock->block(
92+
$this->manager->defaultRouteBlockWaitSeconds()
93+
);
94+
95+
return $this->handleStatefulRequest($request, $session, $next);
96+
} finally {
97+
$lock?->release();
7298
}
73-
*/
74-
return $this->handleStatefulRequest($request, $session, $next);
7599
}
76100

77101

0 commit comments

Comments
 (0)