如何在特定请求中感知客户端已断开连接?
Describe the Question
大部分场景中,我不需要关注客户端发起请求后是否断开了连接,且我需要客户的连接在后台继续执行直到完成 但有特定的场景(sse,websocket连接) 我需要比较灵敏检测客户端是否已经关闭连接,以此释放连接使用的资源
Expected behavior
提供类似ctx.Done的方式提示我连接已被客户端关闭
Hertz version:
v0.9.5
Environment:
mac goalng 1.21.1
Additional context
我已知有一个issue可以配置全局的ctx感知连接断开,但这不符合我的精确需求 https://github.com/cloudwego/hertz/pull/1054
并没有提供这种粒度的控制。建议通过白名单endpoint方式,对非白名单内的 ctx 调用 context.WithoutCancel
Please assign me
@ZUOLI11111111 Thanks, I assign to you, could you please give your solution first ;)
我是个小白 多见谅。可能做的不太好
我自己写完的代码写了个main函数测试
我在控制台输入这些
curl http://127.0.0.1:8888/api/normal curl http://127.0.0.1:8888/api/withoutcancel curl http://127.0.0.1:8888/api/stream curl http://127.0.0.1:8888/api/longprocess curl http://127.0.0.1:8888/api/normal & sleep 5 && kill $! wget http://127.0.0.1:8888/api/normal -O - wget http://127.0.0.1:8888/api/withoutcancel -O - wget http://127.0.0.1:8888/api/stream -O - wget http://127.0.0.1:8888/api/longprocess -O -
然后结果是这样的
我理解的对吗