request_store 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.travis.yml +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +77 -0
- data/Rakefile +12 -0
- data/lib/request_store.rb +21 -0
- data/lib/request_store/railtie.rb +7 -0
- data/lib/request_store/version.rb +3 -0
- data/request_store.gemspec +22 -0
- data/test/request_store_test.rb +28 -0
- data/test/test_helper.rb +6 -0
- metadata +92 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Steve Klabnik
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# RequestStore [![build status](https://secure.travis-ci.org/steveklabnik/request_store.png?branch=master)](https://secure.travis-ci.org/steveklabnik/request_store) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/steveklabnik/request_store)
|
2
|
+
|
3
|
+
Ever needed to use a global variable in Rails? Ugh, that's the worst. If you
|
4
|
+
need gobal state, you've probably reached for `Thread.current`. Like this:
|
5
|
+
|
6
|
+
```
|
7
|
+
def self.foo
|
8
|
+
Thread.current[:foo] ||= 0
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.foo=(value)
|
12
|
+
Thread.current[:foo] = value
|
13
|
+
end
|
14
|
+
```
|
15
|
+
|
16
|
+
Ugh! I hate it. But you gotta do what you gotta do...
|
17
|
+
|
18
|
+
### The problem
|
19
|
+
|
20
|
+
Everyone's worrying about concurrency these days. So people are using those
|
21
|
+
fancy threaded web servers, like Thin or Puma. But if you use `Thread.current`,
|
22
|
+
and you use one of those servers, watch out! Values can stick around longer
|
23
|
+
than you'd expect, and this can cause bugs. For example, if we had this in
|
24
|
+
our controller:
|
25
|
+
|
26
|
+
```
|
27
|
+
def index
|
28
|
+
Thread.current[:counter] ||= 0
|
29
|
+
Thread.current[:counter] += 1
|
30
|
+
|
31
|
+
render :text => Thread.current[:counter]
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
If we ran this on MRI with Webrick, you'd get `1` as output, every time. But if
|
36
|
+
you run it with Thin, you get `1`, then `2`, then `3`...
|
37
|
+
|
38
|
+
### The solution
|
39
|
+
|
40
|
+
Add this line to your application's Gemfile:
|
41
|
+
|
42
|
+
gem 'request_store'
|
43
|
+
|
44
|
+
And change the code to this:
|
45
|
+
|
46
|
+
```
|
47
|
+
def index
|
48
|
+
RequestStore.store[:foo] ||= 0
|
49
|
+
RequestStore.store[:foo] += 1
|
50
|
+
|
51
|
+
render :text => RequestStore.store[:foo]
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
Yep, everywhere you used `Thread.current` just change it to
|
56
|
+
`RequestStore.store`. Now no matter what server you use, you'll get `1` every
|
57
|
+
time: the storage is local to that request.
|
58
|
+
|
59
|
+
### No Rails? No Problem!
|
60
|
+
|
61
|
+
A Railtie is added that configures the Middleware for you, but if you're not
|
62
|
+
using Rails, no biggie! Just use the Middelware yourself, however you need.
|
63
|
+
You'll probably have to shove this somewhere:
|
64
|
+
|
65
|
+
```
|
66
|
+
use RequestStore::Middleware
|
67
|
+
```
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
1. Fork it
|
72
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
73
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
74
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
75
|
+
5. Create new Pull Request
|
76
|
+
|
77
|
+
Don't forget to run the tests with `rake`.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "request_store/version"
|
2
|
+
|
3
|
+
require "request_store/railtie" if defined?(Rails)
|
4
|
+
|
5
|
+
module RequestStore
|
6
|
+
def self.store
|
7
|
+
Thread.current[:request_store] ||= {}
|
8
|
+
Thread.current[:request_store]
|
9
|
+
end
|
10
|
+
|
11
|
+
class Middleware
|
12
|
+
def initialize(app)
|
13
|
+
@app = app
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(env)
|
17
|
+
Thread.current[:request_store] = {}
|
18
|
+
@app.call(env)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'request_store/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "request_store"
|
8
|
+
gem.version = RequestStore::VERSION
|
9
|
+
gem.authors = ["Steve Klabnik"]
|
10
|
+
gem.email = ["steve@steveklabnik.com"]
|
11
|
+
gem.description = %q{RequestStore gives you per-request global storage.}
|
12
|
+
gem.summary = %q{RequestStore gives you per-request global storage.}
|
13
|
+
gem.homepage = "http://github.com/steveklabnik/request_store"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency "rake"
|
21
|
+
gem.add_development_dependency 'minitest', '~> 3.0'
|
22
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
|
3
|
+
require 'request_store'
|
4
|
+
|
5
|
+
class RequestStoreTest < Minitest::Unit::TestCase
|
6
|
+
def test_quacks_like_hash
|
7
|
+
RequestStore.store[:foo] = 1
|
8
|
+
assert_equal 1, RequestStore.store[:foo]
|
9
|
+
assert_equal 1, RequestStore.store.fetch(:foo)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_delegates_to_thread
|
13
|
+
RequestStore.store[:foo] = 1
|
14
|
+
assert_equal 1, Thread.current[:request_store][:foo]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class MiddlewareTest < Minitest::Unit::TestCase
|
19
|
+
def test_middleware_resets_store
|
20
|
+
app = RackApp.new
|
21
|
+
middleware = RequestStore::Middleware.new(app)
|
22
|
+
|
23
|
+
middleware.call({})
|
24
|
+
middleware.call({})
|
25
|
+
|
26
|
+
assert_equal 1, Thread.current[:request_store][:foo]
|
27
|
+
end
|
28
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: request_store
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Steve Klabnik
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: minitest
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '3.0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.0'
|
46
|
+
description: RequestStore gives you per-request global storage.
|
47
|
+
email:
|
48
|
+
- steve@steveklabnik.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- .travis.yml
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- lib/request_store.rb
|
60
|
+
- lib/request_store/railtie.rb
|
61
|
+
- lib/request_store/version.rb
|
62
|
+
- request_store.gemspec
|
63
|
+
- test/request_store_test.rb
|
64
|
+
- test/test_helper.rb
|
65
|
+
homepage: http://github.com/steveklabnik/request_store
|
66
|
+
licenses: []
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 1.8.23
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: RequestStore gives you per-request global storage.
|
89
|
+
test_files:
|
90
|
+
- test/request_store_test.rb
|
91
|
+
- test/test_helper.rb
|
92
|
+
has_rdoc:
|