outpunch-ruby-rack 0.5.5 → 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 +4 -4
- data/lib/outpunch/rack/connection.rb +46 -6
- data/lib/outpunch/rack/middleware.rb +5 -3
- data/lib/outpunch/rack/server.rb +7 -3
- data/lib/outpunch/rack/version.rb +1 -1
- data/lib/outpunch/rack.rb +5 -2
- metadata +6 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1bdda000cffcb7d436395e010a97a2786ef206efec06aef9ca60a420973cd30f
|
|
4
|
+
data.tar.gz: 2d15be8d2e14a016e364974a31f4a540ae7d8a87777049334c6196c963ee0ef8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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,20 +9,27 @@ 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
|
|
17
22
|
end
|
|
18
23
|
|
|
19
|
-
#
|
|
20
|
-
def run(env)
|
|
24
|
+
# Run the WebSocket loop on the hijacked socket. Blocks until disconnected.
|
|
25
|
+
def run(env, io)
|
|
21
26
|
@env = env
|
|
22
|
-
@io =
|
|
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
|
-
@
|
|
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
|
|
@@ -12,10 +12,12 @@ module Outpunch
|
|
|
12
12
|
|
|
13
13
|
def call(env)
|
|
14
14
|
if env["PATH_INFO"] == WS_PATH && websocket_upgrade?(env)
|
|
15
|
-
|
|
15
|
+
# Rack 3: a full hijack returns the client socket; rack.hijack_io is gone from the spec.
|
|
16
|
+
socket = env["rack.hijack"].call
|
|
16
17
|
conn = (@server || Outpunch::Rack.server).create_connection
|
|
17
|
-
Thread.new { conn.run(env) }
|
|
18
|
-
|
|
18
|
+
Thread.new { conn.run(env, socket) }
|
|
19
|
+
# The server ignores the response after a full hijack; Rack 3 still requires a valid one.
|
|
20
|
+
[200, {}, []]
|
|
19
21
|
else
|
|
20
22
|
@app.call(env)
|
|
21
23
|
end
|
data/lib/outpunch/rack/server.rb
CHANGED
|
@@ -10,14 +10,18 @@ module Outpunch
|
|
|
10
10
|
class Server
|
|
11
11
|
HOP_BY_HOP = %w[HOST CONNECTION UPGRADE].freeze
|
|
12
12
|
|
|
13
|
-
|
|
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
|
-
@
|
|
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)
|
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,
|
|
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.
|
|
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
|
+
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.
|
|
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.
|
|
40
|
+
version: 0.6.0
|
|
41
41
|
- !ruby/object:Gem::Dependency
|
|
42
42
|
name: rspec
|
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -72,14 +72,14 @@ dependencies:
|
|
|
72
72
|
requirements:
|
|
73
73
|
- - "~>"
|
|
74
74
|
- !ruby/object:Gem::Version
|
|
75
|
-
version: '
|
|
75
|
+
version: '8.0'
|
|
76
76
|
type: :development
|
|
77
77
|
prerelease: false
|
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
|
79
79
|
requirements:
|
|
80
80
|
- - "~>"
|
|
81
81
|
- !ruby/object:Gem::Version
|
|
82
|
-
version: '
|
|
82
|
+
version: '8.0'
|
|
83
83
|
- !ruby/object:Gem::Dependency
|
|
84
84
|
name: websocket-client-simple
|
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|