outpunch-ruby 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: f8527463f15aafce5145c2390cccd3630d87cd7e026387e30072c3a111dc508d
4
- data.tar.gz: bcdc48201b0c10de8776c07d3f2869ce2b58cea5e817d9f372f00d3f33946103
3
+ metadata.gz: 332688f75ddb314db8b8150c2674a76fbb07b9959b1deb1ba8ba16269c9f1403
4
+ data.tar.gz: 92f287279e205842eff0a4fd654163b627d4ff207a44b60b4a30ef8f6d9edc98
5
5
  SHA512:
6
- metadata.gz: e2b5936d60797c0ebad3d791e5e895747a36bc43b3bfc06d7c8114a371b56ebd3442a7955c98c26d98351439ba55eac6fce86a9dd2e7406ad3f5f4f059112549
7
- data.tar.gz: bc7e8fa44aad70f1245b488d4345830a126bee13e638b4e4ed4dc32d94f2ba75eda59d4fcd0aaa4593dbaf9d48764ed7323403d792fd24c44c4c9e15e1ffb883
6
+ metadata.gz: 91599daf7254233fcb56ade868f10875416773e7623596b08776aca8024e0fabd8141d038220ff982d0f4dd69f1772a95517d6a21deef753a3ab74810233cefe
7
+ data.tar.gz: 876f47ded186b38822af8958da7e9474ea32ee1df672182277627771d1957de3c2a4b4cd58128c068e37a055c23f0ae5791ecafcd450ea737d6dc657b472bbc6
data/Cargo.lock CHANGED
@@ -1016,7 +1016,7 @@ name = "outpunch"
1016
1016
  version = "0.1.0"
