crap_server 0.0.4.1 → 0.0.4.2

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: 153311cb4783062e2a2ff30c82bde05b90eabd31
4
- data.tar.gz: c1b552c709961e5673f398f285e89d27e3767a4d
3
+ metadata.gz: b8f32930965a763afe13876581ab27b27c5c4803
4
+ data.tar.gz: 29ae4fb42d5d261b4e2a4d4e48e3a38f2d5a6282
5
5
  SHA512:
6
- metadata.gz: 88a68cec8ed174dd4dcbffd60e84ab78d24cd1d34d6f5c4e54d62ff60ae4b3e046b84edd311fac55326a792c49a35c089ceae253de68695c9ce3c2faae754a99
7
- data.tar.gz: 047d1427c76f4e6d5a82deaea1f0471231eefb2ebf315d83d59333b2339e7a6a1fdb815d7e44a218231d51b6d2523e6951dd838da51c02d9c831dc27a4c084b0
6
+ metadata.gz: fbb0d3e5004bdafdafffb4b9b6d72188f771e7976ef60d411bb394d30bc35cbe5c1fa059af2c1c09beea38ddee2642cd3400daf535a66ca7036fc28f6cd09c77
7
+ data.tar.gz: f7d46b54a5e050e13d7f664ccb1f6034a89c45891b287d2ebfffebbc3aa2f16916d54b7be6d2cfa14d2cf3b642cb7dbc20aea742108f554ab7f8c6143bf005bb
data/README.md CHANGED
@@ -45,6 +45,10 @@ By default, the pool size run 10 threads per core. You can change it with the po
45
45
 
46
46
  ruby my_app.rb
47
47
 
