登录
首页 >  Golang >  Go问答

如何设置 idtoken.NewClient 的超时选项

来源:stackoverflow

时间:2024-04-07 10:39:30 264浏览 收藏

有志者,事竟成!如果你在学习Golang,那么本文《如何设置 idtoken.NewClient 的超时选项》,就很适合你!文章讲解的知识点主要包括,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

我正在使用 googles idtoken.newclient 从我的 gcp 获取一些信息。我需要设置超时,因为请求时间太长。我尝试这样设置

Timeout: time.Second * 10,
        Transport: http.DefaultTransport,
    }

    options := idtoken.WithHTTPClient(netClient)

    // client is a http.Client that automatically adds an "Authorization" header
    // to any requests made.
    client, err := idtoken.NewClient(ctx, targetURL, options)

但我收到以下错误: idtoken.newclient:transport/http:withhttpclient传递给newtransport

我不知道如何设置此选项


正确答案


当函数接受上下文时,它应该优雅地处理其取消。这意味着,您可以使用 WithTimeout 包装上下文,以便在给定的持续时间后它将被取消,例如:

timeout, cancel := context.WithTimeout(ctx, 10 * time.Second)
defer cancel()
client, err := idtoken.NewClient(timeout, targetURL, options))

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

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