2017-04-11 101 views
1

我们的应用程序在访问Big Query时有时会看到DEADLINE_EXCEEDED。DEAST_EXCEEDED在Go Bigquery

import (
    "cloud.google.com/go/bigquery" 
    "golang.org/x/net/context" 
    "google.golang.org/api/option" 
) 

func MyFunc(ctx context.Context) { 
      : 
    client, err := bigquery.NewClient(ctx, PROJECT_ID, option.WithServiceAccountFile(SERVICE_ACCOUNT_JSON_FILE_PATH)) 
    query := client.Query("SELECT * FROM ....") 
    it, err := query.Read(ctx) 
    var list []MyStruct 
    for { 
     var m MyStruct 
     err := it.Next(&m) 
     if err == iterator.Done { 
      break 
     } 
     if err != nil { 
      <Error Handling> 
     } 
     list = append(list, m) 
    } 
      : 
} 

有时我们会看到这个错误。

Get https://www.googleapis.com/bigquery/v2/projects/myproject/queries/job_zandIeLwH0s8f3FAQ_ORC0zau14?alt=json\u0026startIndex=0\u0026timeoutMs=60000: API error 5 (urlfetch: DEADLINE_EXCEEDED): ('The read operation timed out',)" 

它看起来超时是5秒,但我可以'找到如何更改超时秒数。我写了this post,我修改了我的源代码如下。

ctx_with_deadline, _ := context.WithTimeout(ctx, 1*time.Minute) 
httpClient := &http.Client{ 
    Transport: &oauth2.Transport{ 
     Base: &urlfetch.Transport{Context: ctx_with_deadline}, 
    }, 
} 

client, err := bigquery.NewClient(ctx, PROJECT_ID, option.WithServiceAccountFile(SERVICE_ACCOUNT_JSON_FILE_PATH), option.WithHTTPClient(httpClient)) 

然后我遇到了这个错误。

Post https://www.googleapis.com/bigquery/v2/projects/myproject/jobs?alt=json: oauth2: Transport's Source is nil 

如何在Go Bigquery中更改超时?

+0

你不应该用'ctx_with_deadline'也为bigquery.NewClient? –

+0

@AlexEfimov它看起来封装选项没有截止期限选项。 –

+0

我的意思是像'client,err:= bigquery.NewClient(ctx_with_deadline,PROJECT_ID,option.WithServiceAccountFile(SERVICE_ACCOUNT_JSON_FILE_PATH),option.WithHTTPClient(httpClient))' –

回答

1

使用ctx_with_deadline也创造了大量查询客户端的新实例时:

client, err := bigquery.NewClient(ctx_with_deadline, PROJECT_ID, option.WithServiceAccountFile(SERVICE_ACCOUNT_JSON_FILE_PATH‌​), option.WithHTTPClient(httpClient))