reifier 0.5.0 → 0.6.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: f44cae18b405a8793f34980692b85438987551d7
4
- data.tar.gz: 8dad6a8d76982c62fe8ef97bff274c130c7fd310
3
+ metadata.gz: b29d21d3efe5c6cd139af6449af8657d3f9d0b84
4
+ data.tar.gz: 902c73060ba214bb356815e9f2929d88e601b813
5
5
  SHA512:
6
- metadata.gz: e7c3111aef5ed045e12b832778d60eadbff4783dd99f6e00f1ac98f060d71f6e35e6e32f4f35cbb8e3abc1443203e2b033039119b0cea4a2151456af30d19575
7
- data.tar.gz: 24be8d505dc28cc4c594f4d40c90a6aae43b2da86c5545d842240074662f374a40a186f13c819994129eadd9b00a5a264fa94072c3142575088fe7a1794239eb
6
+ metadata.gz: d7ca642a2d5805356f90afcb4d6a961506ed78457602545c840ee93ded772a286886183569fcc2ecec1b949206607953c8b454f4deb0be918bf3e0432440fe99
7
+ data.tar.gz: adb27c166f7f22437ee134894a02243f61abbaa0afb2fb99069788758abb967390659fda263d915bfa59bb6aa6fd1216aea3fc6492f2ad83b7311f3b1e4f1261
@@ -1,2 +1,2 @@
1
- threads 8, 16 # Using a thread pool size of 8
1
+ threads 8, 16 # Using a thread pool min size of 8 and max size of 16
2
2
  workers 9 # Using 9 worker processes
@@ -5,7 +5,6 @@ module Rack
5
5
  module Handler
6
6
  module Reifier
7
7
  DEFAULT_OPTIONS = {
8
- Workers: 1,
9
8
  MinThreads: 0,
10
9
  MaxThreads: 16
11
10
  }.freeze
@@ -22,7 +21,7 @@ module Rack
22
21
  {
23
22
  'MinThreads=MINTHREADS' => 'Number of minimal threads (default: 0)',
24
23
  'MaxThreads=MAXTHREADS' => 'Number of minimal threads (default: 16)',
25
- 'Workers=WORKERS' => 'Number of workers (default: 1)'
24
+ 'Workers=WORKERS' => 'Number of workers (default: none)'
26
25
  }
27
26
  end
28
27
  end
@@ -28,9 +28,40 @@ module Reifier
28
28
  def start
29
29
  server = TCPServer.new(@options[:Host], @options[:Port])
30
30
 
31
+ puts "# Ruby version: #{RUBY_VERSION}"
32
+ puts "# Min threads: #{@options[:MinThreads]}, max threads: #{@options[:MaxThreads]}"
33
+ puts "# Environment: #{@options[:environment]}"
34
+ puts "# Master PID: #{Process.pid}"
35
+ puts "# Listening on tcp://#{server.addr.last}:#{@options[:Port]}"
36
+
37
+ if @options[:Workers]
38
+ start_clustered(server)
39
+ else
40
+ start_single(server)
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def start_single(server)
47
+ puts '# Started in single mode'
48
+ start!(server)
49
+ end
50
+
51
+ def start_clustered(server)
52
+ puts '# Started in clustered mode'
31
53
  child_pids = []
32
- @options[:Workers].to_i.times do
33
- child_pids << spawn_worker(server)
54
+
55
+ puts '# ================================'
56
+ puts "# Spinning up #{@options[:Workers].to_i} Workers"
57
+ @options[:Workers].to_i.times do |i|
58
+ pid = fork do
59
+ start!(server)
60
+ end
61
+
62
+ puts "# Worker #{i} started with pid: #{pid}"
63
+
64
+ child_pids << pid
34
65
  end
35
66
 
36
67
  Signal.trap 'SIGINT' do
@@ -45,23 +76,15 @@ module Reifier
45
76
  exit
46
77
  end
47
78
 
48
- puts "# Ruby version: #{RUBY_VERSION}"
49
- puts "# Min threads: #{@options[:MinThreads]}, max threads: #{@options[:MaxThreads]}"
50
- puts "# Environment: #{@options[:environment]}"
51
- puts "# Number of Workers used: #{@options[:Workers]}"
52
- puts "# Master PID: #{Process.pid}"
53
- puts "# Listening on tcp://#{server.addr.last}:#{@options[:Port]}"
54
-
55
79
  loop do
56
80
  pid = Process.wait
57
81
  STDERR.puts "Process #{pid} crashed"
58
82
 
59
83
  child_pids.delete(pid)
60
- child_pids << spawn_worker
84
+ child_pids << spawn_worker(server)
61
85
  end
62
86
  end
63
87
 
64
- private
65
88
 
66
89
  def threads(min, max)
67
90
  @options[:MinThreads] = min
@@ -72,50 +95,40 @@ module Reifier
72
95
  @options[:Workers] = count
73
96
  end
74
97
 
75
- def spawn_worker(server)
76
- fork do
77
- pool = Concurrent::ThreadPoolExecutor.new(
78
- min_threads: @options[:MinThreads],
79
- max_threads: @options[:MaxThreads],
80
- max_queue: 0,
81
- fallback_policy: :caller_runs,
82
- )
83
-
84
- # Signal.trap 'SIGINT' do
85
- # puts "Shutting down thread pool in Worker: #{Process.pid}"
86
- # pool.shutdown
87
- # pool.wait_for_termination
88
-
89
- # exit
90
- # end
91
-
92
- loop do
93
- socket = server.accept
94
- socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
95
-
96
- Concurrent::Future.new(executor: pool) do
97
- begin
98
- request = Request.new(socket, @options)
99
- response = Response.new(socket)
100
-
101
- request.handle
102
-
103
- response.protocol = request.protocol
104
- response << @app.call(request.rack_env)
105
-
106
- response.handle
107
- rescue EOFError
108
- # nothing, shit happens
109
- rescue Exception => e
110
- socket.close
111
-
112
- STDERR.puts ERROR_HEADER
113
- STDERR.puts "#{e.class}: #{e}"
114
- STDERR.puts e.backtrace
115
- STDERR.puts ERROR_FOOTER
116
- end
117
- end.execute
118
- end
98
+ def start!(server)
99
+ pool = Concurrent::ThreadPoolExecutor.new(
100
+ min_threads: @options[:MinThreads],
101
+ max_threads: @options[:MaxThreads],
102
+ max_queue: 0,
103
+ fallback_policy: :caller_runs,
104
+ )
105
+
106
+ loop do
107
+ socket = server.accept
108
+ socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
109
+
110
+ Concurrent::Future.new(executor: pool) do
111
+ begin
112
+ request = Request.new(socket, @options)
113
+ response = Response.new(socket)
114
+
115
+ request.handle
116
+
117
+ response.protocol = request.protocol
118
+ response << @app.call(request.rack_env)
119
+
120
+ response.handle
121
+ rescue EOFError
122
+ # nothing, shit happens
123
+ rescue Exception => e
124
+ socket.close
125
+
126
+ STDERR.puts ERROR_HEADER
127
+ STDERR.puts "#{e.class}: #{e}"
128
+ STDERR.puts e.backtrace
129
+ STDERR.puts ERROR_FOOTER
130
+ end
131
+ end.execute
119
132
  end
120
133
  end
121
134
  end
@@ -1,3 +1,3 @@
1
1
  module Reifier
2
- VERSION = "0.5.0".freeze
2
+ VERSION = "0.6.0".freeze
3
3
  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.5.0
4
+ version: 0.6.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-19 00:00:00.000000000 Z
11
+ date: 2016-02-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby