{"id":15636540,"url":"https://github.com/ioquatix/rack-freeze","last_synced_at":"2026-03-04T03:01:24.866Z","repository":{"id":56890118,"uuid":"84387502","full_name":"ioquatix/rack-freeze","owner":"ioquatix","description":"A policy framework for implementing thread-safe rack middleware.","archived":false,"fork":false,"pushed_at":"2021-07-03T21:51:59.000Z","size":31,"stargazers_count":124,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-11-14T08:30:51.449Z","etag":null,"topics":["policy","rack","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ioquatix.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-09T02:15:25.000Z","updated_at":"2025-01-14T01:36:35.000Z","dependencies_parsed_at":"2022-08-20T23:40:55.088Z","dependency_job_id":null,"html_url":"https://github.com/ioquatix/rack-freeze","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/ioquatix/rack-freeze","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ioquatix%2Frack-freeze","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ioquatix%2Frack-freeze/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ioquatix%2Frack-freeze/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ioquatix%2Frack-freeze/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ioquatix","download_url":"https://codeload.github.com/ioquatix/rack-freeze/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ioquatix%2Frack-freeze/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30070479,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-04T01:03:42.280Z","status":"online","status_checked_at":"2026-03-04T02:00:07.464Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["policy","rack","ruby"],"created_at":"2024-10-03T11:04:48.338Z","updated_at":"2026-03-04T03:01:24.852Z","avatar_url":"https://github.com/ioquatix.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rack::Freeze\n\nProvides a policy for Rack middleware which should be frozen by default to prevent mutability bugs in a multi-threaded environment.\n\n**In Rack 2.1+, you can use `Builder#freeze_app` instead of this gem.**\n\n[![Build Status](https://travis-ci.com/ioquatix/rack-freeze.svg)](https://travis-ci.com/ioquatix/rack-freeze)\n[![Code Climate](https://codeclimate.com/github/ioquatix/rack-freeze.svg)](https://codeclimate.com/github/ioquatix/rack-freeze)\n[![Coverage Status](https://coveralls.io/repos/ioquatix/rack-freeze/badge.svg)](https://coveralls.io/r/ioquatix/rack-freeze)\n\n## Motivation\n\nI found issues due to unexpected state mutation when developing [Utopia](https://github.com/ioquatix/utopia). 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.\n\nIdeally, [this concept would be part of rack](https://github.com/rack/rack/issues/1010). 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.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'rack-freeze'\n```\n\n## Usage\n\nAdd this to your config.ru:\n\n```ruby\nrequire 'rack/freeze'\n```\n\nNow all your middleware will be frozen by default.\n\n### What bugs does this fix?\n\nIt guarantees as much as is possible, that middleware won't mutate during a request.\n\n```ruby\n# This modifies `Rack::Builder#use` and `Rack::Builder#to_app` to generate a frozen stack of middleware.\nrequire 'rack/freeze'\n\nclass NonThreadSafeMiddleware\n\tdef initialize(app)\n\t\t@app = app\n\t\t@state = 0\n\tend\n\t\n\tdef call(env)\n\t\t@state += 1\n\t\t\n\t\treturn @app.call(env)\n\tend\nend\n\nuse NonThreadSafeMiddleware\n```\n\nAs `NonThreadSafeMiddleware` mutates it's state `@state += 1`, it will raise a `RuntimeError`. 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](http://www.rubyinside.com/does-the-gil-make-your-ruby-code-thread-safe-6051.html).\n\n### How to write thread-safe middleware?\n\nThere are two options: Don't mutate state, or if you need to for the purposes of performance, implement `#freeze` and use data-structures from [concurrent-ruby](https://github.com/ruby-concurrency/concurrent-ruby).\n\n```ruby\nrequire 'concurrent/map'\n\n# Cache every request based on the path. Don't do this in production :)\nclass CacheEverythingForever\n\tdef initialize(app)\n\t\t@app = app\n\t\t@cache_all_the_things = Concurrent::Map.new\n\tend\n\t\n\tdef freeze\n\t\treturn self if frozen?\n\t\t\n\t\t# Don't freeze @cache_all_the_things\n\t\t@app.freeze\n\t\t\n\t\tsuper\n\tend\n\t\n\tdef call(env)\n\t\t# Use the thread-safe `Concurrent::Map` to fetch the value or store it if it doesn't exist already.\n\t\t@cache_all_the_things.fetch_or_store(env[Rack::PATH_INFO]) do\n\t\t\t@app.call(env)\n\t\tend\n\tend\nend\n```\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n\n## License\n\nReleased under the MIT license.\n\nCopyright, 2017, by [Samuel G. D. Williams](http://www.codeotaku.com).\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fioquatix%2Frack-freeze","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fioquatix%2Frack-freeze","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fioquatix%2Frack-freeze/lists"}