Repository navigation
fix(account): reuse one console connection instead of a handshake per request #248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
79decba
2748ec9
568be8a
0526e32
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,137 @@ | ||||||
| // The link-line filter `build.rs` applies to the kit's CMake `commandFragments`. | ||||||
| // | ||||||
| // A separate file with no dependencies, so `build.rs` and | ||||||
| // `tests/test_wally_build_link_args.rs` can both compile it: a build script's own | ||||||
| // `#[cfg(test)]` module is never run by `cargo test`. | ||||||
|
|
||||||
| /// Drop the arguments a compiler driver needs and a linker does not, keeping | ||||||
| /// the ones whose value is a separate argument out of the same list. | ||||||
| /// | ||||||
| /// `split_fragment` gives "-isystem /dir" back as two entries, and a bare | ||||||
| /// directory is a link input: dropping the flag and keeping the path is what | ||||||
| /// turned a `CXXFLAGS` of `-nostdinc++ -isystem ${SDKROOT}/usr/include/c++/v1 | ||||||
| /// --sysroot=${SDKROOT}` into `ld: file cannot be mmap()ed ... usr/include/c++/v1`. | ||||||
| /// The same shape hides `-I`, `-F`, `-L`'s compile-only siblings and the | ||||||
| /// `--sysroot=<dir>` spelling, which takes its value inside the argument and | ||||||
| /// so leaked on its own. | ||||||
| pub fn link_args_from(chunk: Vec<String>) -> Vec<String> { | ||||||
| let mut kept = Vec::with_capacity(chunk.len()); | ||||||
| let mut rest = chunk.into_iter(); | ||||||
| while let Some(arg) = rest.next() { | ||||||
| if TAKES_SEPARATE_OPERAND.contains(&arg.as_str()) { | ||||||
| // Drop the flag and the argument it eats, whatever it looks like. | ||||||
| rest.next(); | ||||||
| continue; | ||||||
| } | ||||||
| if is_compile_only(&arg) { | ||||||
| continue; | ||||||
| } | ||||||
| kept.push(arg); | ||||||
| } | ||||||
| kept | ||||||
| } | ||||||
|
|
||||||
| /// Compiler flags whose value arrives as the next argument. A pathlib entry | ||||||
| /// here never reaches the linker, and neither does its operand. | ||||||
| const TAKES_SEPARATE_OPERAND: [&str; 6] = [ | ||||||
| "-isystem", | ||||||
| "-I", | ||||||
| "-F", | ||||||
| "-iframework", | ||||||
|
Comment on lines
+39
to
+40
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep
Remove Operand table change in build_link_args.rs
In Check
|
||||||
| "-iquote", | ||||||
| "-idirafter", | ||||||
| ]; | ||||||
|
|
||||||
| /// GCC-style drivers get CMAKE_CXX_FLAGS on the link line too ("-O3 -DNDEBUG"); | ||||||
| /// harmless to a driver, fatal once passed straight to ld (the Swift host's | ||||||
| /// -Xlinker path). Keep only what linking needs. | ||||||
| pub fn is_compile_only(arg: &str) -> bool { | ||||||
| if arg.starts_with("-Wl,") { | ||||||
| return false; | ||||||
| } | ||||||
| if let Some(msvc) = arg.strip_prefix('/') { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Any absolute path starting with Prompt for AI agents
Suggested change
|
||||||
| return is_msvc_compile_only(msvc); | ||||||
| } | ||||||
| if matches!(arg, "-MD" | "-MDd" | "-MT" | "-MTd") { | ||||||
| return true; | ||||||
| } | ||||||
| [ | ||||||
| "-D", | ||||||
| "-U", | ||||||
| "-I", | ||||||
| "-O", | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: The Prompt for AI agents |
||||||
| "-g", | ||||||
| "-W", | ||||||
| "-std=", | ||||||
| "-isystem", | ||||||
| "-pedantic", | ||||||
| // A compile-only sysroot is spelled with the value inside the | ||||||
| // argument, so it has no separate operand to drop with it. | ||||||
| "--sysroot", | ||||||
|
Comment on lines
+68
to
+70
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the directory operand of a separate
Add Suggested fix
Add
|
||||||
| "-nostdinc", | ||||||
| ] | ||||||
| .iter() | ||||||
| .any(|p| arg.starts_with(p)) | ||||||
| } | ||||||
|
|
||||||
| /// MSVC spells CMAKE_CXX_FLAGS with `/` (`/DWIN32 /EHsc /O2 /Ob2 /DNDEBUG`), and | ||||||
| /// `link.exe` warns LNK4044 on each one it is handed. `/D` is a define unless it | ||||||
| /// is one of the linker's own `/D…` options. | ||||||
| pub fn is_msvc_compile_only(opt: &str) -> bool { | ||||||
| const LINKER_D: [&str; 11] = [ | ||||||
| "DEBUG", | ||||||
| "DEBUGTYPE", | ||||||
| "DEF", | ||||||
| "DEFAULTLIB", | ||||||
| "DELAY", | ||||||
| "DELAYLOAD", | ||||||
| "DELAYSIGN", | ||||||
| "DEPENDENTLOADFLAG", | ||||||
| "DLL", | ||||||
| "DRIVER", | ||||||
| "DYNAMICBASE", | ||||||
| ]; | ||||||
| if let Some(define) = opt.strip_prefix('D') { | ||||||
| let name = define.split([':', '=']).next().unwrap_or(define); | ||||||
| let head = format!("D{name}").to_ascii_uppercase(); | ||||||
| return !LINKER_D.iter().any(|l| head == *l); | ||||||
| } | ||||||
| matches!( | ||||||
| opt, | ||||||
| "GR" | "GR-" | "MD" | "MDd" | "MT" | "MTd" | "Zi" | "Z7" | "utf-8" | "bigobj" | "MP" | ||||||
| ) || opt.starts_with("EH") | ||||||
| || opt.starts_with("std:") | ||||||
| || (opt.starts_with('O') && opt.len() <= 3 && !opt.contains(':')) | ||||||
| || (opt.len() == 2 && opt.starts_with('W') && opt.as_bytes()[1].is_ascii_digit()) | ||||||
| } | ||||||
|
|
||||||
| /// A file API fragment can hold several arguments ("-framework IOKit", | ||||||
| /// "kernel32.lib user32.lib"); CMake double-quotes an argument with spaces. | ||||||
| pub fn split_fragment(text: &str) -> Vec<String> { | ||||||
| let mut out = Vec::new(); | ||||||
| let mut current = String::new(); | ||||||
| let mut quoted = false; | ||||||
| let mut any = false; | ||||||
| for c in text.chars() { | ||||||
| match c { | ||||||
| '"' => { | ||||||
| quoted = !quoted; | ||||||
| any = true; | ||||||
| } | ||||||
| c if c.is_whitespace() && !quoted => { | ||||||
| if any { | ||||||
| out.push(std::mem::take(&mut current)); | ||||||
| any = false; | ||||||
| } | ||||||
| } | ||||||
| c => { | ||||||
| current.push(c); | ||||||
| any = true; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| if any { | ||||||
| out.push(current); | ||||||
| } | ||||||
| out | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,7 +7,7 @@ | |||||||||
| //! WinHTTP used), env proxies with loopback bypass. Tests inject a `Transport`. | ||||||||||
|
|
||||||||||
| use std::collections::BTreeMap; | ||||||||||
| use std::sync::Arc; | ||||||||||
| use std::sync::{Arc, Mutex}; | ||||||||||
| use std::time::{Duration, Instant}; | ||||||||||
|
|
||||||||||
| use super::console_contract as contract; | ||||||||||
|
|
@@ -414,6 +414,13 @@ const CONNECT_TIMEOUT_MS: i32 = 10_000; | |||||||||
| const TOTAL_TIMEOUT_MS: i32 = 30_000; | ||||||||||
| const MAX_RESPONSE_BYTES: u64 = 1024 * 1024; | ||||||||||
|
|
||||||||||
| // How long an idle console connection stays pooled. ureq's default is 15s, | ||||||||||
| // which a person waits out while approving a sign-in in the browser and while | ||||||||||
| // reading a usage listing, so each poll or page after a pause would otherwise | ||||||||||
| // pay a fresh handshake. The pool still bounds itself (10 idle connections, | ||||||||||
| // 3 per host) and the key includes the proxy, so this only lengthens reuse. | ||||||||||
| const IDLE_TIMEOUT: Duration = Duration::from_secs(120); | ||||||||||
|
|
||||||||||
| // Proxy discovery (WPAD/PAC auto-detection on Windows, see | ||||||||||
| // windows_autodetected_system_proxy) runs before a request's own timeout | ||||||||||
| // starts and is bounded to 3s of its own. Once discovery has spent any of | ||||||||||
|
|
@@ -887,9 +894,61 @@ fn map_transport_error(error: ureq::Error) -> String { | |||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /// The proxy's own key for equality: two requests that resolve to the same | ||||||||||
| /// proxy can share an agent, and two that do not cannot. | ||||||||||
| fn proxy_cache_key(proxy: &Option<ureq::Proxy>) -> String { | ||||||||||
| match proxy { | ||||||||||
| None => "direct".to_string(), | ||||||||||
| Some(proxy) => format!("{proxy:?}"), | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Key the agent cache by the proxy value, not its
The defect needs one process to see two credential sets with the same timeouts. This can happen only if the environment changes in-process, for example through Key the cache by the proxy value. Suggested fix
Add
|
||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /// One agent per (proxy, connect timeout, total timeout). | ||||||||||
| /// | ||||||||||
| /// Cloning a ureq Agent shares its connection pool, which is what makes this | ||||||||||
| /// reuse real. The entry is created on first use for that key and then lives | ||||||||||
| /// for the process, because an agent holding a pooled connection cannot be | ||||||||||
| /// dropped while a request may still borrow it. | ||||||||||
|
Comment on lines
+910
to
+911
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: The stated reason for the process-lifetime cache is wrong. The map exists so the shared pool survives between calls; dropping the last clone would discard the idle connections. Reword the comment to say that. Prompt for AI agents
Suggested change
|
||||||||||
| fn console_agent(proxy: Option<ureq::Proxy>, connect_ms: i32, total_ms: i32) -> ureq::Agent { | ||||||||||
| static AGENTS: Mutex<Option<BTreeMap<String, ureq::Agent>>> = Mutex::new(None); | ||||||||||
| let key = format!( | ||||||||||
| "{}|{}|{}", | ||||||||||
| proxy_cache_key(&proxy), | ||||||||||
| connect_ms.max(0), | ||||||||||
| total_ms.max(0) | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: The key includes the post-discovery remainder, so a one-millisecond difference creates a separate permanent agent and pool. Key pool identity on stable timeout inputs while preserving each request's remaining budget separately. Prompt for AI agents |
||||||||||
| ); | ||||||||||
| let mut agents = AGENTS | ||||||||||
| .lock() | ||||||||||
| .unwrap_or_else(|poisoned| poisoned.into_inner()); | ||||||||||
| let agents = agents.get_or_insert_with(BTreeMap::new); | ||||||||||
| if let Some(agent) = agents.get(&key) { | ||||||||||
| return agent.clone(); | ||||||||||
| } | ||||||||||
| let config = ureq::Agent::config_builder() | ||||||||||
| .http_status_as_error(false) | ||||||||||
| .max_redirects(0) | ||||||||||
| .tls_config(console_tls_config()) | ||||||||||
| .proxy(proxy) | ||||||||||
| .user_agent("wally-cloud-auth/1") | ||||||||||
| .max_idle_age(IDLE_TIMEOUT) | ||||||||||
| .timeout_connect(Some(Duration::from_millis(connect_ms.max(0) as u64))) | ||||||||||
| .timeout_global(Some(Duration::from_millis(total_ms.max(0) as u64))) | ||||||||||
| .build(); | ||||||||||
| let agent = ureq::Agent::new_with_config(config); | ||||||||||
| agents.insert(key, agent.clone()); | ||||||||||
| agent | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /// The real network transport: ureq with native-tls (the OS trust store, as | ||||||||||
| /// curl and WinHTTP used), env proxies with a loopback bypass, redirects never | ||||||||||
| /// followed, TLS verification on (the connector's default). | ||||||||||
| /// | ||||||||||
| /// One agent is kept per proxy and per timeout pair, so consecutive requests to | ||||||||||
| /// one console reuse a pooled connection instead of paying a TCP+TLS handshake | ||||||||||
| /// each time; a key miss builds a new one. Requests differ only in their | ||||||||||
| /// per-request budget (and, on Windows, the proxy discovered for the URL), so | ||||||||||
| /// the pair is the whole key -- sharing across differing budgets would let one | ||||||||||
| /// caller's shorter timeout shorten another's connection lifetime. | ||||||||||
| fn real_transport(request: &HttpRequest) -> Result<HttpResponse, HttpFailure> { | ||||||||||
| if !super::browser_url_is_safe(&request.url) { | ||||||||||
| return Err("refusing an unsafe console request URL".to_string().into()); | ||||||||||
|
|
@@ -912,17 +971,6 @@ fn real_transport(request: &HttpRequest) -> Result<HttpResponse, HttpFailure> { | |||||||||
| let total = remaining_after_discovery_ms(total, discovery_start.elapsed()); | ||||||||||
| let connect = connect_timeout_ms(total); | ||||||||||
|
|
||||||||||
| let config = ureq::Agent::config_builder() | ||||||||||
| .http_status_as_error(false) | ||||||||||
| .max_redirects(0) | ||||||||||
| .tls_config(console_tls_config()) | ||||||||||
| .proxy(proxy) | ||||||||||
| .user_agent("wally-cloud-auth/1") | ||||||||||
| .timeout_connect(Some(Duration::from_millis(connect.max(0) as u64))) | ||||||||||
| .timeout_global(Some(Duration::from_millis(total.max(0) as u64))) | ||||||||||
| .build(); | ||||||||||
| let agent = ureq::Agent::new_with_config(config); | ||||||||||
|
|
||||||||||
| let mut builder = ureq::http::Request::builder() | ||||||||||
| .method(request.method.as_str()) | ||||||||||
| .uri(&request.url) | ||||||||||
|
|
@@ -935,6 +983,7 @@ fn real_transport(request: &HttpRequest) -> Result<HttpResponse, HttpFailure> { | |||||||||
| .body(request.body.clone()) | ||||||||||
| .map_err(|error| format!("could not build the console request: {error}"))?; | ||||||||||
|
|
||||||||||
| let agent = console_agent(proxy, connect, total); | ||||||||||
| let mut response = agent.run(http_request).map_err(map_transport_error)?; | ||||||||||
|
|
||||||||||
| let status = response.status().as_u16() as i32; | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2:
-Fis also a linker framework search path, not only a compile flag. Moving it intoTAKES_SEPARATE_OPERANDdrops-F <dir>from the link line, where it was previously kept. Remove"-F"fromTAKES_SEPARATE_OPERANDinbuild_link_args.rsunless the kit's link closure is confirmed not to need it.Prompt for AI agents