midori.rb 0.5.4 → 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
  SHA256:
3
- metadata.gz: 6f47091da1ed0c2aa5186718051d4c0f67cf691eb0c0cf579bbfeff0fa007e1e
4
- data.tar.gz: 172665350943d2de1faae70882bfad72a799756edc3d993185ea6a6d0cb1661e
3
+ metadata.gz: ccffda67e06a88ed46a9e81ac52823498d331a147869f54a3c520bc70946f469
4
+ data.tar.gz: 2abeba1e323655b28a547814eccc536773b35316075b3852897ec109b4272719
5
5
  SHA512:
6
- metadata.gz: 1a6801c29d82920d82cd25760ca929f3be55a402169d5518d34941409949e99e799e4a1170b267ccfe7707b66c00ace0b5b0674fffb1ebc3dfd842607a05025e
7
- data.tar.gz: 6ee0eb8e0b7e496ca8c2a8f71e7d5e03d28172bd10839ae92e2ef01981076b3f9aba50d9ae1e7c1e6374b9b93c27fe8b63e1260de40eef8c67595f9dce45c7d2
6
+ metadata.gz: d68c112f68c228a79b46f2983e0bd4ba9b0ff25b8aa9025eca7893909719e7213a8236fca26d78ba2aad12926481d7f989d920148e00b866f9f86c8a6b3cff20
7
+ data.tar.gz: 6eae1c01af1fa93acf6bf817ce1c04121918a53d1f849815778eed0f974c36259bd1ee6df3901b86128d247b77dfca5c81e04e5e1477836aa2dd31c0c2a1061e
@@ -4,7 +4,6 @@ class Midori::Configure
4
4
  extend Configurable
5
5
 
6
6
  set :logger, ::Logger.new(STDOUT)
7
- set :protocol, :http
8
7
  set :bind, '127.0.0.1'
9
8
  set :port, 8080
10
9
  set :route_type, :sinatra