1017
1017
  dependencies = [
1018
1018
  "magnus",
1019
- "outpunch 0.5.6",
1019
+ "outpunch 0.6.0",
1020
1020
  "outpunch-client",
1021
1021
  "rb-sys",
1022
1022
  "rb-sys-build",
@@ -1025,9 +1025,9 @@ dependencies = [
1025
1025
 
1026
1026
  [[package]]
1027
1027
  name = "outpunch"
1028
- version = "0.5.6"
1028
+ version = "0.6.0"
1029
1029
  source = "registry+https://github.com/rust-lang/crates.io-index"
1030
- checksum = "f3410c591a4118576f00abbd453f14af474e84554aaeaaf818ef954b3321c690"
1030
+ checksum = "e9e89e94496161291dec4d1f4d5bb1dab73161d090035765d3d8ffbd329ae282"
1031
1031
  dependencies = [
1032
1032
  "serde",
1033
1033
  "serde_json",
@@ -1037,14 +1037,14 @@ dependencies = [
1037
1037
 
1038
1038
  [[package]]
1039
1039
  name = "outpunch-client"
1040
- version = "0.5.6"
1040
+ version = "0.6.0"
1041
1041
  source = "registry+https://github.com/rust-lang/crates.io-index"
1042
- checksum = "6a3c53774fbabcbc24507879207b19dcc4e3cd02c19265886847529c80a454e0"
1042
+ checksum = "ce74707432b95ff14e85c050182fd0cf34b514c2f0792dbaedd13602f4d4ff44"
1043
1043
  dependencies = [
1044
1044
  "base64",
1045
1045
  "clap",
1046
1046
  "futures-util",
1047
- "outpunch 0.5.6",
1047
+ "outpunch 0.6.0",
1048
1048
  "reqwest",
1049
1049
  "rustls",
1050
1050
  "rustls-platform-verifier",
@@ -8,11 +8,11 @@ name = "outpunch"
8
8
  crate-type = ["cdylib"]
9
9
 
10
10
  [dependencies]
11
- outpunch-client = { version = "0.5.6" }
11
+ outpunch-client = { version = "0.6.0" }
12
12
  magnus = { version = "0.8" }
13
13
  rb-sys = { version = "0.9", features = ["stable-api-compiled-fallback"] }
14
14
  tokio = { version = "1.50.0", features = ["full"] }
15
- outpunch_core = { version = "0.5.6", package = "outpunch" }
15
+ outpunch_core = { version = "0.6.0", package = "outpunch" }
16
16
 
17
17
  [build-dependencies]
18
18
  rb-sys-build = "0.9"
@@ -13,6 +13,8 @@ struct ClientConfigInner {
13
13
  forward_to: String,
14
14
  reconnect_delay: f64,
15
15
  request_timeout: f64,
16
+ ping_interval: f64,
17
+ idle_timeout: f64,
16
18
  }
17
19
 
18
20
  #[wrap(class = "Outpunch::ClientConfig", free_immediately, size)]
@@ -34,6 +36,8 @@ impl ClientConfig {
34
36
  forward_to: forward_to.unwrap_or_else(|| "http://localhost:8080".into()),
35
37
  reconnect_delay: reconnect_delay.unwrap_or(5.0),
36
38
  request_timeout: request_timeout.unwrap_or(25.0),
39
+ ping_interval: 30.0,
40
+ idle_timeout: 75.0,
37
41
  }))
38
42
  }
39
43
 
@@ -46,6 +50,8 @@ impl ClientConfig {
46
50
  forward_to: inner.forward_to.clone(),
47
51
  reconnect_delay: Duration::from_secs_f64(inner.reconnect_delay),
48
52
  request_timeout: Duration::from_secs_f64(inner.request_timeout),
53
+ ping_interval: Duration::from_secs_f64(inner.ping_interval),
54
+ idle_timeout: Duration::from_secs_f64(inner.idle_timeout),
49
55
  }
50
56
  }
51
57
 
@@ -89,6 +95,22 @@ impl ClientConfig {
89
95
  self.0.lock().unwrap().reconnect_delay = v;
90
96
  }
91
97
 
98
+ fn ping_interval(&self) -> f64 {
99
+ self.0.lock().unwrap().ping_interval
100
+ }
101
+
102
+ fn set_ping_interval(&self, v: f64) {
103
+ self.0.lock().unwrap().ping_interval = v;
104
+ }
105
+
106
+ fn idle_timeout(&self) -> f64 {
107
+ self.0.lock().unwrap().idle_timeout
108
+ }
109
+
110
+ fn set_idle_timeout(&self, v: f64) {
111
+ self.0.lock().unwrap().idle_timeout = v;
112
+ }
113
+
92
114
  fn request_timeout(&self) -> f64 {
93
115
  self.0.lock().unwrap().request_timeout
94
116
  }
@@ -152,6 +174,10 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
152
174
  "request_timeout=",
153
175
  method!(ClientConfig::set_request_timeout, 1),
154
176
  )?;
177
+ config_class.define_method("ping_interval", method!(ClientConfig::ping_interval, 0))?;
178
+ config_class.define_method("ping_interval=", method!(ClientConfig::set_ping_interval, 1))?;
179
+ config_class.define_method("idle_timeout", method!(ClientConfig::idle_timeout, 0))?;
180
+ config_class.define_method("idle_timeout=", method!(ClientConfig::set_idle_timeout, 1))?;
155
181
  config_class.define_method("inspect", method!(ClientConfig::inspect, 0))?;
156
182
  config_class.define_method("to_s", method!(ClientConfig::inspect, 0))?;
157
183
 
@@ -1,9 +1,10 @@
1
1
  use std::collections::HashMap;
2
2
  use std::panic;
3
- use std::sync::OnceLock;
4
- use std::time::Duration;
3
+ use std::sync::{Arc, OnceLock};
4
+ use std::time::{Duration, Instant};
5
5
 
6
- use magnus::{Error, ExceptionClass, RHash, RModule, RString, Ruby, prelude::*, wrap};
6
+ use magnus::scan_args::scan_args;
7
+ use magnus::{Error, ExceptionClass, RHash, RModule, RString, Ruby, Value, prelude::*, wrap};
7
8
  use outpunch_core::protocol::{IncomingRequest, TunnelResponse};
