reifier 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ef0dbfbe852ecb662c263830e7f6d3203cc9142
4
- data.tar.gz: 5c3bff16df006254617c1db3dcb25137a405f986
3
+ metadata.gz: b9851d78945b3c347db66a2abaf1226e042250e8
4
+ data.tar.gz: e3995204dda21f2d7b920f62f5015af09fef0b24
5
5
  SHA512:
6
- metadata.gz: 04e3c84227f271e0d9425bb2c16ea2b7082f8a5ee2178e076cdce7027b3d045a5f964b0c06ecf7d2590a682cd6fbe3fcf33d08745b183f7b69a7877d0f74b837
7
- data.tar.gz: b614d7e7e1781a66954abbaca89b23d80696d8bc1a446a2ba106a57309d308e1f297409f1a4fdc776d903c256e376a04750a56bbcd0ff32e53a784e8e797ecd0
6
+ metadata.gz: fd6c52d68405d750ba5ea58b0bb8fbfb919479113dedea8896f1b459a586b1564a42fb0e5e6575e3505eaf19ce95b29d7abd36e13307b47684366c986be2b5c7
7
+ data.tar.gz: 53015621be965b1e53ae5b499fb1728a320617850136e9d8e2d5f246a15de6db3870898e6ca2f53bad026b5abcdf90863a39f31fa877d53c5c588eb185d955a2
data/README.md CHANGED
@@ -28,6 +28,12 @@ Use it through rackup:
28
28
 
29
29
  $ rackup -s reifier
30
30
 
31
+ ## Available Options
32
+
33
+ You can adapt the ThreadPool sized with following option:
34
+
35
+ $ rackup -s reifier -O Threads=16
36
+
31
37
  ## Development
32
38
 
33
39
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -38,7 +44,6 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
38
44
 
39
45
  Bug reports and pull requests are welcome on GitHub at https://github.com/tak1n/reifier.
40
46
 
41
-
42
47
  ## License
43
48
 
44
49
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -9,6 +9,12 @@ module Rack
9
9
  server = ::Reifier::Server.new(app, options)
10
10
  server.start
11
11
  end
12
+
13
+ def self.valid_options
14
+ {
15
+ 'Threads=THREADS' => 'Number of threads (default: 5)'
16
+ }
17
+ end
12
18
  end
13
19
 
14
20
  register :reifier, Reifier
@@ -1,11 +1,18 @@
1
1
  module Reifier
2
2
  class Response
3
- attr_accessor :request_method, :request_uri, :status, :headers, :body, :protocol
3
+ attr_accessor :status, :headers, :body, :protocol
4
+
5
+ def initialize
6
+ @response = ''
7
+ end
4
8
 
5
9
  def handle(socket)
6
- handle_request_line(socket)
7
- handle_headers(socket)
8
- handle_body(socket)
10
+ handle_request_line
11
+ handle_headers
12
+ handle_body
13
+
14
+ socket.write @response
15
+ socket.close
9
16
  end
10
17
 
11
18
  def <<(rack_return)
@@ -16,31 +23,26 @@ module Reifier
16
23
 
17
24
  private
18
25
 
19
- def handle_request_line(socket)
20
- socket.write "#{@protocol} #{@status} #{HTTP_STATUS_CODES[@status]}" + CRLF
26
+ def handle_request_line
27
+ @response << "#{@protocol} #{@status} #{HTTP_STATUS_CODES[@status]}" + CRLF
21
28
  end
22
29
 
23
- def handle_headers(socket)
24
- headers = ''
30
+ def handle_headers
25
31
  @headers.each do |k, v|
26
- headers << "#{k}: #{v}" + CRLF
32
+ @response << "#{k}: #{v}" + CRLF
27
33
  end
28
- headers << 'Connection: close' + CRLF
29
- headers << CRLF
30
-
31
- socket.write headers
34
+ @response << 'Connection: close' + CRLF
35
+ @response << CRLF
32
36
  end
