Skip to content

Commit be866a1

Browse files
authored
fix: reuse storage objects in sessions (#13415)
1 parent fd65296 commit be866a1

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

.changeset/dull-papers-fry.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Reuses experimental session storage object between requests. This prevents memory leaks and improves performance for drivers that open persistent connections to a database.
6+

packages/astro/src/core/session.ts

+11
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ export class AstroSession<TDriver extends SessionDriverName = any> {
7676
// preserving in-memory changes and deletions.
7777
#partial = true;
7878

79+
static #sharedStorage = new Map<string, Storage>();
80+
7981
constructor(
8082
cookies: AstroCookies,
8183
{
@@ -402,6 +404,14 @@ export class AstroSession<TDriver extends SessionDriverName = any> {
402404
return this.#storage;
403405
}
404406

407+
// We reuse the storage object if it has already been created.
408+
// We don't need to worry about the config changing because editing it
409+
// will always restart the process.
410+
if (AstroSession.#sharedStorage.has(this.#config.driver)) {
411+
this.#storage = AstroSession.#sharedStorage.get(this.#config.driver);
412+
return this.#storage!;
413+
}
414+
405415
if (this.#config.driver === 'test') {
406416
this.#storage = (this.#config as SessionConfig<'test'>).options.mockStorage;
407417
return this.#storage!;
@@ -468,6 +478,7 @@ export class AstroSession<TDriver extends SessionDriverName = any> {
468478
this.#storage = createStorage({
469479
driver: driver(this.#config.options),
470480
});
481+
AstroSession.#sharedStorage.set(this.#config.driver, this.#storage);
471482
return this.#storage;
472483
} catch (err) {
473484
throw new AstroError(

0 commit comments

Comments
 (0)