waitress-core 0.0.5 → 0.1.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: 61bec41dd394be9dd6aa5baff4d186c31c43d6e3
4
- data.tar.gz: f4e11e40a4b50397c7db4d58a8b29872c520d5d3
3
+ metadata.gz: 53fac76668e1321e024fed8489d1e3bcf80075e2
4
+ data.tar.gz: 8cc2c10b4661f3c42a5432eda9504691d1d47bf0
5
5
  SHA512:
6
- metadata.gz: 1848048ee5ddecd831b8c3c4e2339762e0a0e1e49c5846087115487d72d42dde4d390fdc18a9511b50e5499dc2cfaf03b7c442673fe611835d89e3eb43517565
7
- data.tar.gz: bd2b4cc0aae4ad4a1127a600597af570d593f4f217894bc6a6e2a18c197876843837473dd8145853bd042b3a6f8de333cc5f58129eaa23d5e52bc02303f86795
6
+ metadata.gz: efb9647f241a506830c1a69f18869e2fcc7b67d8dc092ce0b14512445773fb2e8118a9c3c2ad8809e892f48f176588e32e8e510ef6e9aaaa30c73ec44ca2bb08
7
+ data.tar.gz: fc393bf48cdebecbadc08d8a1dc3ec998443c3ebeb00242065a0bb5c0e03b65f45f670f92c389c8370cfc17d6f1e3f1922fb99d5c3dd2f27286a7c070ff233b0
data/lib/waitress.rb CHANGED
@@ -25,6 +25,13 @@ require 'fileutils'
25
25
  # classes and utilities created by Waitress
26
26
  module Waitress
27
27
 
28
+ SERVERS = []
29
+
30
+ at_exit do
31
+ SERVERS.each { |x| x.killall }
32
+ exit
33
+ end
34
+
28
35
  # Create a new Waitress Server, or, in case of the Filesystem, create a Configuration
29
36
  # for a set of Servers
30
37
  # Params:
@@ -27,6 +27,7 @@ module Waitress
27
27
  @servers = @configurations.map do |conf|
28
28
  s = Waitress::HttpServer.new
29
29
  conf.hosts.each { |h| s << h }
30
+ s.set_processes conf.processes
30
31
  s.ports *conf.ports
31
32
  s
32
33
  end
@@ -82,11 +83,15 @@ module Waitress
82
83
 
83
84
  attr_accessor :hosts
84
85
  attr_accessor :ports
86
+ attr_accessor :processes
85
87
 
86
88
  def initialize configure, *ports
87
89
  @ports = *ports
88
90
  @hosts = []
89
91
  @configure = configure
92
+
93
+ @processes = 10
94
+ @processes = ENV["WAITRESS_PROCESSES"].to_i if ENV.include? "WAITRESS_PROCESSES"
90
95
  end
91
96
 
92
97
  # Create a new VirtualHost with the given regex, priority, and configure it
@@ -101,7 +106,7 @@ module Waitress
101
106
 
102
107
  # Over all the registered VHosts, call this method. Use this to configure
103
108
  # a similar setting for all vhosts at once instead of duplicating code
104
- # across all of them.
109
+ # across all of them.
105
110
  def all_hosts &block
106
111
  @hosts.each { |h| block.call(h) }
107
112
  end
@@ -96,7 +96,8 @@ module Waitress
96
96
  end
97
97
  end
98
98
  done
99
- sock.close
99
+ sock.close rescue nil
100
+ @io.close rescue nil
100
101
  end
101
102
  end
102
103
 
@@ -12,11 +12,22 @@ module Waitress
12
12
  attr_accessor :http_body
13
13
  end
14
14
 
15
+ attr_accessor :processes
16
+
15
17
  # Create a new Server instance with the given ports. If no ports are given,
16
18
  # port 80 will be used as a default
17
19
  def initialize(*ports)
20
+ Waitress::SERVERS << self
18
21
  ports << 80 if ports.length == 0
19
22
  @ports = ports
23
+ @processes = 10
24
+ @processes = ENV["WAITRESS_PROCESSES"].to_i if ENV.include? "WAITRESS_PROCESSES"
25
+ @running_processes = []
26
+ end
27
+
28
+ # Set the amount of concurrent Waitress Processes to run on this Server, per Port
29
+ def set_processes count
30
+ @processes = count
20
31
  end
