https://github.com/steveklabnik/request_store
Per-request global storage for Rack.
https://github.com/steveklabnik/request_store
Keywords from Contributors
activerecord activejob mvc rubygems rspec grape rubocop rack authorization static-code-analysis
Last synced: about 21 hours ago
JSON representation
Repository metadata
Per-request global storage for Rack.
- Host: GitHub
- URL: https://github.com/steveklabnik/request_store
- Owner: steveklabnik
- License: mit
- Created: 2012-12-17T09:43:43.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2024-05-01T20:21:45.000Z (almost 2 years ago)
- Last Synced: 2026-02-26T19:58:18.771Z (5 days ago)
- Language: Ruby
- Homepage: https://github.com/steveklabnik/request_store
- Size: 82 KB
- Stars: 1,487
- Watchers: 31
- Forks: 92
- Open Issues: 9
- Releases: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
README.md
RequestStore

Ever needed to use a global variable in Rails? Ugh, that's the worst. If you
need global state, you've probably reached for Thread.current. Like this:
def self.foo
Thread.current[:foo] ||= 0
end
def self.foo=(value)
Thread.current[:foo] = value
end
Ugh! I hate it. But you gotta do what you gotta do...
The problem
Everyone's worrying about concurrency these days. So people are using those
fancy threaded web servers, like Thin or Puma. But if you use Thread.current,
and you use one of those servers, watch out! Values can stick around longer
than you'd expect, and this can cause bugs. For example, if we had this in
our controller:
def index
Thread.current[:counter] ||= 0
Thread.current[:counter] += 1
render :text => Thread.current[:counter]
end
If we ran this on MRI with Webrick, you'd get 1 as output, every time. But if
you run it with Thin, you get 1, then 2, then 3...
The solution
Add this line to your application's Gemfile:
gem 'request_store'
And change the code to this:
def index
RequestStore.store[:foo] ||= 0
RequestStore.store[:foo] += 1
render :text => RequestStore.store[:foo]
end
Yep, everywhere you used Thread.current just change it to
RequestStore.store. Now no matter what server you use, you'll get 1 every
time: the storage is local to that request.
API
The fetch method returns the stored value if it already exists. If no stored value exists, it uses the provided block to add a new stored value.
top_posts = RequestStore.fetch(:top_posts) do
# code to obtain the top posts
end
Rails 2 compatibility
The gem includes a Railtie that will configure everything properly for Rails 3+
apps, but if your app is tied to an older (2.x) version, you will have to
manually add the middleware yourself. Typically this should just be a matter
of adding:
config.middleware.use RequestStore::Middleware
into your config/environment.rb.
No Rails? No Problem!
A Railtie is added that configures the Middleware for you, but if you're not
using Rails, no biggie! Just use the Middleware yourself, however you need.
You'll probably have to shove this somewhere:
use RequestStore::Middleware
No Rails + Rack::Test
In order to have RequestStore storage cleared between requests, add it to the
app:
# spec_helper.rb
def app
Rack::Builder.new do
use RequestStore::Middleware
run MyApp
end
end
Using with Sidekiq
This gem uses a Rack middleware to clear the store object after every request,
but that doesn't translate well to background processing with
Sidekiq.
A companion library,
request_store-sidekiq
creates a Sidekiq middleware that will ensure the store is cleared after each
job is processed, for security and consistency with how this is done in Rack.
Semantic Versioning
This project conforms to semver. As a result of this
policy, you can (and should) specify a dependency on this gem using the
Pessimistic Version Constraint with
two digits of precision. For example:
spec.add_dependency 'request_store', '~> 1.0'
This means your project is compatible with request_store 1.0 up until 2.0.
You can also set a higher minimum version:
spec.add_dependency 'request_store', '~> 1.1'
Contributing
- 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
Don't forget to run the tests with rake.
Owner metadata
- Name: Steve Klabnik
- Login: steveklabnik
- Email: steve@steveklabnik.com
- Kind: user
- Description: Rust, jj, other things
- Website: http://steveklabnik.com
- Location: Austin, TX
- Twitter:
- Company: East River Source Control
- Icon url: https://avatars.githubusercontent.com/u/27786?u=5af723f67163593cc930a745f73a64a3b1e150e3&v=4
- Repositories: 843
- Last ynced at: 2026-01-31T18:14:38.279Z
- Profile URL: https://github.com/steveklabnik
GitHub Events
Total
- Pull request event: 1
- Fork event: 4
- Watch event: 17
- Issue comment event: 1
Last Year
- Pull request event: 1
- Fork event: 3
- Watch event: 5
Committers metadata
Last synced: 3 days ago
Total Commits: 102
Total Committers: 36
Avg Commits per committer: 2.833
Development Distribution Score (DDS): 0.598
Commits in past year: 0
Committers in past year: 0
Avg Commits per committer in past year: 0.0
Development Distribution Score (DDS) in past year: 0.0
| Name | Commits | |
|---|---|---|
| Steve Klabnik | s****e@s****m | 41 |
| Olle Jonsson | o****n@g****m | 8 |
| Timo Schilling | t****o@s****o | 8 |
| Teo Ljungberg | t****o@t****m | 4 |
| Andrew A. Smith | a****h@g****m | 3 |
| Samuel Williams | s****s@o****z | 2 |
| Praveen Arimbrathodiyil | p****n@d****g | 2 |
| Orien Madgwick | 4****n | 2 |
| Igor Victor | g****a@y****u | 2 |
| Philip Arndt | p@a****o | 2 |
| Alex Ghiculescu | a****x@t****o | 2 |
| Sam Mulube | s****e@c****m | 2 |
| yunlei | y****u@a****m | 1 |
| Nick Giancola | n****k@g****m | 1 |
| Anmol Chopra | a****a@r****n | 1 |
| Akira Matsuda | r****e@d****p | 1 |
| Al-waleed shihadeh | w****v@g****m | 1 |
| Aleksey V Zapparov | i****i@m****g | 1 |
| oleg dashevskii | o****i@g****m | 1 |
| m-nakamura145 | m****5@g****m | 1 |
| jartek | j****1@g****m | 1 |
| TravisTX | t****x@g****m | 1 |
| Tony Novak | a****2@c****u | 1 |
| Todd Sedano | t****o@g****m | 1 |
| Sebastien Vrillaud | k****a@g****m | 1 |
| Ryan McGeary | r****n@m****g | 1 |
| Robert Reiz | r****1@g****m | 1 |
| Philip Roberts | p****l@l****m | 1 |
| Michael Orlitzky | m****l@o****m | 1 |
| Maxime Handfield Lapointe | h****n@h****m | 1 |
| and 6 more... | ||
Committer domains:
- goclio.com: 1
- svyatov.ru: 1
- madebylotus.com: 1
- orlitzky.com: 1
- latentflip.com: 1
- mcgeary.org: 1
- gusto.com: 1
- cornell.edu: 1
- member.fsf.org: 1
- dio.jp: 1
- rocketbox.in: 1
- gophilosophie.com: 1
- appfolio.com: 1
- cosm.com: 1
- tanda.co: 1
- arndt.io: 1
- yandex.ru: 1
- debian.org: 1
- oriontransfer.co.nz: 1
- gettyimages.com: 1
- teoljungberg.com: 1
- schilling.io: 1
- steveklabnik.com: 1
Issue and Pull Request metadata
Last synced: 11 days ago
Total issues: 38
Total pull requests: 68
Average time to close issues: 6 months
Average time to close pull requests: 2 months
Total issue authors: 38
Total pull request authors: 43
Average comments per issue: 3.84
Average comments per pull request: 2.1
Merged pull request: 52
Bot issues: 0
Bot pull requests: 0
Past year issues: 0
Past year pull requests: 2
Past year average time to close issues: N/A
Past year average time to close pull requests: 3 days
Past year issue authors: 0
Past year pull request authors: 1
Past year average comments per issue: 0
Past year average comments per pull request: 0.0
Past year merged pull request: 0
Past year bot issues: 0
Past year bot pull requests: 0
Top Issue Authors
- kozubenko (1)
- ixti (1)
- imechemi (1)
- phuongnd08 (1)
- prcongithub (1)
- khiav223577 (1)
- henkesn (1)
- yannski (1)
- palasha (1)
- vishalTatvaSoft (1)
- vaneyckt (1)
- fsateler (1)
- professor (1)
- eoinkelly (1)
- lawrencepit (1)
Top Pull Request Authors
- timoschilling (8)
- olleolleolle (7)
- steveklabnik (6)
- ABCDEFG0204 (2)
- icheishvili (2)
- smulube (2)
- professor (2)
- m-nakamura145 (2)
- gogainda (2)
- parndt (2)
- latentflip (1)
- rmm5t (1)
- svyatov (1)
- macfanatic (1)
- elado (1)
Top Issue Labels
- question (1)
Top Pull Request Labels
Package metadata
- Total packages: 14
-
Total downloads:
- rubygems: 631,130,374 total
- Total docker downloads: 1,272,134,234
- Total dependent packages: 180 (may contain duplicates)
- Total dependent repositories: 16,729 (may contain duplicates)
- Total versions: 71
- Total maintainers: 2
- Total advisories: 1
gem.coop: request_store
RequestStore gives you per-request global storage.
- Homepage: https://github.com/steveklabnik/request_store
- Documentation: http://www.rubydoc.info/gems/request_store/
- Licenses: MIT
- Latest release: 1.7.0 (published almost 2 years ago)
- Last Synced: 2026-02-26T21:31:03.371Z (5 days ago)
- Versions: 20
- Dependent Packages: 0
- Dependent Repositories: 0
- Downloads: 315,636,969 Total
- Docker Downloads: 636,067,117
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 0.055%
- Downloads: 0.078%
- Docker downloads count: 0.142%
- Maintainers (1)
ubuntu-22.04: ruby-request-store
- Homepage: https://github.com/steveklabnik/request_store
- Licenses: mit
- Latest release: 1.5.0-2 (published 18 days ago)
- Last Synced: 2026-02-13T13:24:35.792Z (18 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 0.284%
- Stargazers count: 0.42%
- Forks count: 0.715%
rubygems.org: request_store
RequestStore gives you per-request global storage.
- Homepage: https://github.com/steveklabnik/request_store
- Documentation: http://www.rubydoc.info/gems/request_store/
- Licenses: MIT
- Latest release: 1.7.0 (published almost 2 years ago)
- Last Synced: 2026-02-26T09:10:12.270Z (5 days ago)
- Versions: 20
- Dependent Packages: 180
- Dependent Repositories: 16,729
- Downloads: 315,493,405 Total
- Docker Downloads: 636,067,117
-
Rankings:
- Downloads: 0.081%
- Docker downloads count: 0.186%
- Dependent packages count: 0.203%
- Dependent repos count: 0.274%
- Average: 0.836%
- Stargazers count: 1.367%
- Forks count: 2.902%
- Maintainers (1)
- Advisories:
proxy.golang.org: github.com/steveklabnik/request_store
- Homepage:
- Documentation: https://pkg.go.dev/github.com/steveklabnik/request_store#section-documentation
- Licenses: mit
- Latest release: v1.7.0 (published almost 2 years ago)
- Last Synced: 2026-02-25T19:00:48.519Z (6 days ago)
- Versions: 20
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Stargazers count: 1.76%
- Forks count: 2.846%
- Average: 6.236%
- Dependent packages count: 9.56%
- Dependent repos count: 10.779%
alpine-v3.3: ruby-request_store
RequestStore gives you per-request global storage
- Homepage: http://github.com/steveklabnik/request_store
- Licenses: MIT
- Latest release: 1.0.8-r0 (published over 10 years ago)
- Last Synced: 2026-02-03T22:27:00.964Z (28 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Stargazers count: 1.127%
- Forks count: 2.765%
- Average: 7.797%
- Dependent packages count: 27.296%
- Maintainers (1)
debian-10: ruby-request-store
- Homepage: http://github.com/steveklabnik/request_store
- Documentation: https://packages.debian.org/buster/ruby-request-store
- Licenses:
- Latest release: 1.3.0-1 (published 20 days ago)
- Last Synced: 2026-02-13T04:25:10.276Z (19 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
ubuntu-20.04: ruby-request-store
- Homepage: https://github.com/steveklabnik/request_store
- Licenses:
- Latest release: 1.5.0-2 (published 18 days ago)
- Last Synced: 2026-02-13T07:21:44.862Z (18 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
ubuntu-23.10: ruby-request-store
- Homepage: https://github.com/steveklabnik/request_store
- Licenses:
- Latest release: 1.5.1-1 (published 18 days ago)
- Last Synced: 2026-02-13T18:31:26.901Z (18 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
ubuntu-23.04: ruby-request-store
- Homepage: https://github.com/steveklabnik/request_store
- Licenses:
- Latest release: 1.5.1-1 (published 20 days ago)
- Last Synced: 2026-02-11T06:48:29.247Z (20 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
debian-11: ruby-request-store
- Homepage: https://github.com/steveklabnik/request_store
- Documentation: https://packages.debian.org/bullseye/ruby-request-store
- Licenses:
- Latest release: 1.5.0-2 (published 21 days ago)
- Last Synced: 2026-02-13T08:24:20.061Z (18 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
debian-12: ruby-request-store
- Homepage: https://github.com/steveklabnik/request_store
- Documentation: https://packages.debian.org/bookworm/ruby-request-store
- Licenses:
- Latest release: 1.5.1-1 (published 19 days ago)
- Last Synced: 2026-02-12T23:39:44.151Z (19 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
debian-13: ruby-request-store
- Homepage: https://github.com/steveklabnik/request_store
- Documentation: https://packages.debian.org/trixie/ruby-request-store
- Licenses:
- Latest release: 1.5.1-1 (published 19 days ago)
- Last Synced: 2026-02-13T13:19:06.517Z (18 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
Dependencies
- actions/checkout v1 composite
- ruby/setup-ruby v1 composite
- rake ~> 13
- rake ~> 12
- rake ~> 11
- minitest ~> 5.0 development
- rake >= 0 development
- rack >= 1.4
Score: 32.26091493320283