You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

61 lines
1.9 KiB

  1. // Copyright 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cloud_test
  15. import (
  16. "context"
  17. "time"
  18. "cloud.google.com/go/bigquery"
  19. )
  20. // To set a timeout for an RPC, use context.WithTimeout.
  21. func Example_timeout() {
  22. ctx := context.Background()
  23. // Do not set a timeout on the context passed to NewClient: dialing happens
  24. // asynchronously, and the context is used to refresh credentials in the
  25. // background.
  26. client, err := bigquery.NewClient(ctx, "project-id")
  27. if err != nil {
  28. // TODO: handle error.
  29. }
  30. // Time out if it takes more than 10 seconds to create a dataset.
  31. tctx, cancel := context.WithTimeout(ctx, 10*time.Second)
  32. defer cancel() // Always call cancel.
  33. if err := client.Dataset("new-dataset").Create(tctx, nil); err != nil {
  34. // TODO: handle error.
  35. }
  36. }
  37. // To arrange for an RPC to be canceled, use context.WithCancel.
  38. func Example_cancellation() {
  39. ctx := context.Background()
  40. // Do not cancel the context passed to NewClient: dialing happens asynchronously,
  41. // and the context is used to refresh credentials in the background.
  42. client, err := bigquery.NewClient(ctx, "project-id")
  43. if err != nil {
  44. // TODO: handle error.
  45. }
  46. cctx, cancel := context.WithCancel(ctx)
  47. defer cancel() // Always call cancel.
  48. // TODO: Make the cancel function available to whatever might want to cancel the
  49. // call--perhaps a GUI button.
  50. if err := client.Dataset("new-dataset").Create(cctx, nil); err != nil {
  51. // TODO: handle error.
  52. }
  53. }