48
+ # More info.
49
+
50
+ More info in the [wiki](https://github.com/anga/crap_server/wiki) of the gem.
51
+
48
52
  # Production ready?
49
53
 
50
54
  No. Use it under your own risk. Right now, the interface can change.
@@ -22,6 +22,10 @@ module CrapServer
22
22
  block.yield @config
23
23
  end
24
24
 
25
+ def per_process(&block)
26
+ @per_process_block = block
27
+ end
28
+
25
29
  # Main method. This setup all the connections and make the logic of the app
26
30
  def run!(&block)
27
31
  begin
@@ -63,6 +67,10 @@ module CrapServer
63
67
 
64
68
  protected
65
69
 
70
+ def per_process_block
71
+ @per_process_block
72
+ end
73
+
66
74
  # Open TCP connection (IPv4 and IPv6)
67
75
  def open_connections
68
76
  start_ipv4_socket
@@ -3,23 +3,12 @@ module CrapServer
3
3
  # The port used.
4
4
  # Default: 7331
5
5
  attr_accessor :port
6
- # Set to true if you want to manage the read.
7
- # Default false
8
- attr_accessor :manual_read
9
6
  # Max read buffer size
10
7
  # Default: 16K
11
8
  attr_accessor :read_buffer_size
12
9
  # The number of maximum penning connections.
13
10
  # Default: Max allowed by the OS
14
11
  attr_accessor :max_pending_connections
15
- # Working method (read, write, accept, connect).
16
- # Available values are:
17
- # :normal
18
- # :partial
19
- # :non_blocking
20
- # If :non_blocking is used TODO
21
- # Default :readpartial
22
- attr_accessor :method
23
12
  # Set to false if you want to manage the close of the connection.
24
13
  # Note that this require manual_read set to true.
25
14
  # DEPERCATED
@@ -28,9 +17,6 @@ module CrapServer
28
17
  attr_accessor :log_file
29
18
  # The log level used
30
19
  attr_accessor :log_level
31
- # The timeout using when we use non-blocking method
32
- # NOT USED
33
- attr_accessor :timeout
34
20
  # Thread pool size. 10 per cor by default
35
21
  attr_accessor :pool_size
36
22
  def initialize
@@ -42,7 +28,6 @@ module CrapServer
42
28
  @auto_close_connection = true
43
29
  @log_file = STDOUT
44
30
  @log_level = Logger::DEBUG
45
- @timeout = nil # By default, no timeout. Used to allow persistent connections.
46
31
  @pool_size = 10
47
32
  end
48
33
  end
@@ -18,6 +18,7 @@ module CrapServer
18
18
  end
19
19
 
20
20
  def remove_to_write(io)
21
+ @buffer ||= {}
21
22
  @to_write.delete io.fileno
22
23
  @buffer.delete io.fileno
23
24
  end
@@ -80,6 +81,7 @@ module CrapServer
80
81
  def close(io)
81
82
  remove_to_read io
82
83
  remove_to_write io
84
+ @closeaw ||= {}
83
85
  @closeaw.delete io.fileno
84
86
  io.close
85
87
  end
@@ -91,7 +93,6 @@ module CrapServer
91
93
  instance.socket = remote_socket
92
94
  instance.config = config
93
95
  instance.address = address_info
94
- instance.send(:method=, config.method)
95
96
  instance.handler = self
96
97
  instance.run data, &block
97
98
  end
@@ -117,6 +118,8 @@ module CrapServer
117
118
  begin
118
119
  _, data = socket, socket.read_nonblock(config.read_buffer_size)
119
120
  yield data, socket, address(socket)
121
+ # We close the connection if we auto_close_connection is true and the user didn't write in the buffer.
122
+ close socket if config.auto_close_connection && buffer(socket).nil?
120
123
  rescue Errno::EAGAIN
121
124
  rescue EOFError
122
125
  remove_to_read socket
@@ -5,7 +5,6 @@ module CrapServer
5
5
  attr_accessor :socket
6
6
  attr_accessor :address
7
7
  attr_accessor :config
8
- attr_accessor :method
9
8
  attr_accessor :handler
10
9
  def initialize; end
11
10
 
@@ -15,15 +14,7 @@ module CrapServer
15
14
  undef :call if self.respond_to? :call
16
15
  # Define the new method to bind the block with this class.
17
16
  self.class.send :define_method, :call, &block
18
- # Running the code depending of the number of args
19
- if block.parameters.size == 1
20
- self.call(data)
21
- elsif block.parameters.size == 2
22
- self.call(data, socket)
23
- else
24
- self.call(data, socket, address)
25
- end
26
- @socket.flush
17
+ self.call(data)
27
18
  end
28
19
 
29
20
  # Write to the client the given string
@@ -42,7 +33,7 @@ module CrapServer
42
33
 
43
34
  # Give access to logger class to the user
44
35
  def logger
45
- @config.logger
36
+ CrapServer::Application.send(:logger)
46
37
  end
47
38
  end
48
39
  end
@@ -41,6 +41,8 @@ module CrapServer
41
41
  def spawn_child
42
42
  fork do
43
43
  begin
44
+
45
+ CrapServer::Application.send(:per_process_block).call if not CrapServer::Application.send(:per_process_block).nil?
44
46
  pool = CrapServer::ThreadPool.new @sockets
45
47
  pool.run &@block_proc
46
48
  rescue Interrupt
@@ -1,3 +1,3 @@
1
1
  module CrapServer
2
- VERSION = '0.0.4.1'
2
+ VERSION = '0.0.4.2'
3
3
  end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe CrapServer::ConnectionHandler do
4
+
5
+ end
@@ -1,3 +1,36 @@
1
+ require 'spec_helper'
2
+
1
3
  describe CrapServer::ThreadPool do
4
+ context 'run' do
5
+ before do
6
+ allow_any_instance_of(CrapServer::ThreadPool).to receive(:spawn_thread).and_return(true)
7
+ allow(ThreadGroup).to receive(:new).and_return(Dummy)
8
+ allow_any_instance_of(CrapServer::ThreadPool).to receive(:sleep).and_return(true)
9
+ end
10
+ it 'should abort on exception' do
11
+ pool = CrapServer::ThreadPool.new([Dummy, Dummy])
12
+ expect(Thread).to receive(:abort_on_exception=).with(true)
13
+ pool.run do
14
+ end
15
+ end
16
+
17
+ it 'should spawn pool_size thread' do
18
+ pool = CrapServer::ThreadPool.new([Dummy, Dummy])
19
+ size = rand(2..20) # We make sure that is a random number and is not a selected test :P
20
+ CrapServer::Application.configure do |c|
21
+ c.pool_size = size
22
+ end
23
+ expect_any_instance_of(CrapServer::ThreadPool).to receive(:spawn_thread).exactly(size).times()
24
+ pool.run do
25
+ end
26
+ end
27
+ end
2
28
 
29
+ context 'spawn_thread' do
30
+ it 'should spawn a new thread' do
31
+ expect(Thread).to receive(:new).and_return(true)
32
+ pool = CrapServer::ThreadPool.new([Dummy, Dummy])
33
+ pool.send(:spawn_thread)
34
+ end
35
+ end
3
36
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crap_server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4.1
4
+ version: 0.0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andres Jose Borek
@@ -61,6 +61,7 @@ files:
61
61
  - lib/crap_server/thread_pool.rb
62
62
  - lib/crap_server/version.rb
63
63
  - spec/application_spec.rb
64
+ - spec/connection_handler_spec.rb
64
65
  - spec/forker_spec.rb
65
66
  - spec/spec_helper.rb
66
67
  - spec/thread_pool_spec.rb
@@ -90,6 +91,7 @@ specification_version: 4
90
91
  summary: Really thin a non intuitive ruby server.
91
92
  test_files:
92
93
  - spec/application_spec.rb
94
+ - spec/connection_handler_spec.rb
93
95
  - spec/forker_spec.rb
94
96
  - spec/spec_helper.rb
95
97
  - spec/thread_pool_spec.rb