Skip to content

Commit 3269b9e

Browse files
committed
Add retry logic to add-participant to mitigate proxy address collisions
1 parent 9951a2a commit 3269b9e

File tree

5 files changed

+45
-19
lines changed

5 files changed

+45
-19
lines changed

src/@types/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface ActiveProxyAddresses {
2424
}
2525

2626
export interface ProxyBindings {
27-
[key: string]: string;
27+
[key: string]: Array<string>;
2828
}
2929

3030
export interface ConversationParticipant {

src/services/session.service.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ export const matchAvailableProxyAddresses = async (activeProxyAddresses: ActiveP
3636
})
3737

3838
if (availableNumbers.length < 1) {
39-
throw new Error('Not enough numbers available in pool.')
39+
throw new Error(`Not enough numbers available in pool for ${key}`)
4040
}
4141

42-
proxyBindings[key] = availableNumbers[0];
42+
proxyBindings[key] = availableNumbers;
4343
}
4444

4545
return proxyBindings;
@@ -49,18 +49,42 @@ export const matchAvailableProxyAddresses = async (activeProxyAddresses: ActiveP
4949
}
5050
}
5151

52+
const retryParticipantAdd = async (conversationSid: string, participantAddress: string, proxyAddresses: Array<string>) => {
53+
try {
54+
while(proxyAddresses.length > 0) {
55+
try {
56+
const participant = {
57+
'messagingBinding.address': participantAddress,
58+
'messagingBinding.proxyAddress': proxyAddresses[0]
59+
} as any
60+
61+
return addParticipant(conversationSid, participant)
62+
63+
} catch(err) {
64+
if (err.code === 50416) {
65+
const remainingProxyAddresses = proxyAddresses.shift()
66+
retryParticipantAdd(conversationSid, participantAddress, remainingProxyAddresses as any)
67+
}
68+
console.log(err)
69+
throw new Error(err)
70+
}
71+
}
72+
73+
throw new Error(`No proxy addresses available for ${participantAddress}`)
74+
} catch(err) {
75+
console.log(err)
76+
throw new Error(err)
77+
}
78+
}
79+
5280
export const addParticipantsToConversation = async (conversationSid: string, proxyBindings: ProxyBindings) => {
5381
const promises = []
5482

55-
for (const [key, value] of Object.entries(proxyBindings)) {
56-
const participant = {
57-
'messagingBinding.address': key,
58-
'messagingBinding.proxyAddress': value
59-
} as any
83+
for (const [participantAddress, proxyAddresses] of Object.entries(proxyBindings)) {
6084

6185
try {
62-
const participantRequest = addParticipant(conversationSid, participant)
63-
promises.push(participantRequest)
86+
const participantAttempt = retryParticipantAdd(conversationSid, participantAddress, proxyAddresses)
87+
promises.push(participantAttempt)
6488
} catch (err) {
6589
console.log(err)
6690
throw new Error(err)

src/utils/addParticipant.util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const addParticipant = async (
1212
.conversations(conversationSid)
1313
.participants
1414
.create(participant)
15-
console.log({createdParticipant})
15+
1616
return createdParticipant
1717
} catch (err) {
1818
console.log('Create participant err', err);

src/utils/deleteConversation.util.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import client from "../twilioClient"
33
import retry from 'async-retry'
44

55
export const deleteConversation = async (conversationSid: string) : Promise<boolean> => {
6-
console.log('Before retry')
76
return retry(async() => {
87
try {
98
await client.conversations.conversations(conversationSid).remove()

src/utils/listParticipantConversations.util.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import { ParticipantConversationInstance } from 'twilio/lib/rest/conversations/v
33
import client from '../twilioClient'
44

55
export const listParticipantConversations = async (phoneNumber: string) : Promise<ParticipantConversationInstance[]> => {
6-
try {
7-
const activeConversations = await client.conversations.participantConversations.list({address: phoneNumber})
8-
return activeConversations
9-
} catch (err) {
10-
console.log(phoneNumber, err)
11-
throw new Error(err)
12-
}
6+
return retry(async () => {
7+
try {
8+
const activeConversations = await client.conversations.participantConversations.list({address: phoneNumber})
9+
return activeConversations
10+
} catch (err) {
11+
console.log(phoneNumber, err)
12+
throw new Error(err)
13+
}
14+
})
15+
1316
}

0 commit comments

Comments
 (0)