为了账号安全,请及时绑定邮箱和手机立即绑定

Go - 实现超时的最佳方式

Go - 实现超时的最佳方式

Go
DIEA 2022-10-10 19:55:28
我有一个异步部署的服务,我需要等待指定的时间才能使其上线。如果指定的时间过去了,我们仍然无法找到服务,那么我们就会出错。在 go 中写这个的最佳方式是什么?我正在考虑使用context.WithTimeout但不确定这将如何工作。谢谢您的帮助!func (c *Client) WaitForServiceToComeAlive(ctx context.Context, name string, timeout time.Duration) error {        var mysvc *Service    var err error    endtime := time.Now().Add(timeout)    for time.Now().Before(endtime) {        mysvc, err = c.GetService(ctx, name)        if err != nil {            return err        }        if mysvc != nil {            break        }        time.Sleep(time.Second * 10)    }    if mysvc == nil {        return fmt.Errorf("svc %s did not register", name)    }    return nil}
查看完整描述

1 回答

?
温温酱

TA贡献1752条经验 获得超4个赞

永远不要使用time.Sleep,特别是如果它是一个很长的时期 - 因为它是不间断的。为什么这很重要?如果它在一个 goroutine 中并且该任务不会完成(即上下文被取消),那么您宁愿立即中止。


因此,要创建一个带有取消的轮询等待:


select {

case <-ctx.Done():               // cancel early if context is canceled

    return ctx.Err()

case <-time.After(pollInterval): // wait for pollInterval duration

}

将较大的超时时间放在输入上下文中:


ctx := context.TODO() // <- outer request context goes here or context.Background()


// wrap context with a timeout

ctx, cancel := context.WithTimeout(ctx, 1 * time.Minute)

defer cancel() // avoid leaks


err := c.WaitForServiceToComeAlive(ctx, "job", 10*time.Second /* poll interval */)

然后您的服务等待功能简化为:


func (c *Client) WaitForServiceToComeAlive(ctx context.Context, name string, pollInterval time.Duration) error {


    var mysvc *Service

    var err error


    for {

        mysvc, err = c.GetService(name) // <- this should take a ctx too - if possible for early cancelation

        if err != nil {

            return err

        }

        if mysvc != nil {

            return nil

        }


        select {

        case <-ctx.Done():

            return ctx.Err()

        case <-time.After(pollInterval):

        }

    }


}

https://play.golang.org/p/JwH5CMyY0I2


查看完整回答
反对 回复 2022-10-10
  • 1 回答
  • 0 关注
  • 54 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信