Skip to content

Make HTTP timeout configurable, raise default to 30s#52

Merged
jiegec merged 4 commits into
z4yx:masterfrom
WangYihang:claude/fix-goauthing-issue-26-W9mNM
May 8, 2026
Merged

Make HTTP timeout configurable, raise default to 30s#52
jiegec merged 4 commits into
z4yx:masterfrom
WangYihang:claude/fix-goauthing-issue-26-W9mNM

Conversation

@WangYihang
Copy link
Copy Markdown
Contributor

The previous 2-second hardcoded timeout in libauth was too short for slow auth-server responses (e.g. ~16s observed at FIT building, see issue #26). Expose libauth.HttpTimeout and add a --timeout / -t flag and "timeout" config-file field so users can tune it for their network.

https://claude.ai/code/session_01Az7YBQcoUBUSZCDQZ23kRc

The previous 2-second hardcoded timeout in libauth was too short for
slow auth-server responses (e.g. ~16s observed at FIT building, see
issue z4yx#26). Expose libauth.HttpTimeout and add a --timeout / -t flag
and "timeout" config-file field so users can tune it for their network.

https://claude.ai/code/session_01Az7YBQcoUBUSZCDQZ23kRc
Comment thread libauth/requests.go Outdated
Reduce the default HTTP timeout from 30 seconds to 2 seconds.
Comment thread cli/main.go Outdated
&cli.StringFlag{Name: "config-file", Aliases: []string{"c"}, Usage: "`path` to your config file, default ~/.auth-thu"},
&cli.StringFlag{Name: "hook-success", Usage: "command line to be executed in shell after successful login/out"},
&cli.IntFlag{Name: "online-interval", Aliases: []string{"I"}, Usage: "the interval between each keepAlive request (s)", Value: 3},
&cli.IntFlag{Name: "timeout", Aliases: []string{"t"}, Usage: "HTTP request timeout in seconds for the auth server", Value: 30},
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This default should be change to 2s as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

关于把超时默认值从 30s 调回 2s 这个改动,其实我个人感觉:

  • timeout 是上限,不是固定耗时。设成 30s 不代表请求要花 30s —— 网络好的时候照样几毫秒返回,对正常用户的体验没有任何影响。

  • 2s 在部分场景下确实会误伤。issue #26 里就有反馈说 FIT 楼里 auth server 响应能到 ~16s,2s 直接就 timeout 失败了。把上限抬高只是提高容错,并不会拖慢正常路径。

  • 对网络敏感、希望快速失败重试的用户可以自己显式调成 2s,但如果默认值面向的是"大多数用户在大多数网络下能正常用",这种场景下 30s 是更安全的兜底。

@jiegec

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不过已经在 960882b 里改回 2s。

WangYihang added 2 commits May 8, 2026 15:03
Reduced the HTTP request timeout for the auth server from 30 seconds to 2 seconds.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to make libauth’s HTTP request timeout configurable from the CLI/config file to better handle slow auth-server responses (per issue #26), rather than relying on a hardcoded short timeout.

Changes:

  • Introduces an exported libauth.HttpTimeout used by libauth HTTP clients.
  • Adds a timeout setting in the CLI config and a --timeout/-t CLI flag to set libauth.HttpTimeout.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
libauth/requests.go Replaces hardcoded HTTP client timeouts with a shared HttpTimeout variable.
cli/main.go Adds config/flag plumbing to set libauth.HttpTimeout from user-provided timeout seconds.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread libauth/requests.go
Comment on lines +19 to +21
// HttpTimeout is the timeout used for all HTTP requests issued by libauth.
// It can be overridden by the caller (e.g. the CLI) before making requests.
var HttpTimeout = 2 * time.Second
Comment thread cli/main.go
Comment on lines 433 to +436
&cli.StringFlag{Name: "config-file", Aliases: []string{"c"}, Usage: "`path` to your config file, default ~/.auth-thu"},
&cli.StringFlag{Name: "hook-success", Usage: "command line to be executed in shell after successful login/out"},
&cli.IntFlag{Name: "online-interval", Aliases: []string{"I"}, Usage: "the interval between each keepAlive request (s)", Value: 3},
&cli.IntFlag{Name: "timeout", Aliases: []string{"t"}, Usage: "HTTP request timeout in seconds for the auth server", Value: 2},
Comment thread cli/main.go
Comment on lines +103 to +108
merged.Timeout = c.Int("timeout")
if !c.IsSet("timeout") && settings.Timeout != 0 {
merged.Timeout = settings.Timeout
}
settings = merged
if settings.Timeout > 0 {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个感觉有点意义不明,命令行参数允许--timeout 0,但是配置文件的timeout: 0是无效的会被忽略,感觉这样才更迷惑了。

其实完全可以不改,explicit "--timeout 0" will be silently ignored本来也不算什么大问题

@Starrah
Copy link
Copy Markdown
Collaborator

Starrah commented May 8, 2026

都在这一个地方讨论吧,不然太分散了

#26 (comment)

诶,好像换成 auth4 也是一样的问题。

在未认证状态下,响应时间一直是 16s 多一点。

root@OpenWrt:~# time curl "https://auth4.tsinghua.edu.cn/cgi-bin/get_challenge?callback=C_a_l_l_b_a_c_k&double_stack=1&ip=&username=wyh-22" -vv
03:36:12.442281 [0-0] > GET /cgi-bin/get_challenge?callback=C_a_l_l_b_a_c_k&double_stack=1&ip=&username=wyh-22 HTTP/1.1
03:36:12.442281 [0-0] > Host: auth4.tsinghua.edu.cn
03:36:12.442281 [0-0] > User-Agent: curl/8.12.1
03:36:12.442281 [0-0] > Accept: /
03:36:12.442281 [0-0] >
03:36:12.445490 [0-0] < HTTP/1.1 200 OK
03:36:12.445739 [0-0] < Server: nginx
03:36:12.445767 [0-0] < Date: Fri, 08 May 2026 03:36:12 GMT
03:36:12.445785 [0-0] < Content-Type: application/json; charset=utf-8
03:36:12.445830 [0-0] < Content-Length: 276
03:36:12.445884 [0-0] < Connection: keep-alive
03:36:12.445899 [0-0] < Srun-Server: SRunCGIAuthIntfSvr V1.18 B20250403
03:36:12.445914 [0-0] < X-Frame-Options: SAMEORIGIN
03:36:12.445927 [0-0] < Set-Cookie: thuwebcookie-47873=ALAEAGGFFAAA; Path=/; HttpOnly
03:36:12.445942 [0-0] <
C_a_l_l_b_a_c_k({"challenge":"241f27378b2baa1e07158d39703921a70a83362431de77b8f509c692fb47feb1","client_ip":"101.6.42.94","ecode":0,"error":"ok","error_msg":"","expire":"60","online_ip":"101.6.42.94","res":"ok","srun_ver":"SRunCGIAuthIntfSvr V1.18 B20250403","st":1778211372})
real 0m 16.04s <---------------- 依然有 16s
user 0m 0.01s
sys 0m 0.00s

root@OpenWrt:~# time curl "https://auth4.tsinghua.edu.cn/cgi-bin/get_challenge?callback=C_a_l_l_b_a_c_k&double_stack=1&ip=&username=wyh-22" -vv
03:36:54.875569 [0-0] > GET /cgi-bin/get_challenge?callback=C_a_l_l_b_a_c_k&double_stack=1&ip=&username=wyh-22 HTTP/1.1
03:36:54.875569 [0-0] > Host: auth4.tsinghua.edu.cn
03:36:54.875569 [0-0] > User-Agent: curl/8.12.1
03:36:54.875569 [0-0] > Accept: /
03:36:54.875569 [0-0] >
03:36:54.878371 [0-0] < HTTP/1.1 200 OK
03:36:54.878606 [0-0] < Server: nginx
03:36:54.878753 [0-0] < Date: Fri, 08 May 2026 03:36:54 GMT
03:36:54.878785 [0-0] < Content-Type: application/json; charset=utf-8
03:36:54.878842 [0-0] < Content-Length: 276
03:36:54.878859 [0-0] < Connection: keep-alive
03:36:54.878873 [0-0] < Srun-Server: SRunCGIAuthIntfSvr V1.18 B20250403
03:36:54.878887 [0-0] < X-Frame-Options: SAMEORIGIN
03:36:54.878900 [0-0] < Set-Cookie: thuwebcookie-47873=ALAEAGGFFAAA; Path=/; HttpOnly
03:36:54.878915 [0-0] <
C_a_l_l_b_a_c_k({"challenge":"40eecbb202eccf96328005164593ad559400471b79e995e5d7d1be8bd64b8d7d","client_ip":"101.6.42.94","ecode":0,"error":"ok","error_msg":"","expire":"60","online_ip":"101.6.42.94","res":"ok","srun_ver":"SRunCGIAuthIntfSvr V1.18 B20250403","st":1778211414})
real 0m 16.03s <---------------- 依然有 16s
user 0m 0.00s
sys 0m 0.00s
手动在 userreg.tsinghua.edu.cn 准入代认证之后,再访问响应时间就马上下来了。

root@OpenWrt:~# time curl "https://auth4.tsinghua.edu.cn/cgi-bin/get_challenge?callback=C_a_l_l_b_a_c_k&double_stack=1&ip=&username=wyh-22" -vv
03:38:30.892269 [0-0] > GET /cgi-bin/get_challenge?callback=C_a_l_l_b_a_c_k&double_stack=1&ip=&username=wyh-22 HTTP/1.1
03:38:30.892269 [0-0] > Host: auth4.tsinghua.edu.cn
03:38:30.892269 [0-0] > User-Agent: curl/8.12.1
03:38:30.892269 [0-0] > Accept: /
03:38:30.892269 [0-0] >
03:38:30.894081 [0-0] < HTTP/1.1 200 OK
03:38:30.894289 [0-0] < Server: nginx
03:38:30.894307 [0-0] < Date: Fri, 08 May 2026 03:38:30 GMT
03:38:30.894321 [0-0] < Content-Type: application/json; charset=utf-8
03:38:30.894345 [0-0] < Content-Length: 276
03:38:30.894360 [0-0] < Connection: keep-alive
03:38:30.894373 [0-0] < Srun-Server: SRunCGIAuthIntfSvr V1.18 B20250403
03:38:30.894386 [0-0] < X-Frame-Options: SAMEORIGIN
03:38:30.894400 [0-0] < Set-Cookie: thuwebcookie-47873=ALAEAGGFFAAA; Path=/; HttpOnly
03:38:30.894414 [0-0] <
C_a_l_l_b_a_c_k({"challenge":"11e45aa6fd097b3f85b32ec70b9f61e57530b11ca642a78a2b9675fe3207135c","client_ip":"101.6.42.94","ecode":0,"error":"ok","error_msg":"","expire":"60","online_ip":"101.6.42.94","res":"ok","srun_ver":"SRunCGIAuthIntfSvr V1.18 B20250403","st":1778211510})real 0m 0.03s
user 0m 0.01s <---------------- 认证之后马上恢复正常。
sys 0m 0.00s

我感觉很奇怪。首先是16秒整,而且稳定,这就说明不是网络的随机波动导致的,而是触发了某种确定性的行为。
然后登录之后会瞬间返回,说明瞬间返回时包走了一个require authentication的路由?

总之我感觉这个现象是很奇怪的

Copy link
Copy Markdown
Collaborator

@Starrah Starrah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM,但是,默认值到底怎么取这个还可以再讨论。
我个人还是倾向维持2s的,不然就是breaking change了,因为像systemd脚本本来就是依靠认证失败可以快速返回来构建的,默认超时改到30s后,每次认证失败要卡30s才提示失败,那systemd的重启周期也被拖了30s之类。

但我更好奇的其实是到底发生了什么,为什么每次,稳定,卡刚好16s这个如此整的数;顺便,我尝试在我的环境(东配楼三层接入)复现,无法复现,我没过认证的时候auth4也是秒开的

@jiegec jiegec merged commit d38bc5b into z4yx:master May 8, 2026
6 checks passed
@WangYihang WangYihang deleted the claude/fix-goauthing-issue-26-W9mNM branch June 1, 2026 07:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants