rack-chain 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +30 -0
- data/Rakefile +6 -0
- data/lib/rack-chain.rb +2 -0
- data/lib/rack-chain/version.rb +5 -0
- data/lib/rack/chain.rb +55 -0
- data/rack-chain.gemspec +20 -0
- data/spec/rack/chain_spec.rb +76 -0
- data/spec/spec_helper.rb +60 -0
- metadata +82 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Nick Sieger
|
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,30 @@
|
|
1
|
+
# Rack::Chain
|
2
|
+
|
3
|
+
Rack::Chain uses fibers to minimize stack depth in Rack applications.
|
4
|
+
|
5
|
+
A Rack application assembled with Rack::Chain runs each middleware
|
6
|
+
`#call` in a separate fiber, thereby avoiding deep stacks.
|
7
|
+
|
8
|
+
The name "chain" comes from `javax.servlet.FilterChain`, which is the
|
9
|
+
equivalent pattern to Rack middleware in the Java Servlet API.
|
10
|
+
|
11
|
+
Until the Rack API morphs into a before/after pattern which would
|
12
|
+
allow decomposing the request pipeline into a flat sequence of
|
13
|
+
function applications over a request and a response, these kinds of
|
14
|
+
cheeky gyrations may be necessary.
|
15
|
+
|
16
|
+
## Requirements
|
17
|
+
|
18
|
+
Because Rack::Chain relies on fibers for its operation, Ruby 1.9 is
|
19
|
+
required.
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
To use Rack::Chain with existing Rack applications, place the
|
24
|
+
following lines in your `config.ru`:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require 'rack/chain'
|
28
|
+
extend Rack::Chain::Linker
|
29
|
+
```
|
30
|
+
|
data/Rakefile
ADDED
data/lib/rack-chain.rb
ADDED
data/lib/rack/chain.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require 'rack/builder'
|
3
|
+
|
4
|
+
class Rack::Chain
|
5
|
+
attr_reader :endpoint
|
6
|
+
attr_accessor :filters
|
7
|
+
|
8
|
+
class Link
|
9
|
+
def initialize(to)
|
10
|
+
@to = to
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(env)
|
14
|
+
Fiber.new do
|
15
|
+
Fiber.yield @to.call(env)
|
16
|
+
end.resume
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(endpoint, filters = [])
|
21
|
+
@endpoint = endpoint
|
22
|
+
@filters = filters
|
23
|
+
end
|
24
|
+
|
25
|
+
def call(env)
|
26
|
+
Link.new(filters.reverse.inject(endpoint) do |endpt,filter|
|
27
|
+
if filter.respond_to?(:[])
|
28
|
+
filter[Link.new(endpt)]
|
29
|
+
else
|
30
|
+
filter.new(Link.new(endpt))
|
31
|
+
end
|
32
|
+
end).call(env)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Include this module in Rack::Builder to make all apps use Rack::Chain.
|
36
|
+
#
|
37
|
+
# Alternatively, extend Rack::Builder in config.ru to use
|
38
|
+
# Rack::Chain for that particular config.ru. Example:
|
39
|
+
#
|
40
|
+
# <pre>
|
41
|
+
# require 'rack/chain'
|
42
|
+
# extend Rack::Chain::Linker
|
43
|
+
# use Middleware1
|
44
|
+
# use Middleware2
|
45
|
+
# run App
|
46
|
+
# </pre>
|
47
|
+
module Linker
|
48
|
+
def to_app
|
49
|
+
app = @map ? generate_map(@run, @map) : @run
|
50
|
+
fail "missing run or map statement" unless app
|
51
|
+
Rack::Chain.new(app, @use)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
data/rack-chain.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/rack-chain/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Nick Sieger"]
|
6
|
+
gem.email = ["nick@nicksieger.com"]
|
7
|
+
gem.description = %q{Rack::Chain builds a Rack application that runs each middleware in its own fiber to minimize stack depth. }
|
8
|
+
gem.summary = %q{Rack::Chain uses fibers to minimize stack depth in Rack applications.}
|
9
|
+
gem.homepage = "https://github.com/nicksieger/rack-chain"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "rack-chain"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Rack::Chain::VERSION
|
17
|
+
|
18
|
+
gem.add_runtime_dependency "rack", [">= 1.4.0"]
|
19
|
+
gem.add_development_dependency "rspec", [">= 2.8.0"]
|
20
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe Rack::Chain do
|
4
|
+
let(:env) { Hash.new }
|
5
|
+
|
6
|
+
let(:app) { app_dummy.new }
|
7
|
+
|
8
|
+
let(:chain) { Rack::Chain.new(app) }
|
9
|
+
|
10
|
+
let(:filter_names) { %w(Foo Bar Baz) }
|
11
|
+
|
12
|
+
let(:filters) { filter_names.map {|x| filter_dummy(x) } }
|
13
|
+
|
14
|
+
let(:full_chain) { chain.tap {|c| c.filters += filters } }
|
15
|
+
|
16
|
+
it "has an ordered list of filters" do
|
17
|
+
full_chain.filters.should == filters
|
18
|
+
end
|
19
|
+
|
20
|
+
it "calls each filter in order" do
|
21
|
+
full_chain.call(env)[2].should == ["App", *filter_names.reverse]
|
22
|
+
end
|
23
|
+
|
24
|
+
context Rack::Chain::Linker, "#to_app" do
|
25
|
+
let(:builder) do
|
26
|
+
Rack::Builder.new do
|
27
|
+
extend Rack::Chain::Linker
|
28
|
+
end.tap do |builder|
|
29
|
+
filters.each do |filter|
|
30
|
+
builder.use filter
|
31
|
+
end
|
32
|
+
builder.run app
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it "overrides Rack::Builder#to_app to create a Rack::Chain" do
|
37
|
+
builder.to_app.should be_instance_of(Rack::Chain)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "calls each filter in order" do
|
41
|
+
builder.to_app.call(env)[2].should == ["App", *filter_names.reverse]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "with a large set of middleware" do
|
46
|
+
let(:number_of_filters) { 100 }
|
47
|
+
|
48
|
+
let(:filter_names) { (1..number_of_filters).map {|n| "Filter#{n}" } }
|
49
|
+
|
50
|
+
let(:filters) { filter_names.map {|x| filter_dummy(x) { caller.size } } }
|
51
|
+
|
52
|
+
# Skip the call stack size for the app
|
53
|
+
let(:app) { app_dummy { nil }.new }
|
54
|
+
|
55
|
+
it "the call stack size stays constant" do
|
56
|
+
call_stack_sizes = full_chain.call(env)[2].compact
|
57
|
+
call_stack_sizes.max.should == call_stack_sizes.min
|
58
|
+
end
|
59
|
+
|
60
|
+
context "but using normal Rack::Builder" do
|
61
|
+
let(:builder) do
|
62
|
+
Rack::Builder.new.tap {|builder|
|
63
|
+
filters.each do |filter|
|
64
|
+
builder.use filter
|
65
|
+
end
|
66
|
+
builder.run app
|
67
|
+
}
|
68
|
+
end
|
69
|
+
|
70
|
+
it "the call stack size grows with each layer" do
|
71
|
+
call_stack_sizes = builder.call(env)[2].compact
|
72
|
+
call_stack_sizes.max.should > number_of_filters
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'rspec'
|
3
|
+
require 'rack/chain'
|
4
|
+
|
5
|
+
module Rack::Chain::Fixtures
|
6
|
+
class FilterDummy
|
7
|
+
def initialize(app)
|
8
|
+
@app = app
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(env)
|
12
|
+
@app.call(env).tap do |result|
|
13
|
+
result[2] ||= []
|
14
|
+
result[2] << body if body
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def body
|
19
|
+
self.class.name.split(/::/)[-1]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class AppDummy
|
24
|
+
def call(env)
|
25
|
+
[200, {"Content-Type" => "text/plain"}, [body]]
|
26
|
+
end
|
27
|
+
|
28
|
+
def body
|
29
|
+
self.class.name.split(/::/)[-1]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
module Dummies
|
34
|
+
end
|
35
|
+
|
36
|
+
def reset_fixtures
|
37
|
+
Dummies.class_eval do
|
38
|
+
constants.each {|c| remove_const(c) }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def filter_dummy(name = "Foo", &block)
|
43
|
+
Dummies.const_set(name, Class.new(FilterDummy) do
|
44
|
+
define_method(:body, &block) if block
|
45
|
+
end)
|
46
|
+
end
|
47
|
+
|
48
|
+
def app_dummy(name = "App", &block)
|
49
|
+
Dummies.const_set(name, Class.new(AppDummy) do
|
50
|
+
define_method(:body, &block) if block
|
51
|
+
end)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
RSpec.configure do |config|
|
56
|
+
config.include Rack::Chain::Fixtures
|
57
|
+
config.after :each do
|
58
|
+
reset_fixtures
|
59
|
+
end
|
60
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-chain
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nick Sieger
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rack
|
16
|
+
requirement: &70342257269420 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.4.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70342257269420
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70342257268820 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.8.0
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70342257268820
|
36
|
+
description: ! 'Rack::Chain builds a Rack application that runs each middleware in
|
37
|
+
its own fiber to minimize stack depth. '
|
38
|
+
email:
|
39
|
+
- nick@nicksieger.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- .rspec
|
46
|
+
- Gemfile
|
47
|
+
- LICENSE
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- lib/rack-chain.rb
|
51
|
+
- lib/rack-chain/version.rb
|
52
|
+
- lib/rack/chain.rb
|
53
|
+
- rack-chain.gemspec
|
54
|
+
- spec/rack/chain_spec.rb
|
55
|
+
- spec/spec_helper.rb
|
56
|
+
homepage: https://github.com/nicksieger/rack-chain
|
57
|
+
licenses: []
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 1.8.17
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: Rack::Chain uses fibers to minimize stack depth in Rack applications.
|
80
|
+
test_files:
|
81
|
+
- spec/rack/chain_spec.rb
|
82
|
+
- spec/spec_helper.rb
|