猿问

如何为 idtoken 设置超时选项。新客户端

我正在使用谷歌从我的gcp获取一些信息。我需要设置超时,因为请求花费的时间太长。我试图像这样设置它idtoken.NewClient


        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 passed to NewTransport


我不知道如何设置此选项


一只甜甜圈
浏览 122回答 1
1回答

海绵宝宝撒

当函数接受上下文时,它应该优雅地处理其取消。这意味着,您可以使用 WithTimeout 包装上下文,以便在给定的持续时间后取消它,例如:timeout, cancel := context.WithTimeout(ctx, 10 * time.Second)defer cancel()client, err := idtoken.NewClient(timeout, targetURL, options))
随时随地看视频慕课网APP

相关分类

Go
我要回答