Skip to content

Commit

Permalink
Merge pull request #60 from eco-stake/improve-rate-limit-tracking
Browse files Browse the repository at this point in the history
Improve rate limit tracking
  • Loading branch information
tombeynon authored Mar 27, 2023
2 parents a5585b2 + 6061163 commit f3a06ed
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions status/healthMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { timeStamp, debugLog, createAgent } from '../utils.js';
const ALLOWED_DELAY = 30 * 60
const ALLOWED_ERRORS = 10
const ERROR_COOLDOWN = 10 * 60
const RATE_LIMIT_COOLDOWN = 3 * 24 * 60 * 60
const HEALTH_TIMEOUT = 5000

function HealthMonitor() {
Expand Down Expand Up @@ -103,23 +104,26 @@ function HealthMonitor() {
}
const responseTime = timings?.phases?.total

let { lastError, lastErrorAt, lastSuccessAt, available, rateLimited } = urlHealth;
let { lastError, lastErrorAt, lastSuccessAt, available, rateLimited, rateLimitedAt } = urlHealth;
let errorCount = urlHealth.errorCount || 0;
if (error) {
errorCount++;
lastError = error.message;
lastErrorAt = Date.now();
rateLimited = rateLimited || (response?.statusCode === 429)
if(response?.statusCode === 429){
rateLimitedAt = Date.now()
}
} else {
lastSuccessAt = Date.now();
if (errorCount > 0) {
const currentTime = Date.now();
const cooldownDate = (currentTime - 1000 * ERROR_COOLDOWN);
const cooldownDate = currentTime - 1000 * ERROR_COOLDOWN;
if (lastErrorAt <= cooldownDate) {
errorCount = 0;
}
}
}
rateLimited = rateLimitedAt && rateLimitedAt > Date.now() - 1000 * RATE_LIMIT_COOLDOWN

let nowAvailable = false;
if (errorCount <= ALLOWED_ERRORS) {
Expand All @@ -141,6 +145,7 @@ function HealthMonitor() {
lastSuccessAt,
errorCount,
rateLimited,
rateLimitedAt,
available: nowAvailable,
blockHeight: blockHeight,
blockTime: blockTime,
Expand All @@ -151,7 +156,7 @@ function HealthMonitor() {

function checkHeader(type, data, chainId) {
let error, blockTime;
if (data && type === 'rpc')
if (data && ['rpc', 'private-rpc'].includes(type))
data = data.result;

const header = data.block.header;
Expand Down

0 comments on commit f3a06ed

Please sign in to comment.