Skip to content

Commit 23682b8

Browse files
debugging retry handler with promise retry. adding retry example.
1 parent cf75240 commit 23682b8

File tree

4 files changed

+85
-220
lines changed

4 files changed

+85
-220
lines changed

examples/basic/basicSendWithRetry.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@ message.to.push("[email protected]");
1717
/**
1818
* Create the client
1919
*/
20-
var client = new SocketLabsClient(exampleConfig.ServerId, exampleConfig.ApiKey, {
20+
var client = new SocketLabsClient(exampleConfig.ServerId, exampleConfig.ApiKey,
21+
{
22+
optionalProxy: "http://localhost:4433"
23+
});
2124

22-
//optionalProxy: "http://localhost:4433",
23-
//requestTimeout: 120,
24-
numberOfRetries = 3
25-
});
26-
27-
console.log(exampleConfig.ServerId);
28-
console.log(exampleConfig.ApiKey);
25+
client.numberOfRetries = 3;
2926

3027
/**
3128
* Send the message

src/core/retryHandler.js

Lines changed: 53 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,163 +1,74 @@
11
'use strict';
22

3-
//const sendResultEnum = require('./sendResultEnum');
4-
5-
const sendResultEnum = require('../sendResultEnum');
6-
const request = require('request');
7-
3+
const axios = require('axios');
4+
const promiseRetry = require('promise-retry');
85

96
class RetryHandler {
107
/**
118
* Creates a new instance of the SocketLabsClient
12-
* @param {number} serverId - Your SocketLabs ServerId number.
13-
* @param {string} apiKey - Your SocketLabs Injection API key
14-
* @param {string} endpointUrl - The SocketLabs Injection API endpoint Url
15-
* @param {string} optionalProxy - The http proxy you would like to use.
16-
* @param {number} requestTimeout - The timeout occured sending the message.
17-
* @param {number} retrySettings - The retry value sent .
9+
* @param {object} request - Request options
10+
* @param {object} retrySettings - The retry value sent .
1811
*
1912
*/
2013

