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

Add jitter to autosync functionality #90

Merged
merged 1 commit into from
Feb 29, 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
6 changes: 6 additions & 0 deletions config/defaults.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ settings:
# TX__SETTINGS__AUTOSYNC_MIN=15
autosync_min: 60

# A jitter to add to autosync functionality to randomly
# distribute cache invalidations over time. Cache will automatically
# invalidate at lastUpdate + autosync_min + random(autosync_jitter_min)
# TX__SETTINGS__AUTOSYNC_JITTER_MIN=15
autosync_jitter_min: 30

# Optionally, a whitelist of project tokens. Set this value if you need
# to limit access to only a subset of Native Project Tokens, vs having
# CDS cache everything. e.g.
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async function routerCacheHelper(req, res, key, filter, syncFunc, ...syncFuncPar
sentContent = true;
}
// check for auto refresh
if ((Date.now() - rdata.ts) >= autoSyncMSec) {
if ((Date.now() - (rdata.ts + (rdata.ts_jitter || 0))) >= autoSyncMSec) {
addJob();
}
break;
Expand Down
3 changes: 3 additions & 0 deletions src/queue/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { sendToTelemetry } = require('../telemetry');
const pullSuccessExpireSec = config.get('settings:pull_success_cache_min') * 60;
const pullErrorExpireSec = config.get('settings:pull_error_cache_min') * 60;
const jobStatusCacheSec = config.get('settings:job_status_cache_min') * 60;
const autoSyncJitterMin = config.get('settings:autosync_jitter_min') * 1;

/**
* Pull content from API syncer job
Expand Down Expand Up @@ -40,6 +41,7 @@ async function syncerPull(job) {
registry.set(`cache:${key}`, {
status: 'success',
ts: Date.now(),
ts_jitter: _.random(autoSyncJitterMin) * 60 * 1000,
etag,
location,
cacheKey,
Expand Down Expand Up @@ -70,6 +72,7 @@ async function syncerPull(job) {
registry.set(`cache:${key}`, {
status: 'error',
ts: Date.now(),
ts_jitter: _.random(autoSyncJitterMin) * 60 * 1000,
statusCode,
statusMessage,
}, pullErrorExpireSec),
Expand Down
Loading