33
37
 
34
- def handle_body(socket)
35
- body = ''
38
+ def handle_body
36
39
  @body.each do |chunk|
37
- body << chunk
40
+ @response << chunk
38
41
  end
39
42
 
40
- body << CRLF
41
-
42
- socket.write body
43
- socket.close
43
+ @response << CRLF
44
+ ensure
45
+ @body.close if @body.respond_to? :close
44
46
  end
45
47
  end
46
48
  end
@@ -13,16 +13,20 @@ module Reifier
13
13
  end
14
14
 
15
15
  server = TCPServer.new(@options[:Host], @options[:Port])
16
- pool = Concurrent::FixedThreadPool.new(5)
16
+
17
+ threads = @options[:Threads] || 5
18
+ pool = Concurrent::FixedThreadPool.new(@options[:Threads] || 5)
17
19
 
18
20
  puts "# Environment: #{@options[:environment]}"
19
21
  puts "# Listening on tcp://#{@options[:Host]}:#{@options[:Port]}"
20
22
  puts "# PID: #{Process.pid}"
23
+ puts "# Number of Threads used: #{threads}"
21
24
 
22
25
  loop do
23
26
  socket = server.accept
24
27
  socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
25
28
 
29
+ # Thread.new do
26
30
  pool.post do
27
31
  begin
28
32
  request = Request.new(@options)
@@ -30,22 +34,14 @@ module Reifier
30
34
 
31
35
  request.handle(socket)
32
36
 
33
- response.request_method = request.request_method
34
- response.request_uri = request.request_uri
35
- response.protocol = request.protocol
36
-
37
+ response.protocol = request.protocol
37
38
  response << @app.call(request.rack_env)
38
39
 
39
40
  response.handle(socket)
40
41
 
41
- log(request, response) if @options[:environment] == 'development'
42
- rescue EOFError
43
- puts 'Request Line is empty'
44
- rescue HTTPParseError
45
- puts 'Request Line must include HTTP (HTTPS enabled?)'
46
- rescue Errno::EPIPE
47
- puts 'Writing to client failed :|'
48
- ensure
42
+ log_request request, response if development?
43
+ rescue Exception => e
44
+ log "\nError: #{e.class}\nMessage: #{e.message}\n\nBacktrace:\n\t#{e.backtrace.join("\n\t")}" if development?
49
45
  socket.close
50
46
  end
51
47
  end
@@ -54,7 +50,15 @@ module Reifier
54
50
 
55
51
  private
56
52
 
57
- def log(request, response)
53
+ def development?
54
+ @options[:environment] == 'development'
55
+ end
56
+
57
+ def log(message)
58
+ puts "[#{Time.now}] #{message}"
59
+ end
60
+
61
+ def log_request(request, response)
58
62
  puts "[#{Time.now}] \"#{request.request_method} #{request.request_path} #{request.protocol}\" #{response.status}"
59
63
  end
60
64
  end
@@ -1,3 +1,3 @@
1
1
  module Reifier
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
@@ -35,5 +35,5 @@ Gem::Specification.new do |spec|
35
35
  spec.add_development_dependency 'pry', '~> 0.10'
36
36
  spec.add_development_dependency 'minitest-reporters', '~> 1.1'
37
37
  spec.add_development_dependency 'rubocop', '~> 0.37'
38
- spec.add_development_dependency 'rack'
38
+ spec.add_development_dependency 'rack', '~> 2.0.0.alpha'
39
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benny Klotz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-15 00:00:00.000000000 Z
11
+ date: 2016-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -112,16 +112,16 @@ dependencies:
112
112
  name: rack
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - ">="
115
+ - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: '0'
117
+ version: 2.0.0.alpha
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - ">="
122
+ - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: '0'
124
+ version: 2.0.0.alpha
125
125
  description: A rack app server written in pure ruby
126
126
  email:
127
127
  - benny.klotz92@gmail.com