Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork4
ioquatix/rack-freeze
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Provides a policy for Rack middleware which should be frozen by default to prevent mutability bugs in a multi-threaded environment.
In Rack 2.1+, you can useBuilder#freeze_app instead of this gem.
I found issues due to unexpected state mutation when developingUtopia. It only became apparent when running in production using multi-threaded passenger. Freezing the middleware (and related state) allowed me to identify these issues, find other issues, and helps prevent these issues in the future.
Ideally,this concept would be part of rack. However, regardless of whether Rack adopts a policy on immutable middleware, this gem provides the tools necessary to implement such a policy transparently on top of existing rack middleware where possible.
Add this line to your application's Gemfile:
gem'rack-freeze'
Add this to your config.ru:
require'rack/freeze'
Now all your middleware will be frozen by default.
It guarantees as much as is possible, that middleware won't mutate during a request.
# This modifies `Rack::Builder#use` and `Rack::Builder#to_app` to generate a frozen stack of middleware.require'rack/freeze'classNonThreadSafeMiddlewaredefinitialize(app)@app=app@state=0enddefcall(env)@state +=1return@app.call(env)endenduseNonThreadSafeMiddleware
AsNonThreadSafeMiddleware mutates it's state@state += 1, it will raise aRuntimeError. In a multi-threaded web-server, unprotected mutation of internal state will lead to undefined behavior.5 out of 4 dentists agree that multi-threaded programming is hard to get right.
There are two options: Don't mutate state, or if you need to for the purposes of performance, implement#freeze and use data-structures fromconcurrent-ruby.
require'concurrent/map'# Cache every request based on the path. Don't do this in production :)classCacheEverythingForeverdefinitialize(app)@app=app@cache_all_the_things=Concurrent::Map.newenddeffreezereturnselfiffrozen?# Don't freeze @cache_all_the_things@app.freezesuperenddefcall(env)# Use the thread-safe `Concurrent::Map` to fetch the value or store it if it doesn't exist already.@cache_all_the_things.fetch_or_store(env[Rack::PATH_INFO])do@app.call(env)endendend
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
Released under the MIT license.
Copyright, 2017, bySamuel G. D. Williams.
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS INTHE SOFTWARE.
About
A policy framework for implementing thread-safe rack middleware.
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.