Skip to content

Commit bd12eba

Browse files
authored
fix(docs): add an operation polling example (#2186)
1 parent 26c6885 commit bd12eba

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

doc.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,51 @@
8484
// fmt.Println("Domain: %s", aErr.Domain())
8585
// }
8686
// }
87+
//
88+
// # Polling Operations
89+
//
90+
// If an API call returns an Operation, that means it could take some time to
91+
// complete the work initiated by the API call. Applications that are interested
92+
// in the end result of the operation they initiated should wait until the
93+
// Operation.Done field indicates it is finished. To do this, use the service's
94+
// Operation client, and a loop, like so:
95+
//
96+
// import (
97+
// gax "github.com/googleapis/gax-go/v2"
98+
// )
99+
//
100+
// // existing application code...
101+
//
102+
// // API call that returns an Operation.
103+
// op, err := myApiClient.CalculateFoo().Do()
104+
// if err != nil {
105+
// // handle err
106+
// }
107+
//
108+
// operationsService = myapi.NewOperationsService(myApiClient)
109+
// pollingBackoff := gax.Backoff{
110+
// Initial: time.Second,
111+
// Max: time.Minute, // Max time between polling attempts.
112+
// Multiplier: 2,
113+
// }
114+
// for {
115+
// if op.Done {
116+
// break
117+
// }
118+
// // not done, sleep with backoff, then poll again
119+
// if err := gax.Sleep(ctx, pollingBackoff.Pause()); err != nil {
120+
// // handle error
121+
// }
122+
// op, err := operationsService.Get(op.Name).Do()
123+
// if err != nil {
124+
// // handle error
125+
// }
126+
// }
127+
//
128+
// if op.Error != nil {
129+
// // handle operation err
130+
// }
131+
//
132+
// // Do something with the response
133+
// fmt.Println(op.Response)
87134
package api

0 commit comments

Comments
 (0)