rack-fiber_pool 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Mike Perham
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,21 @@
1
+ rack-fiber_pool
2
+ ---------------
3
+
4
+ A Rack middleware component which runs each request in a Fiber from a pool of Fibers.
5
+
6
+ Requirements
7
+ ==============
8
+
9
+ * Ruby 1.9
10
+ * thin 1.2.7
11
+
12
+ Usage
13
+ =======
14
+
15
+ Add a require and use statement to your Rack app. See example/app.rb for a simple Sinatra app
16
+ which illustrates proper usage.
17
+
18
+ Author
19
+ =========
20
+
21
+ Mike Perham, http://twitter.com/mperham, http://github.com/mperham, mperham AT gmail.com.
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rack-fiber_pool"
8
+ gem.summary = %Q{Rack middleware to run each request within a Fiber}
9
+ gem.email = "mperham@gmail.com"
10
+ gem.homepage = "http://github.com/mperham/rack-fiber_pool"
11
+ gem.authors = ["Mike Perham"]
12
+ gem.add_development_dependency "shoulda", ">= 0"
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/test_*.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "rack-fiber_pool #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.9.0
data/example/app.rb ADDED
@@ -0,0 +1,24 @@
1
+ raise LoadError, "Ruby 1.9.1 only" if RUBY_VERSION < '1.9.1'
2
+
3
+ require 'rubygems'
4
+ require 'sinatra/base'
5
+ require 'fiber'
6
+
7
+ $LOAD_PATH << File.dirname(__FILE__) + '/../lib'
8
+
9
+ require 'rack/fiber_pool'
10
+
11
+ # rackup -s thin app.rb
12
+ # http://localhost:9292/test
13
+ class App < Sinatra::Base
14
+
15
+ use Rack::FiberPool
16
+
17
+ set :root, File.dirname(__FILE__)
18
+
19
+ get '/test' do
20
+ content_type "text/plain"
21
+ body "Hello world from #{Fiber.current}"
22
+ end
23
+
24
+ end
data/lib/fiber_pool.rb ADDED
@@ -0,0 +1,82 @@
1
+ # Author:: Mohammad A. Ali (mailto:oldmoe@gmail.com)
2
+ # Copyright:: Copyright (c) 2008 eSpace, Inc.
3
+ # License:: Distributes under the same terms as Ruby
4
+
5
+ require 'fiber'
6
+
7
+ class Fiber
8
+
9
+ #Attribute Reference--Returns the value of a fiber-local variable, using
10
+ #either a symbol or a string name. If the specified variable does not exist,
11
+ #returns nil.
12
+ def [](key)
13
+ local_fiber_variables[key]
14
+ end
15
+
16
+ #Attribute Assignment--Sets or creates the value of a fiber-local variable,
17
+ #using either a symbol or a string. See also Fiber#[].
18
+ def []=(key,value)
19
+ local_fiber_variables[key] = value
20
+ end
21
+
22
+ private
23
+
24
+ def local_fiber_variables
25
+ @local_fiber_variables ||= {}
26
+ end
27
+ end
28
+
29
+ class FiberPool
30
+
31
+ # gives access to the currently free fibers
32
+ attr_reader :fibers
33
+ attr_reader :busy_fibers
34
+
35
+ # Code can register a proc with this FiberPool to be called
36
+ # every time a Fiber is finished. Good for releasing resources
37
+ # like ActiveRecord database connections.
38
+ attr_accessor :generic_callbacks
39
+
40
+ # Prepare a list of fibers that are able to run different blocks of code
41
+ # every time. Once a fiber is done with its block, it attempts to fetch
42
+ # another one from the queue
43
+ def initialize(count = 100)
44
+ @fibers,@busy_fibers,@queue,@generic_callbacks = [],{},[],[]
45
+ count.times do |i|
46
+ fiber = Fiber.new do |block|
47
+ loop do
48
+ block.call
49
+ # callbacks are called in a reverse order, much like c++ destructor
50
+ Fiber.current[:callbacks].pop.call while Fiber.current[:callbacks].length > 0
51
+ generic_callbacks.each do |cb|
52
+ cb.call
53
+ end
54
+ unless @queue.empty?
55
+ block = @queue.shift
56
+ else
57
+ @busy_fibers.delete(Fiber.current.object_id)
58
+ @fibers << Fiber.current
59
+ block = Fiber.yield
60
+ end
61
+ end
62
+ end
63
+ fiber[:callbacks] = []
64
+ fiber[:em_keys] = []
65
+ @fibers << fiber
66
+ end
67
+ end
68
+
69
+ # If there is an available fiber use it, otherwise, leave it to linger
70
+ # in a queue
71
+ def spawn(&block)
72
+ if fiber = @fibers.shift
73
+ fiber[:callbacks] = []
74
+ @busy_fibers[fiber.object_id] = fiber
75
+ fiber.resume(block)
76
+ else
77
+ @queue << block
78
+ end
79
+ self # we are keen on hiding our queue
80
+ end
81
+
82
+ end
@@ -0,0 +1,24 @@
1
+ require 'fiber_pool'
2
+
3
+ module Rack
4
+ # Run each request in a Fiber. This FiberPool is
5
+ # provided by em_postgresql. Should probably split
6
+ # this dependency out.
7
+ class FiberPool
8
+ def initialize(app)
9
+ @app = app
10
+ @fiber_pool = ::FiberPool.new
11
+ yield @fiber_pool if block_given?
12
+ end
13
+
14
+ def call(env)
15
+ call_app = lambda do
16
+ result = @app.call(env)
17
+ env['async.callback'].call result
18
+ end
19
+
20
+ @fiber_pool.spawn(&call_app)
21
+ throw :async
22
+ end
23
+ end
24
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'rack/fiber_pool'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,5 @@
1
+ require 'helper'
2
+
3
+ class TestRackFiberPool < Test::Unit::TestCase
4
+ # An exercise left to the reader
5
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-fiber_pool
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 9
8
+ - 0
9
+ version: 0.9.0
10
+ platform: ruby
11
+ authors:
12
+ - Mike Perham
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-03 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: shoulda
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
32
+ description:
33
+ email: mperham@gmail.com
34
+ executables: []
35
+
36
+ extensions: []
37
+
38
+ extra_rdoc_files:
39
+ - LICENSE
40
+ - README.md
41
+ files:
42
+ - LICENSE
43
+ - README.md
44
+ - Rakefile
45
+ - VERSION
46
+ - example/app.rb
47
+ - lib/fiber_pool.rb
48
+ - lib/rack/fiber_pool.rb
49
+ - test/helper.rb
50
+ - test/test_rack-fiber_pool.rb
51
+ has_rdoc: true
52
+ homepage: http://github.com/mperham/rack-fiber_pool
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --charset=UTF-8
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.3.6
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Rack middleware to run each request within a Fiber
81
+ test_files:
82
+ - test/helper.rb
83
+ - test/test_rack-fiber_pool.rb