@@ -14,4 +13,7 @@ class Midori::Configure
14
13
  set :trusted_proxies, /\A127\.0\.0\.1\Z|\A(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\.|
15
14
  \A::1\Z|\Afd[0-9a-f]{2}:.+|\Alocalhost\Z|\Aunix\Z|\Aunix:/ix
16
15
  set :tcp_fast_open, true
16
+ set :keep_alive, true
17
+ set :keep_alive_timeout, 75
18
+ set :keep_alive_requests, 1000
17
19
  end
@@ -23,9 +23,7 @@ class Midori::Connection
23
23
  def listen(socket)
24
24
  EventLoop.register(socket, :rw) do |monitor|
25
25
  @monitor = monitor
26
- if monitor.readable?
27
- receive_data(monitor)
28
- end
26
+ receive_data(monitor) if monitor.readable?
29
27
  if monitor.writable?
30
28
  if !@buffer.empty?
31
29
  send_buffer
@@ -45,6 +43,7 @@ class Midori::Connection
45
43
  end
46
44
 
47
45
  # Send buffer immediately
46
+ # @return [nil] nil
48
47
  private def send_buffer
49
48
  if @monitor.writable?
50
49
  written = @socket.write_nonblock(@buffer)
@@ -59,13 +58,18 @@ class Midori::Connection
59
58
  end
60
59
 
61
60
  # Close the connection
61
+ # @return [nil] nil
62
62
  def close_connection
63
+ EventLoop.remove_timer(@keep_alive_timer) unless @keep_alive_timer.nil? # Be sure to remove timer for memory safety
63
64
  EventLoop.deregister @socket
64
65
  @socket.close
66
+ nil
65
67
  end
66
68
 
67
69
  # Close the connection after writing
70
+ # @return [nil] nil
68
71
  def close_connection_after_writing
69
72
  @close_flag = true
73
+ nil
70
74
  end
71
75
  end
data/lib/midori/server.rb CHANGED
@@ -17,40 +17,41 @@ module Midori::Server
17
17
  def server_initialize(api, logger)
18
18
  @api = api
19
19
  @logger = logger
20
+
20
21
  @request = Midori::Request.new
21
22
  @websocket = Midori::WebSocket.new(self)
22
23
  @eventsource = Midori::EventSource.new(self)
24
+
25
+ # Add keep-alive parameters
26
+ @keep_alive_timer = nil
27
+ @keep_alive_count = 1
23
28
  end
24
29
 
25
30
  # Logic of receiving data
26
- # @param [String] monitor the socket able to read
31
+ # @param [NIO::Monitor] monitor the socket able to read
27
32
  def receive_data(monitor)
28
- lambda do
29
- async_fiber(Fiber.new do
30
- begin
31
- _sock_domain, remote_port, _remote_hostname, remote_ip = monitor.io.peeraddr
32
- port, ip = remote_port, remote_ip
33
- @request.ip = ip
34
- @request.port = port
35
- data = monitor.io.read_nonblock(16_384)
36
- if @request.parsed? && @request.body_parsed?
37
- websocket_request(StringIO.new(data))
38
- else
39
- @request.parse(data)
40
- receive_new_request if @request.parsed && @request.body_parsed?
41
- end
42
- rescue EOFError, Errno::ENOTCONN => _e
43
- close_connection
44
- # Ignore client's disconnection
45
- rescue => e
46
- # :nocov:
47
- # Leave for corner cases
48
- close_connection
49
- @logger.warn "#{@request.ip} - - #{e.class} #{e.backtrace.join("\n")}".yellow
50
- # :nocov:
33
+ async_fiber(Fiber.new do
34
+ begin
35
+ _sock_domain, remote_port, _remote_hostname, remote_ip = monitor.io.peeraddr
36
+ @request.ip, @request.port = remote_port, remote_ip
37
+ data = monitor.io.read_nonblock(16_384)
38
+ if @request.parsed? && @request.body_parsed?
39
+ websocket_request(StringIO.new(data))
40
+ else
41
+ @request.parse(data)
42
+ receive_new_request if @request.parsed && @request.body_parsed?
51
43
  end
52
- end)
53
- end.call
44
+ rescue EOFError, Errno::ENOTCONN => _e
45
+ close_connection
46
+ # Ignore client's disconnection
47
+ rescue => e
48
+ # :nocov:
49
+ # Leave for corner cases
50
+ close_connection
51
+ @logger.warn "#{@request.ip} - - #{e.class} #{e.backtrace.join("\n")}".yellow
52
+ # :nocov:
53
+ end
54
+ end)
54
55
  end
55
56
 
56
57
  # Logic of receiving new request
@@ -68,9 +69,10 @@ module Midori::Server
68
69
  @logger.error e.inspect.red
69
70
  @logger.warn e.backtrace.join("\n").yellow
70
71
  end
72
+
71
73
  unless @request.websocket? || @request.eventsource?
72
74
  send_data @response
73
- close_connection_after_writing
75
+ proceed_keep_alive
74
76
  end
75
77
  end
76
78
 
@@ -109,4 +111,21 @@ module Midori::Server
109
111
  def call_event(event, args = [])
110
112
  -> { @websocket.instance_exec(*args, &@websocket.events[event]) }.call unless @websocket.events[event].nil?
111
113
  end
114
+
115
+ private def proceed_keep_alive
116
+ # Detect if it should close connection
117
+ if !Midori::Configure.keep_alive || (@keep_alive_count >= Midori::Configure.keep_alive_requests)
118
+ close_connection_after_writing
119
+ return
120
+ end
121
+ # Add timeout for keep-alive
122
+ @keep_alive_count += 1
123
+ EventLoop.remove_timer(@keep_alive_timer) unless @keep_alive_timer.nil?
124
+ @keep_alive_timer = EventLoop::Timer.new(Midori::Configure.keep_alive_timeout) do
125
+ close_connection
126
+ end
127
+ EventLoop.add_timer(@keep_alive_timer)
128
+ # Reset request
129
+ @request = Midori::Request.new
130
+ end
112
131
  end
@@ -1,5 +1,5 @@
1
1
  # Midori Module
2
2
  module Midori
3
3
  # Current Version Code
4
- VERSION = '0.5.4'.freeze
4
+ VERSION = '0.6.0'.freeze
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: midori.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - HeckPsi Lab
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-28 00:00:00.000000000 Z
11
+ date: 2018-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: murasaki
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.2.1
19
+ version: 0.2.2
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.2.1
26
+ version: 0.2.2
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: mustermann
29
29
  requirement: !ruby/object:Gem::Requirement