登录
首页 >  Golang >  Go问答

可以无限期询问 time.After() 吗?

来源:stackoverflow

时间:2024-03-19 16:12:32 154浏览 收藏

time.After() 函数用于设置一个超时时间,当时间到了就会返回一个 channel。文章探讨了是否可以设置一个无限长的超时时间。作者指出,time.After() 函数没有提供“永远”的持续时间,但有最大持续时间,约为 292 年。作者建议使用其他解决方案,例如省略 time.After() 并使用简单的接收,或者将超时通道创建移到 select 语句之外并进行相应的初始化。文章还提到了使用 context.Context 来处理超时,这在 Go 代码中非常惯用。

问题内容

有没有办法向 time.after() 询问无限长的时间?

动机:我有一个服务,调用者可以向其请求消息,并且可以选择超时。执行此操作的明显方法是:

func service(timeout *time.Duration) SomeType {
    var timeout_value time.Duration
    if timeout != nil {
        timeout_value = *timeout
    } else {
        timeout_value = time.Forever /* or something */
    }

    select {
    case value <- some_channel:
        return value
    case <- time.After(timeout_value):
        return nil
    }
}

除了我不知道是否有办法说 time.forever


正确答案


没有“永远”的持续时间,但有最大持续时间:

const maxduration time.duration = 1<<63 - 1

maxduration 约为 292 年。对于单个应用程序的生命周期来说应该足够了。但我建议使用以下不使用它的解决方案:

请注意,如果“永远”是预期的最大等待时间,则省略 time.after() 并使用简单的接收会更简单、更高效:

func service(timeout *time.duration) sometype {
    if timeout == nil {
        return <-some_channel
    }

    select {
    case value := <-some_channel:
        return value
    case <-time.after(*timeout):
        return nil
    }
}

您表示您的实际代码要复杂得多,并且包含更多案例。

在这种情况下,我会将超时通道创建移到 select 语句之外,并进行相应的初始化。当 timeoutnil 时,只需离开通道 nil (其零值),它永远不会传递任何值,因此从 nil 通道接收字面上需要“永远”:

func service(timeout *time.duration) sometype {
    var timeoutch <-chan time.time
    if timeout != nil {
        timeoutch = time.after(*timeout)
    }

    select {
    case value := <-some_channel:
        return value
    case <-timeoutch:
        return nil
    }
}

您可以在函数中接受 context.Context,而不是持续时间,我认为这在 go 代码中非常惯用。

然后调用者可以根据需要使用 Context.BackgroundContext.WithTimeout 来调用该函数。 service 函数选择上下文的 done(),在后台上下文永远不会结束的情况下(chan 实际上是 nil)。

func callerNoTimeout() {
    foo := service(context.Background())
}

func callerTimeout() {
    foo := service(context.WithTimeout(context.Background(), timeOut))
}

func service(ctx context.Context) SomeType {
    select {
        case value <-some_channel:
            return value
        case <-ctx.Done():
            return nil
    }
}

今天关于《可以无限期询问 time.After() 吗?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

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