outpunch-ruby-rack 0.5.6 → 0.6.0

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: e12bd5a1fb701111fc1eed2534257f8d90858f35a4a15a53e0562379f9d75fa3
4
- data.tar.gz: e8f986b418f65133db10ea9addd345b964138103158be922af324bae4b72bb6a
3
+ metadata.gz: 1bdda000cffcb7d436395e010a97a2786ef206efec06aef9ca60a420973cd30f
4
+ data.tar.gz: 2d15be8d2e14a016e364974a31f4a540ae7d8a87777049334c6196c963ee0ef8
5
5
  SHA512:
6
- metadata.gz: f4eebc6f1efe3b408264ffaff68143f35685b47aa9feeb7a31153df21616bdde3bdc84630dc4bd019c996268532c7d9a4c46f3f9278678c8dea5004fb335bf96
7
- data.tar.gz: 43d3367f10c0e1eefbdf9e4633dd8d78db022f3875c5d119cdfcdeb4b06460d5e82b65a13b944dd2b162cd6c244dd24cdbebc601afa06d225c80d62447892bbb
6
+ metadata.gz: 5dab41f53b280f7802e0ab2507812df8c2e91a9bd022467e01f1bd1e30fc4c8d6d6c1fd5c32cf737be3e276cd984d5d5a40f73dce20fbfef73d06570b32f6afb
7
+ data.tar.gz: 6e84f446b4509a4286fc3f0efb589e38d03cb91906a244c8d585c64ce5a248daaef1084eb6ded7cc868b6ee78b39099782320ff76f5d3230940049f4ddce9368
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "io/wait"
3
4
  require "websocket/driver"
4
5
 
5
6
  module Outpunch
@@ -8,9 +9,13 @@ module Outpunch
8
9
  # Ruby handles socket I/O and WebSocket framing; the core handles auth,
9
10
  # routing and request state.
10
11
  class Connection
11
- def initialize(native)
12
+ def initialize(native, write_timeout: nil)
12
13
  @native = native
14
+ @write_timeout = write_timeout
13
15
  @write_mutex = Mutex.new
16
+ # Serializes socket writes: the reader thread writes pongs while the writer thread may be
17
+ # partway through a frame (write_within can take several write_nonblock calls).
18
+ @io_mutex = Mutex.new
14
19
  @io = nil
15
20
  @driver = nil
16
21
  @env = nil
@@ -22,6 +27,9 @@ module Outpunch
22
27
  @io = io
23
28
  @driver = WebSocket::Driver.rack(self)
24
29
  @driver.on(:message) { |event| @native.push_message(event.data) }
30
+ # A client ping (the driver answers it) counts as a sign of life (R30).
31
+ @driver.on(:ping) { @native.heartbeat }
32
+ @driver.on(:pong) { @native.heartbeat }
25
33
  @driver.on(:close) { @native.close }
26
34
  @driver.on(:error) { |e| log_error(e.message) }
27
35
  @driver.start
@@ -41,9 +49,16 @@ module Outpunch
41
49
  close_io
42
50
  end
43
51
 
44
- # Called by websocket-driver to write raw bytes to the socket.
52
+ # Called by websocket-driver to write raw bytes to the socket. A write that cannot
53
+ # finish within the write timeout (the client stopped reading) closes the connection,
54
+ # so the service is unregistered instead of queueing requests for it (F7 in
55
+ # spec/FINDINGS.md).
45
56
  def write(data)
46
- @io.write(data)
57
+ @io_mutex.synchronize do
58
+ next @io.write(data) unless @write_timeout && @io.respond_to?(:write_nonblock)
59
+
60
+ write_within(data, monotonic_now + @write_timeout) || stop_stalled
61
+ end
47
62
  rescue IOError, Errno::EPIPE, Errno::ECONNRESET
48
63
  nil
49
64
  end
@@ -71,6 +86,31 @@ module Outpunch
71
86
  close_io
72
87
  end
73
88
 
89
+ # Writes all of data unless the deadline passes first; returns false if it does.
90
+ def write_within(data, deadline)
91
+ until data.empty?
92
+ written = @io.write_nonblock(data, exception: false)
93
+ if written == :wait_writable
94
+ remaining = deadline - monotonic_now
95
+ return false unless remaining.positive? && @io.wait_writable(remaining)
96
+ else
97
+ data = data.byteslice(written..)
98
+ end
99
+ end
100
+ true
101
+ end
102
+
103
+ def stop_stalled
104
+ log_error("client stopped reading; closing the connection")
105
+ @native.close
106
+ close_io
107
+ nil
108
+ end
109
+
110
+ def monotonic_now
111
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
112
+ end
113
+
74
114
  def close_io
75
115
  @io.close unless @io.closed?
76
116
  rescue IOError
@@ -10,14 +10,18 @@ module Outpunch
10
10
  class Server
11
11
  HOP_BY_HOP = %w[HOST CONNECTION UPGRADE].freeze
12
12
 
13
- def initialize(secret:, timeout: 25)
13
+ # client_idle_timeout: seconds a connected client may send nothing (not even a WebSocket
14
+ # ping) before it is treated as gone; clients ping every 30 s by default.
15
+ def initialize(secret:, timeout: 25, client_idle_timeout: 90)
14
16
  raise ArgumentError, "outpunch secret must be set" if secret.nil? || secret.to_s.empty?
15
17
 
16
- @native = Outpunch::Server.new(secret.to_s, timeout.to_f, nil)
18
+ @timeout = timeout.to_f
19
+ @native = Outpunch::Server.new(secret.to_s, @timeout, nil, client_idle_timeout.to_f)
17
20
  end
18
21
 
22
+ # A socket write that takes longer than the request timeout closes the connection.
19
23
  def create_connection
20
- Connection.new(@native.create_connection)
24
+ Connection.new(@native.create_connection, write_timeout: @timeout)
21
25
  end
22
26
 
23
27
  def connected?(service_name)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Outpunch
4
4
  module Rack
5
- VERSION = "0.5.6"
5
+ VERSION = "0.6.0"
6
6
  end
7
7
  end
data/lib/outpunch/rack.rb CHANGED
@@ -10,10 +10,12 @@ require_relative "rack/middleware"
10
10
  module Outpunch
11
11
  module Rack
12
12
  class Configuration
13
- attr_accessor :secret, :timeout, :base_controller, :authorize_service, :hooks, :route_prefix
13
+ attr_accessor :secret, :timeout, :client_idle_timeout, :base_controller, :authorize_service, :hooks,
14
+ :route_prefix
14
15
 
15
16
  def initialize
16
17
  @timeout = 25
18
+ @client_idle_timeout = 90
17
19
  @base_controller = "ActionController::API"
18
20
  @hooks = Outpunch::Rack::Hooks
19
21
  @route_prefix = "/outpunch"
@@ -33,7 +35,8 @@ module Outpunch
33
35
  def server
34
36
  @server ||= Server.new(
35
37
  secret: configuration.secret,
36
- timeout: configuration.timeout
38
+ timeout: configuration.timeout,
39
+ client_idle_timeout: configuration.client_idle_timeout
37
40
  )
38
41
  end
39
42
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: outpunch-ruby-rack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.6
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TechyCorp
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-03 00:00:00.000000000 Z
11
+ date: 2026-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: websocket-driver
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - '='
32
32
  - !ruby/object:Gem::Version
33
- version: 0.5.6
33
+ version: 0.6.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '='
39
39
  - !ruby/object:Gem::Version
40
- version: 0.5.6
40
+ version: 0.6.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement