Running a high-volume batch export using @genesyscloud/platform-client-sdk. The script pulls 5000 interactions per minute. Everything works fine for the first 30 minutes, then suddenly the requests start dropping with 401 Unauthorized.
I’ve implemented a standard refresh token interceptor, but it seems the SDK isn’t waiting for the refresh promise to resolve before firing off the next queued request. I’m seeing a burst of 401s followed by a successful refresh, but the failed requests don’t retry automatically.
Here’s the interceptor setup:
const { Client, PlatformClient } = require('@genesyscloud/platform-client-sdk');
const client = PlatformClient.create({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
basePath: 'https://api.mypurecloud.com'
});
// Custom refresh logic
client.login().then(() => {
console.log('Initial login successful');
// Start batch job
runBatchExport();
}).catch(err => {
console.error('Login failed', err);
});
async function runBatchExport() {
const conversationsApi = client.ConversationsApi;
for (let i = 0; i < 100; i++) {
try {
const result = await conversationsApi.getConversationsConversation({
conversationId: `conv-${i}`
});
console.log(`Fetched ${i}`);
} catch (err) {
if (err.status === 401) {
console.warn('Token expired, attempting refresh...');
await client.login(); // This should refresh
console.log('Refreshed, retrying...');
// Manual retry here, but feels wrong
} else {
throw err;
}
}
}
}
The client.login() call inside the catch block does refresh the token, but by then, 5-10 other concurrent requests have already hit the 401 wall. The SDK seems to be allowing multiple requests to proceed while the refresh is pending.
Is there a built-in way to lock the request queue during refresh in the JS SDK? Or am I missing a config flag that handles this race condition? The Node.js environment is handling the concurrency fine, just the auth layer is breaking.