tp2 0.8.1 → 0.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 75f66b5020339be69ee5ee0122eea1673a4f96330cedfec95cde805ef43b9221
4
- data.tar.gz: efc072f110cacc703c6ae75f4bade2a0753c73ecffae0f3055f6346eb165c79d
3
+ metadata.gz: 991ec5937591a3e57675d686d11670b682e7c5644391b4f301b091befcbae17f
4
+ data.tar.gz: 7413f8027c235ff4377077d7391b58c7540cf1257d05c3d9c6b837705c1c1329
5
5
  SHA512:
6
- metadata.gz: 5d522b6958b99135fe730b514d30ec8be9e18e01cabce1892e188ca2c255c5f963c4fc6ecf6c6b6f82b0c7ef9fc10c56dd2020a57bfbf9ea7abf27b3a3c88d2b
7
- data.tar.gz: 64a166087f1bae5a36f648ae2068ffa731fc4ecba6e77c400a311fc8be82eea795cf25b640f94f91731d7d3d7484645837b462a0058159a3eb68f2787e54e8d5
6
+ metadata.gz: fead1d1fdfad4ad3a66e757ed9613af4651fe25fe112cfede015d7c410f4655e52f51eaaa7b7530aea84d49201566088e88305464cd823a8dd9583b7bf21297d
7
+ data.tar.gz: 01cfb68c33c5a7b2a652e7d01d87798be696dc3bcbc0a62dbc1b9c134e709170cc9fc35d9ca4873518bb308eccf0f0973720238814f167e52f34a203cfff03d8
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ # Version 0.8.3 2025-06-09
2
+
3
+ - Improve error handling
4
+
5
+ # Version 0.8.2 2025-06-07
6
+
7
+ - Improve logging
8
+
1
9
  # Version 0.8 2025-06-07
2
10
 
3
11
  - Add silent mode
@@ -16,6 +16,7 @@ module TP2
16
16
 
17
17
  @done = nil
18
18
  @response_headers = nil
19
+ @status = nil
19
20
  end
20
21
 
21
22
  def run
@@ -25,13 +26,18 @@ module TP2
25
26
  persist = serve_request
26
27
  break if !persist
27
28
  end
28
- rescue UM::Terminate
29
- # terminated
30
29
  rescue Exception => e
31
- puts '!' * 40
32
- p e
33
- puts e.backtrace.join("\n")
34
- exit!
30
+ case e
31
+ when UM::Terminate
32
+ # we're done
33
+ when SystemCallError
34
+ @opts[:log]&.log("Encountered #{e.class}, closing connection")
35
+ when StandardError
36
+ @opts[:log]&.log("Unhandled exception #{e.class}: #{e.message}, closing connection")
37
+ @opts[:log]&.log(e.backtrace.join("\n"))
38
+ else
39
+ raise
40
+ end
35
41
  ensure
36
42
  @machine.close(@fd)
37
43
  end
@@ -161,16 +167,17 @@ module TP2
161
167
  private
162
168
 
163
169
  def log(request, response_headers)
170
+ return if !@opts[:log]
171
+
164
172
  request_headers = request.headers
165
173
  str = format(
166
- "%<stamp>s %<method>s %<path>s %<status>s %<tx>d\n",
167
- stamp: Time.now.strftime('%Y-%m-%d %H:%M:%S.%3N'),
174
+ "%<method>s %<path>s %<status>s %<tx>d",
168
175
  method: request_headers[':method'].upcase,
169
176
  path: request_headers[':path'],
170
177
  status: @status,
171
178
  tx: request_headers[':tx']
172
179
  )
173
- @machine.write_async(1, str)
180
+ @opts[:log].log(str)
174
181
  end
175
182
 
176
183
  RE_REQUEST_LINE = /^([a-z]+)\s+([^\s]+)\s+(http\/[0-9\.]{1,3})/i.freeze
