1 問題描述
在使用cargo進行復雜軟件安裝時, 依賴比較多的庫, 需要快速下載, 出現了該問題“Couldn't resolve host name (Could not resolve host: crates)”, 錯誤日志如下:
1 $ cargo install mdbook 2 Updating `git://mirrors.ustc.edu.cn/crates.io-index` index 3 Installing mdbook v0.4.4 4 Downloaded byteorder v1.3.4 (registry `git://mirrors.ustc.edu.cn/crates.io-index`) 5 Downloaded inotify-sys v0.1.4 (registry `git://mirrors.ustc.edu.cn/crates.io-index`) 6 warning: spurious network error (2 tries remaining): [6] Couldn't resolve host name (Could not resolve host: crates) 7 warning: spurious network error (2 tries remaining): [6] Couldn't resolve host name (Could not resolve host: crates) 8 warning: spurious network error (2 tries remaining): [6] Couldn't resolve host name (Could not resolve host: crates) 9 warning: spurious network error (2 tries remaining): [6] Couldn't resolve host name (Could not resolve host: crates) 10 warning: spurious network error (2 tries remaining): [6] Couldn't resolve host name (Could not resolve host: crates) 11 warning: spurious network error (2 tries remaining): [6] Couldn't resolve host name (Could not resolve host: crates) 12 warning: spurious network error (2 tries remaining): [6] Couldn't resolve host name (Could not resolve host: crates) 13 warning: spurious network error (2 tries remaining): [6] Couldn't resolve host name (Could not resolve host: crates) 14 warning: spurious network error (2 tries remaining): [6] Couldn't resolve host name (Could not resolve host: crates) 15 warning: spurious network error (2 tries remaining): [6] Couldn't resolve host name (Could not resolve host: crates) 16 warning: spurious network error (2 tries remaining): [6] Couldn't resolve host name (Could not resolve host: crates) 17 warning: spurious network error (2 tries remaining): [6] Couldn't resolve host name (Could not resolve host: crates) 18 warning: spurious network error (2 tries remaining): [6] Couldn't resolve host name (Could not resolve host: crates) 19 warning: spurious network error (2 tries remaining): [6] Couldn't resolve host name (Could not resolve host: crates) 20 Downloaded input_buffer v0.3.1 (registry `git://mirrors.ustc.edu.cn/crates.io-index`) 21 。。。// 跳過部分日志 22 error: failed to compile `mdbook v0.4.4`, intermediate artifacts can be found at `/tmp/cargo-installzrH2PU` 23 24 Caused by: 25 failed to download from `https://crates-io.proxy.ustclug.org/api/v1/crates/tokio-tungstenite/0.11.0/download` 26 27 Caused by: 28 [6] Couldn't resolve host name (Could not resolve host: crates)
看到該問題, 還特意檢查了網絡, 設置了dns,
在文件 /etc/resolv.conf 中增加了阿里的dns域名服務器, 問題沒有解決;
nameserver 223.5.5.5 nameserver 223.6.6.6
查找資料找到了問題根因和解決辦法;
2 解決辦法
臨時規避,禁止並行化下載安裝, 命令行輸入: CARGO_HTTP_MULTIPLEXING=false cargo install mdbook
$ CARGO_HTTP_MULTIPLEXING=false cargo install mdbook pdating `git://mirrors.ustc.edu.cn/crates.io-index` index Installing mdbook v0.4.4 Downloaded cfg-if v0.1.10 (registry `git://mirrors.ustc.edu.cn/crates.io-index`) Downloaded bytes v0.5.6 (registry `git://mirrors.ustc.edu.cn/crates.io-index`) Downloaded futures-io v0.3.8 (registry `git://mirrors.ustc.edu.cn/crates.io-index`) Downloaded http-body v0.3.1 (registry `git://mirrors.ustc.edu.cn/crates.io-index`) Downloaded markup5ever_rcdom v0.1.0 (registry `git://mirrors.ustc.edu.cn/crates.io-index`) Downloaded num-integer v0.1.44 (registry `git://mirrors.ustc.edu.cn/crates.io-index`) Downloaded phf_shared v0.8.0 (registry `git://mirrors.ustc.edu.cn/crates.io-index`) Downloaded rand_pcg v0.2.1 (registry `git://mirrors.ustc.edu.cn/crates.io-index`) Downloaded serde_urlencoded v0.6.1 (registry `git://mirrors.ustc.edu.cn/crates.io-index`) 。。。 Compiling markup5ever_rcdom v0.1.0 Compiling ammonia v3.1.0 Compiling hyper v0.13.9 Compiling warp v0.2.5 Compiling mdbook v0.4.4 Finished release [optimized] target(s) in 3m 01s Installing /home/neo/.cargo/bin/mdbook Installed package `mdbook v0.4.4` (executable `mdbook`)
編譯時間略長, 也還可以接受。。。
3 引申閱讀
問題根因分析:
詳見:https://github.com/ustclug/discussions/issues/294
主要原因如下:
knight42 commented on Apr 30 我找到這個問題的原因了,是因為我們的 nginx 會根據 client ip 對請求數作限制,默認是 4r/s 即每秒 4 個請求,如果超過的話就會返回 503。而我們的 nginx 配置里有這么一段 error_page 500 502 503 504 =302 $scheme://$proxy_host$uri; 即當上游返回 503 的時候,nginx 會讓 client 302 到 $scheme://$proxy_host$uri,而這個時候 nginx 還沒開始做 proxy,$proxy_host 應該還是空的,
所以 client 會看到 nginx 返回的 302 的響應里的 location 是 https:///crates/futures-io/futures-io-0.3.4.crate. 目前我先對 crates-io.proxy.ustclug.org 稍微放開了一下限制 limit_req_zone $binary_remote_addr zone=rust-crates:10m rate=20r/s; limit_req zone=rust-crates burst=150; 同時調整了一下配置,減少了一次 302 跳轉,@s977120 你現在可以試試看有沒有問題。 不過感覺這個只是臨時的 workaround,要徹底解決應該要讓 cargo 限制一下並發請求數。
最終解決方案, 要控制cargo下載時的並發數, 估計要看一下源碼,確認如何實現的。當前嘗試使用cargo install -j 4 時,問題依舊;
使用說明如下:
$ cargo install --help cargo-install Install a Rust binary. Default location is $HOME/.cargo/bin USAGE: cargo install [OPTIONS] [--] [crate]... OPTIONS: -q, --quiet No output printed to stdout --version <VERSION> Specify a version to install --git <URL> Git URL to install the specified crate from --branch <BRANCH> Branch to use when installing from git --tag <TAG> Tag to use when installing from git --rev <SHA> Specific commit to use when installing from git --path <PATH> Filesystem path to local crate to install --list list all installed packages and their versions -j, --jobs <N> Number of parallel jobs, defaults to # of CPUs -f, --force Force overwriting existing crates or binaries --no-track Do not save tracking information --features <FEATURES>... Space or comma separated list of features to activate --all-features Activate all available features --no-default-features Do not activate the `default` feature --profile <PROFILE-NAME> Install artifacts with the specified profile --debug Build in debug mode instead of release mode --bin <NAME>... Install only the specified binary --bins Install all binaries --example <NAME>... Install only the specified example --examples Install all examples --target <TRIPLE>... Build for the target triple --target-dir <DIRECTORY> Directory for all generated artifacts --root <DIR> Directory to install packages into --index <INDEX> Registry index to install from --registry <REGISTRY> Registry to use -v, --verbose Use verbose output (-vv very verbose/build.rs output) --color <WHEN> Coloring: auto, always, never --frozen Require Cargo.lock and cache are up to date --locked Require Cargo.lock is up to date --offline Run without accessing the network -Z <FLAG>... Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details -h, --help Prints help information ARGS: <crate>... Run `cargo help install` for more detailed information.