Skip to content

[210_20] 异步 HTTP API by libcpr#410

Merged
da-liii merged 6 commits intomainfrom
jinser/async-req
Feb 13, 2026
Merged

[210_20] 异步 HTTP API by libcpr#410
da-liii merged 6 commits intomainfrom
jinser/async-req

Conversation

@jetjinser
Copy link
Collaborator

@jetjinser jetjinser commented Feb 12, 2026

[210_20] 异步 HTTP API

注:该 API 由于 S7 的线程不安全,只能暴露一个 poll 来推进进度。在 QT 中应当将 poll 放在循环中定时调用,或自定义事件通知开始更新。

任务相关的代码文件

  • src/goldfish.hpp
  • goldfish/liii/http.scm
  • tests/goldfish/liii/http-test.scm
  • demo/async_http_demo.scm
  • demo/async_vs_sync_demo.scm

如何测试

# 构建
xmake build goldfish

# 运行测试
./bin/goldfish tests/goldfish/liii/http-test.scm

# 运行 demo
./bin/goldfish demo/async_http_demo.scm
./bin/goldfish demo/async_vs_sync_demo.scm

2026/2/13 实现 libcpr 异步 HTTP 绑定

What

  1. 新增 C++ 层异步 HTTP 绑定:

    • f_http_async_get / glue_http_async_get:异步 GET 请求
    • f_http_async_post / glue_http_async_post:异步 POST 请求
    • f_http_async_head / glue_http_async_head:异步 HEAD 请求
    • f_http_poll / glue_http_poll:非阻塞轮询完成的请求
    • f_http_wait_all / glue_http_wait_all:阻塞等待所有请求完成
  2. 新增 Scheme 层包装函数:

    • http-async-get(http-async-get url callback [params] [headers] [proxy])
    • http-async-post(http-async-post url callback [params] [data] [headers] [proxy])
    • http-async-head(http-async-head url callback [params] [headers] [proxy])
    • http-poll:检查并执行已完成的回调
    • http-wait-all(http-wait-all [timeout]) 等待所有请求完成
  3. 新增 demo 文件:

    • demo/async_http_demo.scm:展示基本用法、并发请求、轮询机制
    • demo/async_vs_sync_demo.scm:对比同步和异步请求的性能差异
  4. 新增测试用例:

    • 异步 GET/POST/HEAD 测试
    • 多个并发请求测试
    • http-poll 返回值测试

Why

提供真正的异步 HTTP 请求能力,使多个 HTTP 请求可以并发执行而不阻塞主线程。这对于需要同时请求多个资源的场景(如批量 API 调用、并行数据获取)可以显著提升性能。

How

  1. 使用 libcpr 原生异步 API:通过 session.GetAsync()session.PostAsync()session.HeadAsync() 启动异步请求,利用 libcpr 内部的全局线程池执行网络 I/O。

  2. Session 生命周期管理:使用 std::shared_ptr<cpr::Session> 保持 Session 对象存活,直到异步操作完成,避免 bad_weak_ptr 错误。

  3. 线程安全的回调机制

    • 后台线程只负责执行 HTTP 请求,不直接调用 Scheme 回调
    • 主线程通过 http-pollhttp-wait-all 检查完成的请求并执行回调
    • 使用 s7_gc_protect / s7_gc_unprotect_at 保护 Scheme 回调不被 GC
  4. 并发执行验证:3 个 delay/1 的请求总耗时约 2 秒(而非 6 秒),证明请求是并发执行的。

@jetjinser jetjinser changed the title libcpr async [210_20] 异步 HTTP API by libcpr Feb 13, 2026
@jetjinser jetjinser marked this pull request as ready for review February 13, 2026 02:35
(lambda (response)
(set! async-completed #t)
(set! async-response response)))
(http-wait-all 30)
Copy link
Contributor

Choose a reason for hiding this comment

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

30s

Copy link
Contributor

@da-liii da-liii left a comment

Choose a reason for hiding this comment

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

LGTM

@da-liii da-liii merged commit 4a3c2d8 into main Feb 13, 2026
4 checks passed
@da-liii da-liii deleted the jinser/async-req branch February 13, 2026 03:02
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.

2 participants