登录
首页 >  Golang >  Go问答

考查函数超时和 goroutine溢出问题

来源:stackoverflow

时间:2024-02-11 16:36:24 170浏览 收藏

golang学习网今天将给大家带来《考查函数超时和 goroutine溢出问题》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到等等知识点,如果你是正在学习Golang或者已经是大佬级别了,都非常欢迎也希望大家都能给我建议评论哈~希望能帮助到大家!

问题内容

我希望对名为 foo 的函数设置超时。考虑以下因素

func fooWithTimeout(d time.Duration) error {
    ch := make(chan error, 1)
    go func() {
        ch <- foo()
    }()

    select {
    case err := <-ch:
        return err
    case <-time.After(d):
        return errors.New("foo has timed out")
    }
}
  • 如果 foo 超时,那么 foo 是否能够写入通道 ch 或者是否存在 goroutine 阻塞或恐慌的风险?
  • 一旦 foowithtimeout 退出,频道 ch 会发生什么?
  • 此代码是否存在潜在问题?
  • 我应该在调用 foo 之前在 go func(){...}() 中添加 defer close(ch) 吗?
  • 在此示例中我使用缓冲通道(大小为 1)还是无缓冲通道有关系吗?

正确答案


计时器计时后,foowithtimeout 将返回。 goroutine 将继续运行,直到 foo 返回。

如果foo超时,它会写入通道ch,因为它已被缓冲。

如果 foo 返回,通道 ch 最终将被垃圾回收。

您不需要关闭频道。一旦超出范围,就会被垃圾回收。

大量调用 foowithtimeout 将创建大量资源。每次调用都会创建两个 goroutine。超时的正确方法是将 foo 更改为使用上下文。

基于 https://stackoverflow.com/a/73611534/1079543 构建,这里是带有上下文的 foo

package main

import (
    "context"
    "fmt"
    "log"
    "time"
)

func foo(ctx context.Context) (string, error) {
    ch := make(chan string, 1)

    go func() {
        fmt.Println("Sleeping...")
        time.Sleep(time.Second * 1)
        fmt.Println("Wake up...")
        ch <- "foo"
    }()

    select {
    case <-ctx.Done():
        return "", fmt.Errorf("context cancelled: %w", ctx.Err())
    case result := <-ch:
        return result, nil
    }
}

func main() {
    ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
    defer cancel()

    res, err := foo(ctx)
    if err != nil {
        log.Fatalf("foo failed: %v", err)
    }

    log.Printf("res: %s", res)
}

今天关于《考查函数超时和 goroutine溢出问题》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

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