21
32
 
22
33
  # Set or Get the ports for this server. If arguments are provided, the ports
@@ -32,16 +43,25 @@ module Waitress
32
43
  # set (or 80 for the default)
33
44
  def run *ports
34
45
  @ports = ports unless ports.length == 0
35
- @threads = @ports.map { |x| Thread.new { launch_port x } }
36
46
  self.each do |vhost|
37
47
  vhost.on_server_start self
38
48
  end
49
+
50
+ @running_processes = @ports.map do |port|
51
+ launch_port(port)
52
+ end
53
+ @running_processes.flatten!
39
54
  self
40
55
  end
41
56
 
57
+ # Killall running processes
58
+ def killall
59
+ @running_processes.each { |x| x.kill }
60
+ end
61
+
42
62
  # Join the server, blocking the current thread in order to keep the server alive.
43
63
  def join
44
- @threads.each { |x| x.join }
64
+ @running_processes.each { |x| x.wait }
45
65
  end
46
66
 
47
67
  # Handle a client based on an IO stream, if you plan to serve on a non-socket
@@ -52,48 +72,54 @@ module Waitress
52
72
 
53
73
  :private
54
74
  def launch_port port
55
- @server = TCPServer.new port
56
- while true
57
- client = @server.accept
58
- go do
59
- begin
60
- handle_client client
61
- rescue => e
62
- puts "Server Error: #{e} (Fix This!)"
63
- puts e.backtrace
75
+ serv = TCPServer.new port
76
+ processes = []
77
+ @processes.times do
78
+ processes << gofork {
79
+ while true
80
+ begin
81
+ client = serv.accept
82
+ handle_client client
83
+ rescue => e
84
+ puts "Server Error: #{e} (Fix This!)"
85
+ puts e.backtrace
86
+ client.close rescue nil
87
+ end
64
88
  end
65
- end
89
+ }
66
90
  end
91
+ processes
67
92
  end
68
93
 
69
94
  def handle_client client_socket
70
- gofork do
71
- begin
72
- data = client_socket.readpartial(8192)
73
- nparsed = 0
74
-
75
- parser = Waitress::HttpParser.new
76
- params = HttpParams.new
77
-
78
- while nparsed < data.length
79
- nparsed = parser.execute(params, data, nparsed)
80
- if parser.finished?
81
- build_request params, client_socket
82
- else
83
- ch = client.readpartial(8192)
84
- break if !ch or ch.length == 0
85
-
86
- data << ch
87
- end
95
+ # pro = gofork do
96
+ begin
97
+ data = client_socket.readpartial(8192)
98
+ nparsed = 0
99
+
100
+ parser = Waitress::HttpParser.new
101
+ params = HttpParams.new
102
+
103
+ while nparsed < data.length
104
+ nparsed = parser.execute(params, data, nparsed)
105
+ if parser.finished?
106
+ build_request params, client_socket
107
+ else
108
+ ch = client.readpartial(8192)
109
+ break if !ch or ch.length == 0
110
+
111
+ data << ch
88
112
  end
89
- rescue EOFError, Errno::ECONNRESET, Errno::EPIPE, Errno::EINVAL, Errno::EBADF
90
- client_socket.close rescue nil
91
- rescue => e
92
- puts "Client Error: #{e}"
93
- puts e.backtrace
94
113
  end
95
- end.wait
96
- client_socket.close unless client_socket.closed?
114
+ rescue EOFError, Errno::ECONNRESET, Errno::EPIPE, Errno::EINVAL, Errno::EBADF
115
+ client_socket.close rescue nil
116
+ rescue => e
117
+ puts "Client Error: #{e}"
118
+ puts e.backtrace
119
+ end
120
+ # end
121
+ client_socket.close rescue nil
122
+ # pro.wait
97
123
  end
98
124
 
99
125
  def build_request headers, client_socket
@@ -1,3 +1,3 @@
1
1
  module Waitress
2
- VERSION = "0.0.5"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: waitress-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaci Brunning