Repository navigation
Stop cookieless requests from growing server-side sessions - #1455
Merged
Merged
Conversation
Every request through Amber::Pipe::Flash wrote session["_flash"] = "{}",
even with an empty flash, so every request without a session cookie
created a session. Session::Store#build ignored session.store and always
built the memory adapter session, and MemorySessionAdapter had no size
limit, so each such request kept about 413 bytes for two weeks. A :web
route served to clients that never send the cookie back grew from
441 MiB after 30 s to 858 MiB after 90 s.
- Pipe::Flash writes the session only when the flash has messages, or
when it must clear a flash the session still holds.
- Session::Store#build honors session.store: signed_cookie and
encrypted_cookie build a CookieStore and keep nothing on the server;
adapter (newly valid) and redis use the configured session adapter.
- MemorySessionAdapter keeps at most max_sessions sessions and drops the
session used longest ago. The limit is session.max_memory_sessions
(default 10,000, AMBER_SESSION_MAX_MEMORY_SESSIONS overrides it), and
validate! rejects a value below 1.
Specs cover each fix, and a regression spec serves 100,000 cookieless
GET requests to a :web route with store: adapter and asserts that no
session is stored, no Set-Cookie is sent, and retained heap grows by
less than 8 MiB.
A Cookies::Store subclass can override #encrypted, as the spec mocks do, which widened the return type of Session::Store#cookie_store and failed to compile. Narrow it with is_a? and raise a ConfigurationError naming the class when it is not an EncryptedStore.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
A
:webroute served to clients that never send the session cookie back (load generators, crawlers, clients that ignore cookies) grew the process without limit. Measured on a release build of an Amber CLI 2.0.11 app at 32 connections, the beta.10:webroute shape reached 441 MiB after 30 s and 858 MiB after 90 s, with 302 and 692 MiB still in use after a forced collection: about 413 bytes per new visitor.Three defects combined:
Amber::Pipe::Flashwrotesession["_flash"] = "{}"on every response, even when the flash was empty, so every cookieless request created a session.Session::Store#buildignoredsession.storeand always built the memory adapter session.store: signed_cookie, the default, still kept every session on the server.MemorySessionAdapterhad no size limit and kept entries for two weeks (the generatedexpires).What changes
Pipe::Flashwrites the session only when the flash has messages, or when it must clear a flash the session still holds. A request that never sets a flash leaves the session unchanged.Session::Store#buildhonorssession.store.signed_cookieandencrypted_cookiebuild aCookieStoreand keep 0 bytes on the server.adapter(now a valid value, as the generated YAML already documents) andredisuse the configuredsession.adapter.MemorySessionAdapterkeeps at mostmax_sessionssessions and drops the session used longest ago when it is full. Reading, writing or extending a session counts as using it. The limit is the typed settingsession.max_memory_sessions(default 10,000,AMBER_SESSION_MAX_MEMORY_SESSIONSoverrides it), read once when the memory adapter is first built;validate!rejects a value below 1.Behavior change: apps on
store: signed_cookieorencrypted_cookienow get cookie sessions, as configured. An app that depends on server-side sessions (for exampleregenerate_session!, which is a no-op for cookie sessions) setsstore: adapter. CHANGELOG and the configuration guide say so.Specs (each fails on v2-dev at f987f1b)
spec/amber/pipes/flash_spec.cr: forsigned_cookie,encrypted_cookieandadapter, an empty flash sends noSet-Cookieand stores no session; a flash message reaches the next request and is cleared once read.spec/amber/router/session_spec.cr: each store value builds the right store; asigned_cookieorencrypted_cookieapp round-trips a session value through the cookie while the memory adapter's size stays unchanged. The old "creates an adapter session store by default" example asserted the defect and is replaced.spec/amber/adapters/memory_session_adapter_spec.cr: the adapter never exceeds its limit, evicts the least recently used session first, and does not evict when an existing session is updated at the limit.spec/amber/configuration/session_config_spec.cr,env_override_spec.cr: the new setting, its YAML key, its override and its validation.spec/amber/pipes/cookieless_web_requests_spec.cr: serves 100,000 cookielessGETrequests to a:webroute through Session, Flash and CSRF withstore: adapterand the two-week expiry, and asserts noSet-Cookie, no stored session, and retained heap growth (after two forced collections) below 8 MiB. Before the fix the same run stores 100,000 sessions, about 40 MiB.Testing
CI is green on all jobs (3,074 examples, 0 failures on Crystal 1.22.1 ubuntu).
Load test with the diagnosis harness: an Amber CLI 2.0.11 app, release build (
crystal-alpha1.21.0), the beta.10:webpipeline (Logger, Session, Flash, CSRF) around a full render of GET /, 32 connections, cookielessohaload,gc_initial_heap_mib: 0.store: signed_cookiestore: signed_cookieAMBER_SESSION_STORE=adapterMemory no longer grows with the request count, and throughput rises about 27% because no session cookie is encrypted per request.
A companion amber_cli PR corrects the generated
sessioncomments: amberframework/amber_cli#59.