muscovy
muscovy

Reputation: 43

BigQuery golang client: which context to use?

I'm using the Go BigQuery client package and I'm a bit confused about context usage.

In the docs, only the Put function explicitly mentions to use a new context with timeout to avoid indefinite retries. Is it safe to use context.Background() for all other calls?

Upvotes: 3

Views: 680

Answers (1)

Sergi Muñoz
Sergi Muñoz

Reputation: 75

From golang site:

Package context defines the Context type, which carries deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes.

Context, then, are designed to be adjusted and customized for each use case. You would use the functions provided by the library to achieve that.

The WithCancel, WithDeadline, and WithTimeout functions take a Context (the parent) and return a derived Context (the child) and a CancelFunc. Calling the CancelFunc cancels the child and its children, removes the parent's reference to the child, and stops any associated timer

Thus, the goDocs recommends the use of WithTimeout function in your context while inserting data to BigQuery and preventing from duplicates, as you have already seen.

The only must is to pass a non-nil Context. You would usually use context.Background() as a way to go (as I can see in snippets.go shared from Cloud Github), yet you are safe to pass context.TODO if you are unsure about which one to use.

Upvotes: 1

Related Questions