rfcs icon indicating copy to clipboard operation
rfcs copied to clipboard

Add Optional Session

Open RomainLanz opened this issue 5 years ago • 7 comments

Rendered

At the moment, any routes create a session by default. This can lead to performance issue for high traffic application and may be undesired.

We should provide an easy and developer-friendly way to disable session on some routes.

RomainLanz avatar Sep 07 '20 19:09 RomainLanz

Isn't the session implementable/exposable as a middleware? That would make it much easier to customize where it is enabled.

targos avatar Sep 07 '20 20:09 targos

In AdonisJS 4 it was handled by a middleware.

In V5, the session is initiated directly from the provider in a server hook (source code).

RomainLanz avatar Sep 07 '20 20:09 RomainLanz

Was there a technical reason for implementing it this way?

targos avatar Sep 08 '20 09:09 targos

@targos Yup. Middleware doesn't guarantee that downstream code will be executed everytime. For example: Following is the example of how the session middleware was implemented earlier

class SessionMiddleware {
  public async handle (ctx, next) {
    await ctx.session.init()
    await next()
    await ctx.session.commit()
  }
}

Now if the next method (which is next middleware or the controller) raises an exception, the commit method will never be executed. Yes, we can wrap it inside a try/catch statement. But then that prevents the global exception handler from being called.

The server hooks in v5 guarantee that before and after hooks are always called. There is a limitation in what you can achieve with these hooks, but the guarantee of being always called is what sessions need.

thetutlage avatar Sep 08 '20 10:09 thetutlage

I understand. Then I think it's important that this proposal defines a to exclude the session from either:

  • A route prefix
  • A group of routes

targos avatar Sep 08 '20 10:09 targos

Any update?

Xstoudi avatar Jan 04 '21 12:01 Xstoudi

Yes, we can wrap it inside a try/catch statement. But then that prevents the global exception handler from being called.

You could also wrap it in a try/finally statement.

targos avatar Jan 04 '21 12:01 targos