Skip to content

Commit 5cedcbb

Browse files
Merge pull request #532 from MindscapeHQ/ph/limit-ping
Limit Ping to be less frequent.
2 parents e0349ce + 0abe2f5 commit 5cedcbb

File tree

5 files changed

+60
-26
lines changed

5 files changed

+60
-26
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2929
3030
-->
3131

32+
## [3.1.2]
33+
34+
### Changed
35+
- Updated when ping happens so it only happens once, and when successful, it will store in sessionStorage and not ping again. This is to prevent multiple pings from happening on every page load.
36+
3237
## [3.1.1]
3338

3439
### Fixed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "raygun4js",
3-
"version": "3.1.0",
3+
"version": "3.1.2",
44
"homepage": "http://raygun.com",
55
"authors": [
66
"Mindscape <[email protected]>"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
],
88
"title": "Raygun4js",
99
"description": "Raygun.com plugin for JavaScript",
10-
"version": "3.1.1",
10+
"version": "3.1.2",
1111
"homepage": "https://github.com/MindscapeHQ/raygun4js",
1212
"author": {
1313
"name": "MindscapeHQ",

raygun4js.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
33
<metadata>
44
<id>raygun4js</id>
5-
<version>3.1.0</version>
5+
<version>3.1.2</version>
66
<title>Raygun4js</title>
77
<authors>Raygun Limited</authors>
88
<owners>Raygun Limited</owners>

src/raygun.loader.js

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,17 @@
1919
crashReportingEnabled = false,
2020
captureUnhandledRejections;
2121

22+
var hasSessionStorage = false;
23+
try {
24+
hasSessionStorage = !!window.sessionStorage;
25+
} catch (e) {
26+
// sessionStorage not available
27+
}
28+
2229
var metadata = {
2330
ping : {
31+
sessionStorageItem : 'raygun4js-successful-ping',
2432
sendPing : true,
25-
pingIntervalId : -1,
2633
failedPings : 0
2734
},
2835
};
@@ -230,41 +237,64 @@
230237
};
231238

232239
function ping() {
233-
if(metadata.ping.failedPings > 2) {
234-
clearInterval(metadata.ping.pingIntervalId);
235-
}
236-
237240
if(!Raygun.Options || !Raygun.Options._raygunApiKey || !Raygun.Options._raygunApiUrl){
238-
metadata.ping.failedPings++;
239-
return;
241+
return;
240242
}
241243

242244
var url = Raygun.Options._raygunApiUrl + "/ping?apiKey=" + encodeURIComponent(Raygun.Options._raygunApiKey);
243245
var data = {
244-
crashReportingEnabled: crashReportingEnabled ? true : false,
245-
realUserMonitoringEnabled: realUserMonitoringEnabled ? true : false,
246-
providerName: "raygun4js",
247-
providerVersion: '{{VERSION}}'
246+
crashReportingEnabled: crashReportingEnabled ? true : false,
247+
realUserMonitoringEnabled: realUserMonitoringEnabled ? true : false,
248+
providerName: "raygun4js",
249+
providerVersion: '{{VERSION}}'
248250
};
249251

252+
// Check if we've already pinged with the same data
253+
if (hasSessionStorage) {
254+
var storedData = sessionStorage.getItem(metadata.ping.sessionStorageItem);
255+
if (storedData && storedData === JSON.stringify(data)) {
256+
return;
257+
}
258+
}
259+
250260
fetch(url, {
251-
method: 'POST',
252-
headers: {
253-
'Content-Type': 'application/json'
254-
},
255-
body: JSON.stringify(data)
261+
method: 'POST',
262+
headers: {
263+
'Content-Type': 'application/json'
264+
},
265+
body: JSON.stringify(data)
256266
}).then(function(response) {
257-
if (response.ok) {
258-
metadata.ping.failedPings = 0;
259-
} else {
260-
// Request failed
261-
metadata.ping.failedPings++;
267+
if (response.ok) {
268+
if (hasSessionStorage) {
269+
// Record successful ping in local storage
270+
sessionStorage.setItem(metadata.ping.sessionStorageItem, JSON.stringify(data));
262271
}
263-
}).catch(function() {
272+
metadata.ping.failedPings = 0;
273+
} else {
274+
retryPing(metadata.ping.failedPings);
264275
metadata.ping.failedPings++;
276+
}
277+
}).catch(function() {
278+
retryPing(metadata.ping.failedPings);
279+
metadata.ping.failedPings++;
265280
});
266281
}
267282

283+
var retryPing = function(failedPings) {
284+
if (failedPings > 5) {
285+
// Stop retrying after 5 failed attempts
286+
return;
287+
}
288+
289+
// Generates a delay of 10/20/40/80/120 seconds
290+
var backoffDelay = Math.min(
291+
10 * Math.pow(2, metadata.ping.failedPings),
292+
120 // 2 minutes
293+
) * 1000;
294+
295+
// Retry after backoff delay
296+
setTimeout(ping, backoffDelay);
297+
};
268298

269299
var installGlobalExecutor = function() {
270300
window[window['RaygunObject']] = function() {
@@ -325,7 +355,6 @@
325355

326356
if(metadata.ping.sendPing) {
327357
ping(); //call immediately
328-
metadata.ping.pingIntervalId = setInterval(ping, 1000 * 60 * 5); //5 minutes
329358
}
330359
window[window['RaygunObject']].q = errorQueue;
331360
};

0 commit comments

Comments
 (0)