adelnor 0.0.6 → 0.0.7

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: dd3a506fee52615e09d8aed4c85bea4a9dfdc58567a373b40b5960d903dc621f
4
- data.tar.gz: b69d40e33b17d24932f6feee996116c785660e9519b9062a95de258bca85d0b7
3
+ metadata.gz: 5bbe7f5e96936b78536738d717ea5babeb525aec4ecc202774e6564095d21663
4
+ data.tar.gz: 8da6cf7523a6d8f5d520fe6eb1c8e02320b53e79f631b0e36d0401a29f093f47
5
5
  SHA512:
6
- metadata.gz: f2f5ea6b2e64cdb6f9f5447a7590d73fa5bcb1b251b3e438b56bd9bc44a8112a9dc95bbf8492a871f29c61c4dc15b6604673517a3d06035d1234fbcedbfec8e0
7
- data.tar.gz: 99c4805e7b08d6948021d03fdee3c5c9dd311485e2d2bfde2c773241cfb900ce548fe8a5297044ecd9fbc43a3abc06faca0862fc626af620278740a16874a8ec
6
+ metadata.gz: b46502cfb371089998df7cf3d98221224df23ebb77cfd6f368d9d3b21df8ef8244a8cde14207c8b511146011fcd6766498483288ade45f263fdb00b4c7ed3927
7
+ data.tar.gz: d7fdfbe381e3c56c4c6bfdc7109afdd10ee9bd05c7d95d5da012fe671f3b547763a3bb916e519c2228038a1a6be7c473236c5bb623662e0ff7b8f7672209349b
data/README.md CHANGED
@@ -7,12 +7,13 @@
7
7
  ![Build](https://github.com/leandronsp/adelnor/actions/workflows/build.yml/badge.svg)
8
8
  [![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop)
9
9
  [![Ruby Style Guide](https://img.shields.io/badge/code_style-community-brightgreen.svg)](https://rubystyle.guide)
10
- ``` .___ .__
11
- _____ __| _/____ | | ____ ___________
12
- \__ \ / __ |/ __ \| | / \ / _ \_ __ \
13
- / __ \_/ /_/ \ ___/| |_| | ( <_> ) | \/
14
- (____ /\____ |\___ >____/___| /\____/|__|
15
- \/ \/ \/ \/
10
+ ```
11
+ ___ _______ _______ __ .__ __. ______ .______
12
+ / \ | \ | ____|| | | \ | | / __ \ | _ \
13
+ / ^ \ | .--. || |__ | | | \| | | | | | | |_) |
14
+ / /_\ \ | | | || __| | | | . ` | | | | | | /
15
+ / _____ \ | '--' || |____ | `----.| |\ | | `--' | | |\ \----.
16
+ /__/ \__\ |_______/ |_______||_______||__| \__| \______/ | _| `._____|
16
17
  ```
17
18
 
18
19
  [adelnor](https://rubygems.org/gems/adelnor) is a dead simple, yet Rack-compatible, HTTP server written in Ruby.
@@ -56,4 +57,16 @@ $ make bundle.install
56
57
  $ make sample.server
57
58
  ```
58
59
 
60
+ ## Using Thread Pool
61
+
62
+ ```ruby
63
+ require './lib/adelnor/server'
64
+
65
+ app = -> (env) do
66
+ [200, { 'Content-Type' => 'text/html' }, 'Hello world!']
67
+ end
68
+
69
+ Adelnor::Server.run app, 3000, thread_pool: 5
70
+ ```
71
+
59
72
  Open `http://localhost:3000`
data/adelnor.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'adelnor'
5
- spec.version = '0.0.6'
5
+ spec.version = '0.0.7'
6
6
  spec.summary = 'Adelnor HTTP server'
7
7
  spec.description = 'A dead simple, yet Rack-compatible, HTTP server written in Ruby'
8
8
  spec.authors = ['Leandro Proença']
@@ -21,6 +21,8 @@ module Adelnor
21
21
  lines = @message.split(/\r\n/)
22
22
  headline = lines.shift
23
23
 
24
+ return self unless headline
25
+
24
26
  parse_headline!(headline)
25
27
  parse_headers!(lines)
26
28
 
@@ -19,6 +19,6 @@ module Adelnor
19
19
  private
20
20
 
21
21
  def headers_as_string = @headers.reduce('', &method(:acc_header_string))
22
- def acc_header_string(acc, (key, value)) = acc += "#{key}: #{value}\r\n"
22
+ def acc_header_string(acc, (key, value)) = acc + "#{key}: #{value}\r\n"
23
23
  end
24
24
  end
@@ -9,15 +9,17 @@ require_relative './response'
9
9
 
10
10
  module Adelnor
11
11
  class Server
12
- def initialize(rack_app, port)
12
+ def initialize(rack_app, port, options = {})
13
13
  @rack_app = rack_app
14
14
  @port = port
15
+ @options = options
15
16
  @socket = Socket.new(:INET, :STREAM)
16
17
  addr = Socket.sockaddr_in(@port, '0.0.0.0')
17
18
 
18
19
  @socket.bind(addr)
19
20
  @socket.listen(2)
20
21
 
22
+ @thread_queue = Queue.new if @options[:thread_pool]
21
23
  puts welcome_message
22
24
  end
23
25
 
@@ -39,15 +41,44 @@ module Adelnor
39
41
  end
40
42
 
41
43
  def run
44
+ if (pool_size = @options[:thread_pool])
45
+ return run_with_thread_pool(pool_size)
46
+ end
47
+
42
48
  loop do
43
49
  client, = @socket.accept
44
50
 
45
51
  handle(client)
46
-
47
52
  client.close
48
53
  end
49
54
  end
50
55
 
56
+ def run_with_thread_pool(pool_size)
57
+ pool_size.times do
58
+ handle_request_in_thread
59
+ end
60
+
61
+ loop do
62
+ client, = @socket.accept
63
+
64
+ @thread_queue.push(client)
65
+ end
66
+ end
67
+
68
+ def handle_request_in_thread
69
+ Thread.new do
70
+ tid = Thread.current.object_id
71
+
72
+ puts "[#{tid}] Thread started"
73
+ loop do
74
+ client = @thread_queue.pop
75
+
76
+ handle(client)
77
+ client.close
78
+ end
79
+ end
80
+ end
81
+
51
82
  def handle(client)
52
83
  read_request_message(client)
53
84
  .then { |message| Request.build(message) }
@@ -61,9 +92,11 @@ module Adelnor
61
92
  def read_request_message(client)
62
93
  message = ''
63
94
 
64
- message += client.gets
65
- puts
66
- puts "[#{Time.now}] #{message}"
95
+ if (line = client.gets)
96
+ message += line
97
+ end
98
+
99
+ puts "\n[#{Time.now}] #{message}"
67
100
 
68
101
  while (line = client.gets)
69
102
  break if line == "\r\n"
@@ -75,6 +108,8 @@ module Adelnor
75
108
  end
76
109
 
77
110
  def welcome_message
111
+ thread_pool_message = "Running with thread pool of #{@options[:thread_pool]} threads" if @options[:thread_pool]
112
+
78
113
  <<~WELCOME
79
114
  |\---/|
80
115
  | o_o |
@@ -86,6 +121,8 @@ module Adelnor
86
121
 
87
122
  Adelnor is running at http://0.0.0.0:#{@port}
88
123
  Listening to HTTP connections
124
+
125
+ #{thread_pool_message}
89
126
  WELCOME
90
127
  end
91
128
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adelnor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leandro Proença
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-22 00:00:00.000000000 Z
11
+ date: 2023-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -43,7 +43,7 @@ homepage: https://github.com/leandronsp/adelnor
43
43
  licenses:
44
44
  - MIT
45
45
  metadata: {}
46
- post_install_message:
46
+ post_install_message:
47
47
  rdoc_options: []
48
48
  require_paths:
49
49
  - lib
@@ -58,8 +58,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
58
  - !ruby/object:Gem::Version
59
59
  version: '0'
60
60
  requirements: []
61
- rubygems_version: 3.3.3
62
- signing_key:
61
+ rubygems_version: 3.4.10
62
+ signing_key:
63
63
  specification_version: 4
64
64
  summary: Adelnor HTTP server
65
65
  test_files: []