21-
constructor(serverId, apiKey, {
22-
23-
endpointUrl = null,
24-
optionalProxy = null,
25-
requestTimeout = 120,
26-
retryHandler = null,
27-
} = {}) {
28-
29-
/**
30-
* Your SocketLabs ServerId number.
31-
*/
32-
this.serverId = serverId;
33-
34-
/**
35-
* Your SocketLabs Injection API key
36-
*/
37-
this.apiKey = apiKey;
14+
constructor(
15+
maximumNumberOfRetries) {
3816

17+
this._defaultNumberOfRetries = 0;
18+
this._maximumAllowedNumberOfRetries = 5;
3919

40-
/**
41-
* The SocketLabs Injection API endpoint Url
42-
*/
43-
this.userAgent = `SocketLabs-node/${version} (nodejs ${process.version})`;
20+
if (maximumNumberOfRetries != null) {
21+
if (maximumNumberOfRetries < 0) throw new Error(`Argument maximumNumberOfRetries is invalid. maximumNumberOfRetries must be greater than 0`);
22+
if (maximumNumberOfRetries > this._maximumAllowedNumberOfRetries) throw new Error(`Argument maximumNumberOfRetries is invalid. The maximum number of allowed retries is ${this._maximumAllowedNumberOfRetries}`);
4423

45-
/**
46-
* The SocketLabs Injection API endpoint Url
47-
*/
48-
//this.endpointUrl = "http://localhost:4433";
49-
50-
this.endpointUrl = "https://inject.socketlabs.com/api/v1/email";
51-
if (endpointUrl && endpointUrl !== '') {
52-
this.endpointUrl = endpointUrl;
53-
}
54-
55-
if (optionalProxy && typeof optionalProxy === 'string') {
56-
request.defaults({
57-
'proxy': optionalProxy
58-
});
24+
this.MaximumNumberOfRetries = maximumNumberOfRetries;
5925
}
26+
else
27+
this.MaximumNumberOfRetries = this._defaultNumberOfRetries;
6028

61-
requestTimeout = {
62-
63-
get requestTimeout() {
64-
return this.requestTimeout;
65-
},
66-
set requestTimeout(value) {
67-
this.requestTimeout = value;
68-
}
69-
};
29+
this.errorStatusCodes = [500, 502, 503, 504, "ECONNABORTED"];
7030

71-
retryHandler = {
72-
get retryHandler() {
73-
return this.retryHandler;
74-
},
75-
set retryHandler(value) {
76-
this.retryHandler = value;
77-
}
78-
79-
};
8031
}
8132

8233
/**
83-
* Set the unique key generated by the Injection API if an unexpected error occurs during the SocketLabsClient send request.
84-
* @param {string} value
85-
*/
86-
87-
88-
/**
89-
* Set the result of the SocketLabsClient send request.
90-
* @param {sendResultEnum} value
34+
*
35+
* @param {*} request
36+
* @returns
9137
*/
92-
93-
setResult(value) {
94-
var sr;
95-
if (typeof value === 'undefined' || !value) {
96-
return;
97-
}
98-
else if ((typeof value === 'object') &&
99-
('name' in value) &&
100-
('value' in value) &&
101-
('message' in value)) {
102-
103-
104-
/**
105-
* The result of the SocketLabsClient send request.
106-
*/
107-
this.result = value;
108-
this.responseMessage = value.message;
109-
}
110-
else {
111-
throw new Error("Invalid sendResult, type of 'sendResultEnum' was expected.");
112-
}
38+
send(request) {
39+
return new Promise((resolve, reject) => {
40+
41+
promiseRetry(
42+
{
43+
retries: this.MaximumNumberOfRetries,
44+
factor: 2,
45+
randomize: true,
46+
minTimeout: 1000,
47+
maxTimeout: 10000
48+
},
49+
(retry, number) => {
50+
console.log(`attempt: ${number}`);
51+
return axios(request)
52+
.then(
53+
(response) => { resolve(response); },
54+
(error) => {
55+
let statusCode = (error.code) ? error.code : (error.status) ? error.status : error.response.status;
56+
console.log(`statusCode: ${statusCode}`);
57+
if ((this.errorStatusCodes.includes(statusCode) &&
58+
number <= this.MaximumNumberOfRetries))
59+
retry(error);
60+
reject(error);
61+
}
62+
)
63+
.catch(retry);
64+
})
65+
.then(
66+
(result) => { resolve(result); },
67+
(error) => { reject(error) })
68+
.catch(error => { reject(error); });
69+
70+
});
11371
}
72+
}
11473

115-
static parse(value) {
116-
console.log(value);
117-
console.log(value.statusCode);
118-
119-
switch (value.statusCode) {
120-
case 200: //HttpStatusCode.OK
121-
122-
var r = sendResultEnum[body.ErrorCode];
123-
if (r === undefined) {
124-
response.setResult(sendResultEnum.UnknownError);
125-
}
126-
else {
127-
response.setResult(r);
128-
}
129-
break;
130-
131-
case 500: //HttpStatusCode.InternalServerError
132-
response.setResult(sendResultEnum.InternalError);
133-
break;
134-
135-
case 502: //HttpStatusCode.Bad Gateway
136-
response.setResult(sendResultEnum.BadGateway);
137-
return response;
138-
//break;
139-
140-
case 503: //HttpStatusCode.Service Unavailable
141-
response.setResult(sendResultEnum.ServiceUnavailable);
142-
break;
143-
case 504: //HttpStatusCode.GatewayTimeout
144-
response.setResult(sendResultEnum.GatewayTimeout);
145-
break;
146-
147-
default:
148-
response.transactionReceipt = body.transactionReceipt;
149-
response.setResult(sendResultEnum.UnknownError);
150-
151-
break;
152-
}
153-
154-
}
155-
156-
SendAsync(content, cancellationToken) {
157-
158-
if (retrySettings.MaximumNumberOfRetries == 0)
159-
return await HttpClient
160-
161-
162-
}
163-
}
74+
module.exports = RetryHandler;

src/retrySettings.js

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)