File tree 2 files changed +17
-0
lines changed
2 files changed +17
-0
lines changed Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change @@ -76,6 +76,8 @@ export class AstroSession<TDriver extends SessionDriverName = any> {
76
76
// preserving in-memory changes and deletions.
77
77
#partial = true ;
78
78
79
+ static #sharedStorage = new Map < string , Storage > ( ) ;
80
+
79
81
constructor (
80
82
cookies : AstroCookies ,
81
83
{
@@ -402,6 +404,14 @@ export class AstroSession<TDriver extends SessionDriverName = any> {
402
404
return this . #storage;
403
405
}
404
406
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
+
405
415
if ( this . #config. driver === 'test' ) {
406
416
this . #storage = ( this . #config as SessionConfig < 'test' > ) . options . mockStorage ;
407
417
return this . #storage! ;
@@ -468,6 +478,7 @@ export class AstroSession<TDriver extends SessionDriverName = any> {
468
478
this . #storage = createStorage ( {
469
479
driver : driver ( this . #config. options ) ,
470
480
} ) ;
481
+ AstroSession . #sharedStorage. set ( this . #config. driver , this . #storage) ;
471
482
return this . #storage;
472
483
} catch ( err ) {
473
484
throw new AstroError (
You can’t perform that action at this time.
0 commit comments