当前位置: > > > > 在 Echo/Go 上实现特定路线超时的最佳方法
来源:stackoverflow
2024-04-30 15:45:33
0浏览
收藏
在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天就整理分享《在 Echo/Go 上实现特定路线超时的最佳方法》,聊聊,希望可以帮助到正在努力赚钱的你。
问题内容
我想知道为 echo 库上的特定路线设置超时的最佳方法。
我用context.withtimeout实现了超时,但使用了几个函数来封装上下文,我认为这是错误的。
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
是否有任何中间件或更好的方法来实现这一点?
解决方案
向服务器 context.context 添加超时
在不确切知道您要做什么的情况下回答这个问题有点棘手。我将首先回答如何使用 withtimeout
和中间件来处理上下文包装。
中间件可以添加/修改请求上下文,如下所示:
func timeoutmiddleware(timeout time.duration, next func(w http.responsewriter, req *http.request)) func(w http.responsewriter, req *http.request) { return func(w http.responsewriter, req *http.request) { // wrap the existing context from the request ctx, cancel := context.withtimeout(req.context(), timeout) // always do this to clean up contexts, otherwise they'll hang out // and gather since they are blocked go rountines defer cancel() // put the new context into the request req = req.withcontext(ctx) // pass the modified request forward next(w, req) // handle any ctx timeout issues in the general middleware if err := ctx.err(); err != nil { if errors.is(err, context.deadlineexceeded) { log.println("http request timed out") w.write([]byte("timed out")) } } } }
问题是 next(w, req)
需要您的 http 处理程序来处理上下文超时。如果 http 处理程序忽略上下文,则不会超时。比如这样:
func endless(w http.responsewriter, req *http.request) { ctx := req.context() // just a busy loop.... for { select { case <-ctx.done(): // exit the handler if the context is done(), even if our function is not. return default: fmt.println("wait") time.sleep(1 * time.second) } } }
如果您正在进行数据库调用或需要在处理程序中花费时间的操作,通常数据库 api 会接受 context.context
以便在上下文被取消时提前中止。
因此,此解决方案为进入处理程序的请求上下文添加了超时,您仍然需要管理该处理程序。
客户端超时
您始终可以将超时添加到您的请求中:
client := http.Client{ Timeout: time.Second, } client.Do(...)
今天关于《在 Echo/Go 上实现特定路线超时的最佳方法》的内容介绍就到此结束,如果有什么疑问或者建议,可以在公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!