data/lib/tp2/logger.rb ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TP2
4
+ class Logger
5
+ def initialize(machine, fd = STDOUT.fileno, **opts)
6
+ @machine = machine
7
+ @fd = fd
8
+ @opts = opts
9
+ end
10
+
11
+ def log(str)
12
+ str = format("%s %s\n", Time.now.strftime('%Y-%m-%d %H:%M:%S.%3N'), str)
13
+ @machine.write_async(@fd, str)
14
+ end
15
+ end
16
+ end
data/lib/tp2/server.rb CHANGED
@@ -15,9 +15,9 @@ module TP2
15
15
  TP2::RackAdapter.load(opts[:app_location])
16
16
  end
17
17
 
18
- def self.tp2_app(opts)
18
+ def self.tp2_app(machine, opts)
19
19
  if opts[:app_location]
20
- puts "Loading app at #{opts[:app_location]}" if opts[:log]
20
+ opts[:log]&.log("Loading app at #{opts[:app_location]}")
21
21
  require opts[:app_location]
22
22
 
23
23
  opts.merge!(TP2.config)
@@ -40,7 +40,7 @@ module TP2
40
40
  def app_from_opts
41
41
  case @opts[:app_type]
42
42
  when nil, :tp2
43
- Server.tp2_app(@opts)
43
+ Server.tp2_app(@machine, @opts)
44
44
  when :rack
45
45
  Server.rack_app(@opts)
46
46
  when :static
@@ -67,7 +67,7 @@ module TP2
67
67
  @accept_fibers << @machine.spin { accept_incoming(fd) }
68
68
  end
69
69
  bind_string = bind_info.map { it.join(':') }.join(", ")
70
- @machine.puts("Listening on #{bind_string}") if @opts[:log]
70
+ @opts[:log]&.log("Listening on #{bind_string}")
71
71
 
72
72
  # map fibers
73
73
  @connection_fiber_map = {}
@@ -123,6 +123,8 @@ module TP2
123
123
  end
124
124
 
125
125
  def graceful_shutdown
126
+ @opts[:log]&.log("Shutting down gracefully...")
127
+
126
128
  # stop listening
127
129
  close_all_server_fds
128
130
  @machine.snooze
data/lib/tp2/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module TP2
2
- VERSION = '0.8.1'
2
+ VERSION = '0.8.3'
3
3
  end
data/lib/tp2.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require 'uringmachine'
4
4
  require 'tp2/version'
5
5
  require 'tp2/server'
6
+ require 'tp2/logger'
6
7
 
7
8
  class UringMachine
8
9
  def puts(str)
@@ -24,16 +25,23 @@ module TP2
24
25
 
25
26
  class << self
26
27
  def run(opts = nil, &app)
27
- return if @in_run
28
+ if @in_run
29
+ @config = opts if opts
30
+ @config[:app] = app if app
31
+ return
32
+ end
28
33
 
29
34
  opts ||= @config || {}
30
35
  begin
31
36
  @in_run = true
32
37
  machine = UM.new
38
+
39
+ machine.puts(TP2::BANNER) if opts[:banner]
40
+ opts[:log] = opts[:log] && TP2::Logger.new(machine, **opts)
41
+
33
42
  server = Server.new(machine, opts, &app)
34
43
 
35
44
  setup_signal_handling(machine, Fiber.current)
36
- machine.puts(TP2::BANNER) if opts[:banner]
37
45
  server.run
38
46
  ensure
39
47
  @in_run = false
@@ -59,7 +67,6 @@ module TP2
59
67
  # to be done
60
68
  def watch_for_int_signal(machine, queue, fiber)
61
69
  sig = machine.shift(queue)
62
- machine.puts("\nGot #{sig} signal, shutting down gracefully...")
63
70
  machine.schedule(fiber, UM::Terminate.new)
64
71
  end
65
72
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tp2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
@@ -71,6 +71,7 @@ files:
71
71
  - examples/simple.rb
72
72
  - lib/tp2.rb
73
73
  - lib/tp2/http1_adapter.rb
74
+ - lib/tp2/logger.rb
74
75
  - lib/tp2/rack_adapter.rb
75
76
  - lib/tp2/request_extensions.rb
76
77
  - lib/tp2/server.rb