From 0327722b07b70ed9bff4478581963ecc808390bb Mon Sep 17 00:00:00 2001 From: noahdietz Date: Mon, 25 Sep 2023 12:33:50 -0700 Subject: [PATCH 1/4] fix(docs): add an operation polling example --- doc.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/doc.go b/doc.go index 0bce85869b5..67004a9d6dc 100644 --- a/doc.go +++ b/doc.go @@ -84,4 +84,38 @@ // fmt.Println("Domain: %s", aErr.Domain()) // } // } +// +// # Polling Operations +// +// If an API call returns an Operation, that means it could take some time to +// complete the work initiated by the API call. Applications that are interested +// in the end result of the operation they initiated should wait until the +// Operation.Done field indicates it is finished. To do this, use the service's +// Operation client, and a loop, like so: +// +// op, err := myApiClient.CalculateFoo().Do() +// if err != nil { +// // handle err +// } +// +// operationsService = NewOperationsService(myApiClient) +// for { +// if op.Done { +// break +// } +// // not done, sleep then poll again +// time.Sleep(1 * time.Second) +// +// op, err := operationsService.Get(op.Name).Do() +// if err != nil { +// // handle error +// } +// } +// +// if op.Error != nil { +// // handle operation err +// } +// +// // Do something with the response +// fmt.Println(op.Response) package api From a65b837c0b964230c5c1e977a7fdfcd8260de6bd Mon Sep 17 00:00:00 2001 From: noahdietz Date: Mon, 25 Sep 2023 12:35:17 -0700 Subject: [PATCH 2/4] fmt --- doc.go | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/doc.go b/doc.go index 67004a9d6dc..7791b252ea2 100644 --- a/doc.go +++ b/doc.go @@ -93,29 +93,29 @@ // Operation.Done field indicates it is finished. To do this, use the service's // Operation client, and a loop, like so: // -// op, err := myApiClient.CalculateFoo().Do() -// if err != nil { -// // handle err -// } -// -// operationsService = NewOperationsService(myApiClient) -// for { -// if op.Done { -// break +// op, err := myApiClient.CalculateFoo().Do() +// if err != nil { +// // handle err +// } +// +// operationsService = NewOperationsService(myApiClient) +// for { +// if op.Done { +// break +// } +// // not done, sleep then poll again +// time.Sleep(1 * time.Second) +// +// op, err := operationsService.Get(op.Name).Do() +// if err != nil { +// // handle error +// } // } -// // not done, sleep then poll again -// time.Sleep(1 * time.Second) -// -// op, err := operationsService.Get(op.Name).Do() -// if err != nil { -// // handle error -// } -// } // -// if op.Error != nil { -// // handle operation err -// } +// if op.Error != nil { +// // handle operation err +// } // -// // Do something with the response -// fmt.Println(op.Response) +// // Do something with the response +// fmt.Println(op.Response) package api From 4fe94cdf2ed9d40ecb52970448f232bda855370b Mon Sep 17 00:00:00 2001 From: noahdietz Date: Mon, 25 Sep 2023 12:37:35 -0700 Subject: [PATCH 3/4] fmt --- doc.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/doc.go b/doc.go index 7791b252ea2..7cd3dcf25a7 100644 --- a/doc.go +++ b/doc.go @@ -95,25 +95,25 @@ // // op, err := myApiClient.CalculateFoo().Do() // if err != nil { -// // handle err +// // handle err // } // -// operationsService = NewOperationsService(myApiClient) -// for { -// if op.Done { -// break -// } -// // not done, sleep then poll again -// time.Sleep(1 * time.Second) -// -// op, err := operationsService.Get(op.Name).Do() -// if err != nil { -// // handle error -// } +// operationsService = NewOperationsService(myApiClient) +// for { +// if op.Done { +// break // } +// // not done, sleep then poll again +// time.Sleep(1 * time.Second) +// +// op, err := operationsService.Get(op.Name).Do() +// if err != nil { +// // handle error +// } +// } // // if op.Error != nil { -// // handle operation err +// // handle operation err // } // // // Do something with the response From 79c0fac8747df5f909a30e792cce77e097200055 Mon Sep 17 00:00:00 2001 From: noahdietz Date: Mon, 25 Sep 2023 13:30:20 -0700 Subject: [PATCH 4/4] add backoff --- doc.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/doc.go b/doc.go index 7cd3dcf25a7..3095f42b1e5 100644 --- a/doc.go +++ b/doc.go @@ -93,19 +93,32 @@ // Operation.Done field indicates it is finished. To do this, use the service's // Operation client, and a loop, like so: // +// import ( +// gax "github.com/googleapis/gax-go/v2" +// ) +// +// // existing application code... +// +// // API call that returns an Operation. // op, err := myApiClient.CalculateFoo().Do() // if err != nil { // // handle err // } // -// operationsService = NewOperationsService(myApiClient) +// operationsService = myapi.NewOperationsService(myApiClient) +// pollingBackoff := gax.Backoff{ +// Initial: time.Second, +// Max: time.Minute, // Max time between polling attempts. +// Multiplier: 2, +// } // for { // if op.Done { // break // } -// // not done, sleep then poll again -// time.Sleep(1 * time.Second) -// +// // not done, sleep with backoff, then poll again +// if err := gax.Sleep(ctx, pollingBackoff.Pause()); err != nil { +// // handle error +// } // op, err := operationsService.Get(op.Name).Do() // if err != nil { // // handle error