8
9
  use outpunch_core::server::{
9
10
  Connection as CoreConnection, OutpunchServer, ServerConfig, TunnelError,
@@ -66,27 +67,45 @@ pub struct Server {
66
67
  }
67
68
 
68
69
  impl Server {
69
- fn new(secret: String, timeout: f64, max_body_size: Option<usize>) -> Self {
70
+ /// `Server.new(secret, timeout, max_body_size, client_idle_timeout = nil)`; nil means the
71
+ /// default for that setting.
72
+ fn new(args: &[Value]) -> Result<Self, Error> {
73
+ let args = scan_args::<(String, f64, Option<usize>), (Option<f64>,), (), (), (), ()>(args)?;
74
+ let (secret, timeout, max_body_size) = args.required;
75
+ let (client_idle_timeout,) = args.optional;
70
76
  let defaults = ServerConfig::default();
71
- Self {
77
+ Ok(Self {
72
78
  inner: OutpunchServer::new(ServerConfig {
73
79
  secret,
74
80
  timeout: Duration::from_secs_f64(timeout),
75
81
  max_body_size: max_body_size.unwrap_or(defaults.max_body_size),
82
+ client_idle_timeout: client_idle_timeout
83
+ .map(Duration::from_secs_f64)
84
+ .unwrap_or(defaults.client_idle_timeout),
76
85
  ..defaults
77
86
  }),
78
- }
87
+ })
79
88
  }
80
89
 
81
90
  fn create_connection(&self) -> Connection {
82
91
  let inner = self.inner.create_connection();
83
92
  let (tx, rx) = mpsc::unbounded_channel();
93
+ let stall = Arc::new(StallCheck::new(self.inner.request_timeout()));
94
+ let weak = inner.downgrade();
95
+ let check = stall.clone();
84
96
  inner.on_message(move |msg| {
97
+ if check.enqueued()
98
+ && let Some(connection) = weak.upgrade()
99
+ {
100
+ connection.close();
101
+ }
85
102
  let _ = tx.send(msg);
86
103
  });
87
104
  Connection {
88
105
  inner,
106
+ server: self.inner.clone(),
89
107
  outgoing: Mutex::new(rx),
108
+ stall,
90
109
  }
91
110
  }
92
111
 
@@ -162,10 +181,56 @@ fn response_hash(ruby: &Ruby, response: TunnelResponse) -> Result<RHash, Error>
162
181
  Ok(hash)
163
182
  }
164
183
 
184
+ /// Detects a consumer that stopped calling `next_outgoing`: the message at the front of the
185
+ /// queue has waited longer than the request timeout. The binding does not own the socket, so
186
+ /// this stands in for the adapters' socket write timeout (F7 in spec/FINDINGS.md). Checked when
187
+ /// a message is queued, which is when a stalled consumer would make memory grow.
188
+ struct StallCheck {
189
+ limit: Duration,
190
+ state: std::sync::Mutex<StallState>,
191
+ }
192
+
193
+ struct StallState {
194
+ queued: usize,
195
+ head_since: Instant,
196
+ }
197
+
198
+ impl StallCheck {
199
+ fn new(limit: Duration) -> Self {
200
+ Self {
201
+ limit,
202
+ state: std::sync::Mutex::new(StallState {
203
+ queued: 0,
204
+ head_since: Instant::now(),
205
+ }),
206
+ }
207
+ }
208
+
209
+ /// Records a queued message; returns true if the consumer has stalled.
210
+ fn enqueued(&self) -> bool {
211
+ let mut state = self.state.lock().unwrap_or_else(|e| e.into_inner());
212
+ let stalled = state.queued > 0 && state.head_since.elapsed() > self.limit;
213
+ if state.queued == 0 {
214
+ state.head_since = Instant::now();
215
+ }
216
+ state.queued += 1;
217
+ stalled
218
+ }
219
+
220
+ /// Records a message taken off the queue; the next one starts waiting now.
221
+ fn taken(&self) {
222
+ let mut state = self.state.lock().unwrap_or_else(|e| e.into_inner());
223
+ state.queued = state.queued.saturating_sub(1);
224
+ state.head_since = Instant::now();
225
+ }
226
+ }
227
+
165
228
  #[wrap(class = "Outpunch::Connection", free_immediately, size)]
166
229
  pub struct Connection {
167
230
  inner: CoreConnection,
231
+ server: OutpunchServer,
168
232
  outgoing: Mutex<mpsc::UnboundedReceiver<String>>,
233
+ stall: Arc<StallCheck>,
169
234
  }
170
235
 
171
236
  impl Connection {
@@ -189,14 +254,28 @@ impl Connection {
189
254
  }
190
255
 
191
256
  /// Block until the core has a message for the tunnel client. Returns nil once the
192
- /// connection has ended.
257
+ /// connection has ended. Skips requests whose caller has already been answered.
193
258
  fn next_outgoing(ruby: &Ruby, rb_self: &Self) -> Result<Option<String>, Error> {
194
259
  loop {
195
260
  let result = without_gvl(ruby, |cancel| {
196
261
  runtime().block_on(async {
197
- let mut outgoing = rb_self.outgoing.lock().await;
262
+ // The lock is taken inside the select!, so an interrupt also stops a
263
+ // thread still waiting for it (F2 in spec/FINDINGS.md).
264
+ let next_pending = async {
265
+ let mut outgoing = rb_self.outgoing.lock().await;
266
+ loop {
267
+ let msg = outgoing.recv().await;
268
+ if msg.is_some() {
269
+ rb_self.stall.taken();
270
+ }
271
+ match msg {
272
+ Some(msg) if !rb_self.server.is_pending(&msg) => continue,
273
+ msg => break msg,
274
+ }
275
+ }
276
+ };
198
277
  tokio::select! {
199
- msg = outgoing.recv() => Some(msg),
278
+ msg = next_pending => Some(msg),
200
279
  _ = cancel.notified() => None,
201
280
  }
202
281
  })
@@ -211,6 +290,11 @@ impl Connection {
211
290
  self.inner.close();
212
291
  }
213
292
 
293
+ /// Record that the client is alive; call it for every WebSocket ping frame.
294
+ fn heartbeat(&self) {
295
+ self.inner.heartbeat();
296
+ }
297
+
214
298
  /// Run the connection lifecycle (auth, relay, cleanup). Blocks until the connection ends.
215
299
  /// An exception raised into this thread (Thread#raise, Thread#kill, Timeout) closes the
216
300
  /// connection and then propagates.
@@ -235,7 +319,7 @@ pub fn init(ruby: &Ruby, module: &RModule) -> Result<(), Error> {
235
319
  module.define_error("TunnelTimeoutError", timeout_error)?;
236
320
 
237
321
  let server = module.define_class("Server", ruby.class_object())?;
238
- server.define_singleton_method("new", magnus::function!(Server::new, 3))?;
322
+ server.define_singleton_method("new", magnus::function!(Server::new, -1))?;
239
323
  server.define_method(
240
324
  "create_connection",
241
325
  magnus::method!(Server::create_connection, 0),
@@ -250,6 +334,7 @@ pub fn init(ruby: &Ruby, module: &RModule) -> Result<(), Error> {
250
334
  magnus::method!(Connection::next_outgoing, 0),
251
335
  )?;
252
336
  connection.define_method("close", magnus::method!(Connection::close, 0))?;
337
+ connection.define_method("heartbeat", magnus::method!(Connection::heartbeat, 0))?;
253
338
  connection.define_method("run", magnus::method!(Connection::run, 0))?;
254
339
 
255
340
  Ok(())
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: outpunch-ruby
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: rb_sys