Skip to content
This repository was archived by the owner on Oct 12, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { translate, MessagingError } from "./errors";
import { delay } from ".";
import * as log from "./log";
import { defaultRetryAttempts, defaultDelayBetweenRetriesInSeconds } from "./util/constants";
import { resolve } from "dns";

/**
* Determines whether the object is a Delivery object.
Expand Down Expand Up @@ -62,6 +63,11 @@ export interface RetryConfig<T> {
* next attempt. Default: 15.
*/
delayInSeconds?: number;
/**
* @property {string} connectionHost The host "<yournamespace>.servicebus.windows.net".
* Used to check network connectivity.
*/
connectionHost?: string;
Comment thread
ramya-rao-a marked this conversation as resolved.
}

/**
Expand Down Expand Up @@ -91,6 +97,20 @@ function validateRetryConfig<T>(config: RetryConfig<T>): void {
}
}


async function checkNetworkConnection(host: string): Promise<boolean> {
return new Promise((res) => {
resolve(host, function (err: any): void {
if (err && err.code === "ECONNREFUSED") {
res(false);
} else {
res(true);
}
});
});
}


/**
* It will attempt to linearly retry an operation specified number of times with a specified
* delay in between each retry. The retries will only happen if the error is retryable.
Expand Down Expand Up @@ -122,6 +142,14 @@ export async function retry<T>(config: RetryConfig<T>): Promise<T> {
if (!err.translated) {
err = translate(err);
}

if (!err.retryable && err.name === "ServiceCommunicationError" && config.connectionHost) {
const isConnected = await checkNetworkConnection(config.connectionHost);
Comment thread
ramya-rao-a marked this conversation as resolved.
if (!isConnected) {
err.name = "ConnectionLostError";
err.retryable = true;
}
}
lastError = err;
log.error("[%s] Error occured for '%s' in attempt number %d: %O", config.connectionId,
config.operationType, j, err);
Expand Down
Loading