refactor: implement fallback API connection#2020
refactor: implement fallback API connection#2020AMIRKHANEF wants to merge 3 commits intoPolkaGate:mainfrom
Conversation
WalkthroughRefactors ApiProvider connection flow to async/await, adds MAX_RETRIES and exponential backoff, makes connection attempts sequential (awaiting handleAutoMode, connectToEndpoint, connectToLightClient), updates connectToEndpoint to use throwOnConnect and explicit disconnect/reset on failure, and adds retry/fallback to AUTO_MODE_DEFAULT_ENDPOINT with bounded retries. Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant getApi
participant requestApiConnection as requestApiConnection<br/>(async)
participant handleAutoMode
participant connectToEndpoint
participant connectToLightClient
Caller->>getApi: getApi(genesisHash,endpoints, retryCount?)
alt No endpoint selected
getApi->>getApi: set AUTO_MODE_DEFAULT_ENDPOINT if needed
end
getApi->>requestApiConnection: await requestApiConnection()
rect rgb(220, 235, 255)
Note over requestApiConnection: Sequential connection steps
requestApiConnection->>handleAutoMode: await handleAutoMode()
handleAutoMode-->>requestApiConnection: resolved endpoint
requestApiConnection->>connectToEndpoint: await connectToEndpoint() (throwOnConnect:true)
alt connect success
connectToEndpoint-->>requestApiConnection: ApiPromise
requestApiConnection->>connectToLightClient: await connectToLightClient()
connectToLightClient-->>requestApiConnection: light client ready
else connect failure
connectToEndpoint->>connectToEndpoint: disconnect wsProvider, reset endpoint if AUTO
connectToEndpoint-->>requestApiConnection: throw error
end
end
rect rgb(255, 235, 230)
Note over getApi: Retry & backoff
requestApiConnection-->>getApi: success or error
alt error
getApi->>getApi: log, wait (exponential backoff)
getApi->>getApi: if retryCount < MAX_RETRIES -> recursive getApi(retryCount+1)
else success
getApi-->>Caller: return ApiPromise
end
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Nitpick comments (1)
packages/extension-ui/src/Popup/contexts/ApiProvider.tsx (1)
284-285: Update comment for clarity.The comment says "Start connection" but the
awaitmeans the code waits for the connection to complete, not just initiate it.Apply this diff:
- // Start connection + // Request connection and wait for completion await requestApiConnection(genesisHash, endpoint, endpoints);
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/extension-ui/src/Popup/contexts/ApiProvider.tsx(4 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/extension-ui/src/Popup/contexts/ApiProvider.tsx (2)
packages/extension-polkagate/src/util/constants.ts (1)
AUTO_MODE_DEFAULT_ENDPOINT(270-275)packages/extension-polkagate/src/util/types.ts (2)
EndpointType(894-899)DropdownOption(876-879)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: build
- GitHub Check: Analyze (javascript)
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/extension-ui/src/Popup/contexts/ApiProvider.tsx (1)
16-16: Reuse shared MAX_RETRIES constant.There is already a
MAX_RETRIESexported frompackages/extension-polkagate/src/util/api/utils.ts(value 7). Redefining a different value here risks divergence and makes tuning harder to reason about. Prefer importing the shared constant (or centralizing configuration) so retry limits stay consistent across the codebase.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/extension-ui/src/Popup/contexts/ApiProvider.tsx(5 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2024-08-31T05:19:02.468Z
Learnt from: AMIRKHANEF
Repo: PolkaGate/extension PR: 1467
File: packages/extension-polkagate/src/util/types.ts:0-0
Timestamp: 2024-08-31T05:19:02.468Z
Learning: The `SavedEndpoint` interface in the `packages/extension-polkagate/src/util/types.ts` file has a property named `isOnManual`, which indicates whether the endpoint is in manual mode.
Applied to files:
packages/extension-ui/src/Popup/contexts/ApiProvider.tsx
🧬 Code graph analysis (1)
packages/extension-ui/src/Popup/contexts/ApiProvider.tsx (3)
packages/extension-polkagate/src/util/api/utils.ts (1)
MAX_RETRIES(5-5)packages/extension-polkagate/src/util/constants.ts (1)
AUTO_MODE_DEFAULT_ENDPOINT(270-275)packages/extension-polkagate/src/util/types.ts (2)
EndpointType(894-899)DropdownOption(876-879)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: build
Close: #2015
Summary by CodeRabbit