The Google Cloud client libraries for Swift provide
*PollingUntilDone methods to poll long-running operations until they complete.
You might need to change the default polling limits, the backoff intervals, or
the types of polling errors that stop the polling loop. This document shows you
how to override the default polling policies in the client libraries.
Before you begin
This document uses the Workflows API. To enable this API, follow the
Workflows quickstart. Verify that your principal
has the Workflows Admin Identity and Access Management (IAM) role
(roles/workflows.admin), and add swift-google-cloud-workflows-v1 and
swift-google-gax to your Package.swift dependencies.
For complete setup instructions for the Swift client libraries, see Quickstart: Get started with Swift.
Long-running operations
Some Google Cloud APIs perform operations that take too long to finish within a single request-response cycle. Instead of blocking until the work finishes, the API starts a background task on the server. It immediately returns an operation resource that represents the in-progress work.
To get the final result, the client must periodically poll the service for the
operation's status. When you call a *PollingUntilDone helper method, the
client library runs this polling loop for you:
- The client sends the initial request to start the background task on the server.
- While the operation is still in progress, the client waits for a delay determined by the polling backoff policy and requests the latest status of the operation.
- If a status check fails—for example, due to a transient network or service error—the polling error policy determines whether to continue polling or stop the loop. The policy also enforces overall time or attempt limits so polling doesn't run indefinitely.
- When the server reports that the operation has finished, the helper returns the completed resource—or throws the operation's error if the background task failed on the server.
Default polling behavior
Each client uses the following default policies for this polling loop:
- The polling error policy continues polling on
AIP-194 on google.aip.dev transient errors,
TOO_MANY_REQUESTSerrors, and I/O errors, and stops polling after 30 minutes. - The polling backoff policy uses truncated exponential backoff without jitter, starting at one second and doubling between attempts up to a maximum of five minutes.
In addition, each status request made during the polling loop is an idempotent RPC and uses the client's retry policy before reporting an error to the polling loop.
Override the policies for a client
The policies that you set on a client apply to every long-running operation polled through that client. To override the default polling policies for a client, follow these steps:
Add the imports required to use the client library:
Write a function that receives the project ID, location, and workflow ID as parameters:
Initialize a client that stops polling sooner than the default. Call the
BasePollingErrorPolicy.unbounded()method to keep the default definition of transient polling errors, and chain thewithTimeLimit(_:)andwithAttemptLimit(_:)methods to set the limits. You can use either limit on its own:Initialize a client that also configures a custom backoff policy between polling attempts:
The library also provides the following polling error policies:
AIP194.unbounded(): continues polling only onUNAVAILABLEerrors. You can chain this policy with thecontinueOnIO()andcontinueOnTooManyRequests()methods.AlwaysPoll.unbounded(): continues polling on all errors.
If your app has requirements that the built-in types don't cover, you can
implement the PollingErrorPolicy and PollingBackoffPolicy protocols.
Override the policy for a single request
Every *PollingUntilDone method accepts a RequestOptions value. The polling
policies that it sets apply to that operation only and take precedence over the
client configuration:
If the workflow update completes within the configured polling policy, the
*PollingUntilDone helper returns the updated Workflow resource. After
testing this example, delete the workflow to avoid incurring charges:
gcloud workflows delete WORKFLOW_ID \
--location=LOCATION
Replace WORKFLOW_ID and LOCATION
with the workflow ID and location from your test.
Complete code
The following example shows the complete code for this document:
What's next
- Learn how to call long-running operations.
- Learn how to override the default retry policies.
- Learn how to override the default endpoint.
- Learn how to enable debug logging to troubleshoot requests and responses.
- Browse the Google Cloud client libraries for Swift reference documentation.