登录
首页 >  Golang >  Go问答

(协程泄漏)http.TimeoutHandler 无法终止对应的 ServeHTTP 协程

来源:stackoverflow

时间:2024-03-14 23:21:29 198浏览 收藏

有志者,事竟成!如果你在学习Golang,那么本文《(协程泄漏)http.TimeoutHandler 无法终止对应的 ServeHTTP 协程》,就很适合你!文章讲解的知识点主要包括,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

超时处理程序将 servehttp 执行转移到新的 goroutine 上,但在计时器结束后无法终止该 goroutine。对于每个请求,它都会创建两个 goroutine,但 servehttp goroutine 永远不会用上下文杀死。

无法找到杀死 goroutine 的方法。

编辑带有 time.sleep 函数的 for 循环,代表着超出我们计时器范围的巨大计算。可以用任何其他函数替换它。

package main

import (
    "fmt"
    "io"
    "net/http"
    "runtime"
    "time"
)

type api struct{}

func (a api) ServeHTTP(w http.ResponseWriter, req *http.Request) {
    // For-loop block represents huge computation and usually takes more time
    // Can replace with any code
    i := 0
    for {
        if i == 500 {
            break
        }
        fmt.Printf("#goroutines: %d\n", runtime.NumGoroutine())
        time.Sleep(1 * time.Second)
        i++
    }
    _, _ = io.WriteString(w, "Hello World!")
}

func main() {
    var a api
    s := http.NewServeMux()
    s.Handle("/", a)
    h := http.TimeoutHandler(s, 1*time.Second, `Timeout`)

    fmt.Printf("#goroutines: %d\n", runtime.NumGoroutine())

    _ = http.ListenAndServe(":8080", h)
}

servehttp goroutine 应该与请求上下文一起终止,通常不会发生。


解决方案


我发现,如果你没有任何方法到达你的频道,那么当 goroutine 运行时就没有办法杀死或停止它。

在大型计算任务中,您必须在特定时间间隔或特定任务完成后观看通道。

使用 context.Context 指示 go 例程中止其功能。当然,go 例程必须侦听此类取消事件。

因此,对于您的代码,请执行以下操作:

ctx := req.Context() // this will be implicitly canceled by your TimeoutHandler after 1s

i := 0
for {
    if i == 500 {
        break
    }

    // for any long wait (1s etc.) always check the state of your context
    select {
    case <-time.After(1 * time.Second): // no cancelation, so keep going
    case <-ctx.Done():
        fmt.Println("request context has been canceled:", ctx.Err())
        return // terminates go-routine
    }
    i++
}

演示:https://play.golang.org/p/VEnW0vsItXm

注意: context 被设计为链接式 - 允许以级联方式取消多个级别的子任务。

在典型的 rest 调用中,我们会发起数据库请求。因此,为了确保此类阻塞和/或缓慢的调用及时完成,不应使用 Query,而应使用 QueryContext - 传入 http 请求的上下文作为第一个参数。

好了,本文到此结束,带大家了解了《(协程泄漏)http.TimeoutHandler 无法终止对应的 ServeHTTP 协程》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>