diff --git a/ts/session/sending/MessageSender.ts b/ts/session/sending/MessageSender.ts index b11b86c8e..e0439a85c 100644 --- a/ts/session/sending/MessageSender.ts +++ b/ts/session/sending/MessageSender.ts @@ -20,17 +20,19 @@ export function canSendToSnode(): boolean { /** * Send a message via service nodes. + * * @param message The message to send. - * @param retries The amount of times to retry sending. + * @param attempts The amount of times to attempt sending. Minimum value is 1. */ export async function send( - { device, plainTextBuffer, encryption, timestamp, ttl }: RawMessage, - retries: number = 3 + message: RawMessage, + attempts: number = 3 ): Promise { if (!canSendToSnode()) { throw new Error('lokiMessageAPI is not initialized.'); } + const { device, plainTextBuffer, encryption, timestamp, ttl } = message; const { envelopeType, cipherText } = await MessageEncrypter.encrypt( device, plainTextBuffer, @@ -39,13 +41,10 @@ export async function send( const envelope = await buildEnvelope(envelopeType, timestamp, cipherText); const data = wrapEnvelope(envelope); - // pRetry counts retries after making the first call. - // So a retry count of 3 means you make a request then if it fails you make another request 3 times until it succeeds. - // This means a total of 4 requests are being sent, where as for us when we want 3 retries we only want 3 requests sent. return pRetry( async () => lokiMessageAPI.sendMessage(device, data, timestamp, ttl), { - retries: retries - 1, + retries: Math.max(attempts - 1, 0), factor: 1, } ); diff --git a/ts/test/session/sending/MessageSender_test.ts b/ts/test/session/sending/MessageSender_test.ts index 8a6525ec8..c534aef9e 100644 --- a/ts/test/session/sending/MessageSender_test.ts +++ b/ts/test/session/sending/MessageSender_test.ts @@ -80,10 +80,10 @@ describe('MessageSender', () => { it('should only retry the specified amount of times before throwing', async () => { lokiMessageAPIStub.sendMessage.throws(new Error('API error')); - const retries = 2; - const promise = MessageSender.send(rawMessage, retries); + const attempts = 2; + const promise = MessageSender.send(rawMessage, attempts); await expect(promise).is.rejectedWith('API error'); - expect(lokiMessageAPIStub.sendMessage.callCount).to.equal(retries); + expect(lokiMessageAPIStub.sendMessage.callCount).to.equal(attempts); }); it('should not throw error if successful send occurs within the retry limit', async () => {