starlette icon indicating copy to clipboard operation
starlette copied to clipboard

Reduce the frequency of set-cookie in SessionMiddleware

Open Zaczero opened this issue 1 month ago • 0 comments

Closes #2019, Requires #3068

Summary

Utilize the new TimestampSigner to obtain timestamp information, and in combination with a simple equality check, reduce the frequency of set-cookie responses. The set-cookie is sent whenever:

  • Session data changes
  • Session data is older than 1/4 of max_age

I have additionally corrected the handling of max_age so in the unrealistic edge case of 0 it will treat it as configured value. This is also how itsdangerous handles it, so the current implementation is buggy in that sense.

Checklist

  • [x] I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • [ ] I've updated the documentation accordingly.

Zaczero avatar Nov 06 '25 03:11 Zaczero