Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit Ping to be less frequent. #532

Merged
merged 4 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

-->

## [3.1.2]

### Changed
- 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.

## [3.1.1]

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "raygun4js",
"version": "3.1.0",
"version": "3.1.2",
"homepage": "http://raygun.com",
"authors": [
"Mindscape <[email protected]>"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"title": "Raygun4js",
"description": "Raygun.com plugin for JavaScript",
"version": "3.1.1",
"version": "3.1.2",
"homepage": "https://github.com/MindscapeHQ/raygun4js",
"author": {
"name": "MindscapeHQ",
Expand Down
2 changes: 1 addition & 1 deletion raygun4js.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>raygun4js</id>
<version>3.1.0</version>
<version>3.1.2</version>
<title>Raygun4js</title>
<authors>Raygun Limited</authors>
<owners>Raygun Limited</owners>
Expand Down
75 changes: 52 additions & 23 deletions src/raygun.loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@
crashReportingEnabled = false,
captureUnhandledRejections;

var hasSessionStorage = false;
try {
hasSessionStorage = !!window.sessionStorage;
} catch (e) {
// sessionStorage not available
}

var metadata = {
ping : {
sessionStorageItem : 'raygun4js-successful-ping',
sendPing : true,
pingIntervalId : -1,
failedPings : 0
},
};
Expand Down Expand Up @@ -230,41 +237,64 @@
};

function ping() {
if(metadata.ping.failedPings > 2) {
clearInterval(metadata.ping.pingIntervalId);
}

if(!Raygun.Options || !Raygun.Options._raygunApiKey || !Raygun.Options._raygunApiUrl){
metadata.ping.failedPings++;
return;
return;
}

var url = Raygun.Options._raygunApiUrl + "/ping?apiKey=" + encodeURIComponent(Raygun.Options._raygunApiKey);
var data = {
crashReportingEnabled: crashReportingEnabled ? true : false,
realUserMonitoringEnabled: realUserMonitoringEnabled ? true : false,
providerName: "raygun4js",
providerVersion: '{{VERSION}}'
crashReportingEnabled: crashReportingEnabled ? true : false,
realUserMonitoringEnabled: realUserMonitoringEnabled ? true : false,
providerName: "raygun4js",
providerVersion: '{{VERSION}}'
};

// Check if we've already pinged with the same data
if (hasSessionStorage) {
var storedData = sessionStorage.getItem(metadata.ping.sessionStorageItem);
if (storedData && storedData === JSON.stringify(data)) {
return;
}
}

fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}).then(function(response) {
if (response.ok) {
metadata.ping.failedPings = 0;
} else {
// Request failed
metadata.ping.failedPings++;
if (response.ok) {
if (hasSessionStorage) {
// Record successful ping in local storage
sessionStorage.setItem(metadata.ping.sessionStorageItem, JSON.stringify(data));
}
}).catch(function() {
metadata.ping.failedPings = 0;
} else {
retryPing(metadata.ping.failedPings);
metadata.ping.failedPings++;
}
}).catch(function() {
retryPing(metadata.ping.failedPings);
metadata.ping.failedPings++;
});
}

var retryPing = function(failedPings) {
if (failedPings > 5) {
// Stop retrying after 5 failed attempts
return;
}

// Generates a delay of 10/20/40/80/120 seconds
var backoffDelay = Math.min(
10 * Math.pow(2, metadata.ping.failedPings),
120 // 2 minutes
) * 1000;

// Retry after backoff delay
setTimeout(ping, backoffDelay);
};

var installGlobalExecutor = function() {
window[window['RaygunObject']] = function() {
Expand Down Expand Up @@ -325,7 +355,6 @@

if(metadata.ping.sendPing) {
ping(); //call immediately
metadata.ping.pingIntervalId = setInterval(ping, 1000 * 60 * 5); //5 minutes
}
window[window['RaygunObject']].q = errorQueue;
};
Expand Down