-
Notifications
You must be signed in to change notification settings - Fork 7
Improve error handling, introduce retry logic for GCP PubSub #370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7bcafaa
Improve error handling logic, extra tests, bugfixes for deduplication…
kibertoad 6f9e7dc
More tests
kibertoad 16365f8
Cleanup
kibertoad 8af627a
second cleanup
kibertoad d744f67
Implement reconnect at start
kibertoad 94e2a6d
Add documentation
kibertoad 235b7c4
Improve error structure
kibertoad 046d8fb
Status code management cleanup
kibertoad 196dbd6
minor cleanup
kibertoad 9dccfe3
Address CodeRabbit concerns
kibertoad 07fd731
Address CodeRabbit concerns
kibertoad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/gcp-pubsub/lib/errors/SubscriptionDoesNotExistError.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| export class SubscriptionDoesNotExistError extends Error { | ||
| public readonly subscriptionName: string | ||
|
|
||
| constructor(subscriptionName: string) { | ||
| super(`Subscription ${subscriptionName} does not exist`) | ||
| this.name = 'SubscriptionDoesNotExistError' | ||
| this.subscriptionName = subscriptionName | ||
| } | ||
| } | ||
|
|
||
| export function isSubscriptionDoesNotExistError( | ||
| error: unknown, | ||
| ): error is SubscriptionDoesNotExistError { | ||
| return ( | ||
| typeof error === 'object' && | ||
| error !== null && | ||
| 'name' in error && | ||
| 'message' in error && | ||
| error.name === 'SubscriptionDoesNotExistError' && | ||
| typeof error.message === 'string' | ||
| ) | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { status as GrpcStatus } from '@grpc/grpc-js' | ||
|
|
||
| /** | ||
| * gRPC status codes for which subscription operations should be retried. | ||
| * | ||
| * Includes both: | ||
| * 1. GCP-documented retryable errors (DEADLINE_EXCEEDED, RESOURCE_EXHAUSTED, INTERNAL, UNAVAILABLE) | ||
| * 2. Eventual consistency errors common after Terraform deployments (NOT_FOUND, PERMISSION_DENIED) | ||
| * | ||
| * **Why PERMISSION_DENIED is included:** | ||
| * After Terraform deployments, IAM permissions can take several minutes to propagate across | ||
| * GCP's distributed infrastructure. During this window, the subscription may report | ||
| * PERMISSION_DENIED even though permissions are correctly configured. | ||
| * | ||
| * **Why NOT_FOUND is included:** | ||
| * Similar to PERMISSION_DENIED, newly created subscriptions may not be immediately visible | ||
| * across all GCP endpoints due to eventual consistency. | ||
| * | ||
| * @see https://cloud.google.com/pubsub/docs/reference/error-codes | ||
| * @see https://github.com/googleapis/nodejs-pubsub/issues/979 | ||
| */ | ||
| export const RETRYABLE_GRPC_STATUS_CODES = [ | ||
| GrpcStatus.DEADLINE_EXCEEDED, | ||
| GrpcStatus.NOT_FOUND, | ||
| GrpcStatus.PERMISSION_DENIED, | ||
| GrpcStatus.RESOURCE_EXHAUSTED, | ||
| GrpcStatus.INTERNAL, | ||
| GrpcStatus.UNAVAILABLE, | ||
| ] as const | ||
|
|
||
| export type RetryableGrpcStatusCode = (typeof RETRYABLE_GRPC_STATUS_CODES)[number] | ||
|
|
||
| const RETRYABLE_CODES_SET = new Set<number>(RETRYABLE_GRPC_STATUS_CODES) | ||
|
|
||
| /** | ||
| * Type for errors with a numeric gRPC status code and message. | ||
| */ | ||
| export type GrpcError = { | ||
| code: number | ||
| message: string | ||
| } | ||
|
|
||
| /** | ||
| * Checks if an error has gRPC error properties (code and message). | ||
| * Uses duck typing to avoid fragile instanceof checks. | ||
| * | ||
| * @param error - The error to check | ||
| * @returns true if the error has numeric `code` and string `message` properties | ||
| */ | ||
| export function isGrpcError(error: unknown): error is GrpcError { | ||
| return ( | ||
| typeof error === 'object' && | ||
| error !== null && | ||
| 'code' in error && | ||
| 'message' in error && | ||
| typeof (error as GrpcError).code === 'number' && | ||
| typeof (error as GrpcError).message === 'string' | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Checks if an error is a gRPC error with a retryable status code. | ||
| * | ||
| * @param error - The error to check | ||
| * @returns true if the error has a retryable gRPC status code | ||
| */ | ||
| export function isRetryableGrpcError(error: unknown): error is GrpcError { | ||
| return isGrpcError(error) && RETRYABLE_CODES_SET.has(error.code) | ||
| } | ||
|
|
||
| /** | ||
| * Gets the gRPC status code from an error if it has one. | ||
| * | ||
| * @param error - The error to extract the code from | ||
| * @returns The gRPC status code, or undefined if not a gRPC error | ||
| */ | ||
| export function getGrpcStatusCode(error: unknown): number | undefined { | ||
| if (isGrpcError(error)) { | ||
| return error.code | ||
| } | ||
| return undefined | ||
| } | ||
|
|
||
| // Re-export for convenience | ||
| export { GrpcStatus } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.