spyder 0.1.1 → 0.1.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
  SHA256:
3
- metadata.gz: d8fbffd16e4740ebf51ca1e51cb49c5a2f665408580340786ac36ba484f045c4
4
- data.tar.gz: 6a0c26ce6168805cf1e1636835bd3aa4e376b022be5d4f2874bdfbc80682523e
3
+ metadata.gz: 3417dd82aa219dd2526c0fb01be1f6e9c58723651bac784b2f082a4c8d3b5f20
4
+ data.tar.gz: eca6629c49c0048f7304da5c71f8701d38b1a24ef4a160f923bf5267d0cea628
5
5
  SHA512:
6
- metadata.gz: 4cac8464742015ecba92eb7d0ff9ca0d3c7bde3ca14ea274200344de1b1fd92aa424aede419ceab17afe6e60693f54ed6077c27ab233459496e03a780026b9b2
7
- data.tar.gz: 37b4244fc29821e8efa337d7ffd0cf6e490bf1e94f526933bfafa1dea08b87c30b5312bfd8c6bf8209794108fab82bb0fc9debf9f2c72cc758cc16d9152104fd
6
+ metadata.gz: d97926b4bc3a3064d7e6224c0bbb61ec2a225207f766802be5821db8d9b092eba60e92aec37b5012843e073999aa057c79e946551503e680dcf7c2e327013f32
7
+ data.tar.gz: 51a1d5d5a6b4e8d7ba2598ddcf674e1acb224e35fa77a2c53f90a97f936709bc52274f9f92cef492d93f96ef3095758a7e946e63cc2db7720859a4c654096509
data/lib/spyder/server.rb CHANGED
@@ -4,8 +4,9 @@ module Spyder
4
4
  class Server
5
5
  attr_accessor :router
6
6
 
7
- def initialize(bind, port, router: Router.new, max_threads: 4)
7
+ def initialize(bind, port, router: Router.new, max_threads: 4, tcp_backlog: 10)
8
8
  @server = TCPServer.new(bind, port)
9
+ @tcp_backlog = tcp_backlog
9
10
  @max_threads = max_threads
10
11
  @middleware = []
11
12
  @threads = []
@@ -18,44 +19,45 @@ module Spyder
18
19
  end
19
20
 
20
21
  def start
21
- @server.listen(10)
22
- loop do
23
- client = @server.accept
22
+ busy_threads = 0
23
+ @server.listen(@tcp_backlog)
24
24
 
25
- app_thread = Thread.new do
26
- error = nil
27
- begin
28
- process_new_client(client)
29
- rescue Exception => e
30
- error = e
25
+ loop do
26
+ time_start = Process.clock_gettime(:CLOCK_MONOTONIC, :float_second)
27
+ loop do
28
+ current_busy = @tp_sync.synchronize { busy_threads }
29
+ break if current_busy < @max_threads
30
+ sleep(0)
31
+ current_time = Process.clock_gettime(:CLOCK_MONOTONIC, :float_second)
32
+ if (current_time - time_start) > 1.0
33
+ # puts "Waiting a long time: #{(current_time - time_start)}"
34
+ sleep 0.2
31
35
  end
36
+ end
32
37
 
33
- if error
34
- puts error.full_message
38
+ client = @server.accept
39
+ @tp_sync.synchronize { busy_threads += 1 }
35
40
 
36
- response = Response.make_generic :internal_server_error
37
- dispatch_response(client, response)
38
- end
41
+ Thread.new do
42
+ begin
43
+ error = nil
44
+ begin
45
+ process_new_client(client)
46
+ rescue Exception => e
47
+ error = e
48
+ end
39
49
 
40
- client.close rescue nil
41
- end
50
+ if error
51
+ puts error.full_message
42
52
 
43
- over_capacity = true
44
- added_thread_to_list = false
45
- while over_capacity
46
- @tp_sync.synchronize do
47
- unless added_thread_to_list
48
- @threads << app_thread
49
- added_thread_to_list = true
53
+ response = Response.make_generic :internal_server_error
54
+ dispatch_response(client, response)
50
55
  end
51
- over_capacity = (@threads.length >= @max_threads)
52
- # puts("#{@threads.length} of #{@max_threads}")
53
56
 
54
- @threads.delete_if { |t| !t.alive? }
57
+ client.close rescue nil
58
+ ensure
59
+ @tp_sync.synchronize { busy_threads -= 1 }
55
60
  end
56
-
57
- # puts("XXX OVER CAPACITY!") if over_capacity
58
- sleep 0 if over_capacity
59
61
  end
60
62
  end
61
63
  end
@@ -99,19 +101,24 @@ module Spyder
99
101
  content_length = response.body.length
100
102
  end
101
103
 
102
- socket.write("HTTP/1.1 #{response.code} #{response.reason_sentence.b}\r\n")
103
- response.headers.ordered.each do |name, value|
104
- socket.write("#{name.b}: #{value.b}\r\n")
105
- end
106
- socket.write("connection: close\r\n") # FIXME:
107
- socket.write("content-length: #{content_length}\r\n") if content_length
108
- socket.write("\r\n")
109
-
110
- if response.body
111
- Array(response.body).each do |part|
112
- content = part.respond_to?(:call) ? part.call : part
113
- socket.write(content.b)
104
+ begin
105
+ socket.write("HTTP/1.1 #{response.code} #{response.reason_sentence.b}\r\n")
106
+ response.headers.ordered.each do |name, value|
107
+ socket.write("#{name.b}: #{value.b}\r\n")
108
+ end
109
+ socket.write("connection: close\r\n") # FIXME:
110
+ socket.write("content-length: #{content_length}\r\n") if content_length
111
+ socket.write("\r\n")
112
+
113
+ if response.body
114
+ Array(response.body).each do |part|
115
+ content = part.respond_to?(:call) ? part.call : part
116
+ socket.write(content.b)
117
+ end
114
118
  end
119
+ rescue Errno::EPIPE
120
+ # socket closed. So what?
121
+ socket.close rescue nil
115
122
  end
116
123
  end
117
124
 
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Spyder
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.2'
5
5
  end
6
-
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spyder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - André D. Piske
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-02-11 00:00:00.000000000 Z
11
+ date: 2024-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mustermann