Skip to content

Commit 8110939

Browse files
committed
refactor(lit-client): centralise retry reasons as const
1 parent 3c27611 commit 8110939

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

packages/lit-client/src/lib/LitClient/helpers/executeWithHandshake.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ type RetryMetadata = {
2323
reason: string;
2424
};
2525

26+
// Pause briefly before retrying so dropped nodes have time to deregister and surviving owners rebroadcast shares.
27+
const RETRY_BACKOFF_MS = 1_000;
28+
29+
export const RETRY_REASONS = {
30+
missingVerificationKey: 'missing-verification-key',
31+
networkFetch: 'network-fetch-error',
32+
noValidShares: 'no-valid-shares',
33+
generic: 'retry',
34+
} as const;
35+
2636
export namespace EdgeCase {
2737
export const isMissingVerificationKeyError = (error: unknown): boolean => {
2838
if (!error || typeof error !== 'object') {
@@ -102,15 +112,18 @@ export namespace EdgeCase {
102112

103113
const deriveRetryMetadata = (error: unknown): RetryMetadata => {
104114
if (EdgeCase.isMissingVerificationKeyError(error)) {
105-
return { shouldRetry: true, reason: 'missing-verification-key' };
115+
return {
116+
shouldRetry: true,
117+
reason: RETRY_REASONS.missingVerificationKey,
118+
};
106119
}
107120

108121
if (EdgeCase.isNetworkFetchError(error)) {
109-
return { shouldRetry: true, reason: 'network-fetch-error' };
122+
return { shouldRetry: true, reason: RETRY_REASONS.networkFetch };
110123
}
111124

112125
if (EdgeCase.isNoValidSharesError(error)) {
113-
return { shouldRetry: true, reason: 'no-valid-shares' };
126+
return { shouldRetry: true, reason: RETRY_REASONS.noValidShares };
114127
}
115128

116129
return { shouldRetry: false, reason: '' };
@@ -129,9 +142,25 @@ export const executeWithHandshake = async <ReturnType>(
129142
const retryMetadata = deriveRetryMetadata(error);
130143

131144
if (retryMetadata.shouldRetry) {
132-
const reason = retryMetadata.reason || 'retry';
145+
const reason = retryMetadata.reason || RETRY_REASONS.generic;
133146
const refreshLabel = `${operation}-${reason}`.replace(/-+/g, '-');
134147

148+
if (
149+
reason === 'no-valid-shares' ||
150+
reason === 'network-fetch-error'
151+
) {
152+
await new Promise((resolve) => setTimeout(resolve, RETRY_BACKOFF_MS));
153+
}
154+
155+
console.log(
156+
'[executeWithHandshake] retrying operation',
157+
operation,
158+
'reason:',
159+
reason,
160+
'refreshLabel:',
161+
refreshLabel
162+
);
163+
135164
_logger.warn(
136165
{
137166
error,

0 commit comments

Comments
 (0)