diff --git a/lib/config-store.js b/lib/config-store.js index 33e37b2..4635d23 100644 --- a/lib/config-store.js +++ b/lib/config-store.js @@ -83,6 +83,7 @@ const DEFAULTS = { deleteSourceAfterSuccessfulUpload: false, showDropTarget: false, globalMaxSpeedKbs: 0, // 0 = unlimited global speed + speedUnits: 'decimal', pendingQueue: null, uploadRecovery: null, scramble: { @@ -420,6 +421,9 @@ class ConfigStore { } } + const savedGlobal = data.globalSettings || {}; + const binarySpeeds = savedGlobal.speedUnits !== 'decimal'; + const toDecimalKbs = (value) => binarySpeeds && Number(value) > 0 ? Math.round(Number(value) * 1000 / 1024) : value; // Merge hoster settings with defaults const hosterSettings = {}; for (const name of Object.keys(DEFAULTS.hosterSettings)) { @@ -427,12 +431,15 @@ class ConfigStore { ...HOSTER_SETTINGS_DEFAULTS, ...(data.hosterSettings && data.hosterSettings[name] || {}) }; + hosterSettings[name].maxSpeedKbs = toDecimalKbs(hosterSettings[name].maxSpeedKbs); + hosterSettings[name].restartBelowKbs = toDecimalKbs(hosterSettings[name].restartBelowKbs); } - const savedGlobal = data.globalSettings || {}; const globalSettings = { ...DEFAULTS.globalSettings, ...savedGlobal }; + globalSettings.globalMaxSpeedKbs = toDecimalKbs(globalSettings.globalMaxSpeedKbs); + globalSettings.speedUnits = 'decimal'; delete globalSettings.allowPlaintextCredentialStorage; // Deep-merge nested objects so new keys are always present for (const key of Object.keys(DEFAULTS.globalSettings)) { diff --git a/lib/throttle.js b/lib/throttle.js index abb1ad0..e6a90a2 100644 --- a/lib/throttle.js +++ b/lib/throttle.js @@ -1,42 +1,41 @@ /** - * Token-bucket speed limiter for bandwidth throttling. + * Shared upload rate limiter. Every consumed byte reserves send time at the + * configured rate, so concurrent uploads together never exceed it. Only a + * short burst (burstMs) may be sent ahead after idle time. * maxBytesPerSec = 0 means unlimited (passthrough). */ class Throttle { - constructor(maxBytesPerSec) { + constructor(maxBytesPerSec, options = {}) { this.maxBps = maxBytesPerSec || 0; - this.tokens = this.maxBps; - this.lastRefill = Date.now(); + this.burstMs = Math.max(0, Number(options.burstMs ?? 200)); + this.now = typeof options.now === 'function' ? options.now : () => Date.now(); + this.nextFreeAt = this.now(); } async consume(bytes, signal) { - if (this.maxBps <= 0) return; // unlimited - - while (bytes > 0) { - if (signal && signal.aborted) return; - this._refill(); - const available = Math.min(bytes, Math.floor(this.tokens)); - if (available > 0) { - this.tokens -= available; - bytes -= available; + if (this.maxBps <= 0 || !(bytes > 0)) return; + if (signal && signal.aborted) return; + const now = this.now(); + const start = Math.max(this.nextFreeAt, now - this.burstMs); + this.nextFreeAt = start + (bytes * 1000) / this.maxBps; + const wait = start - now; + if (wait <= 0) return; + await new Promise((resolve) => { + const timer = setTimeout(done, wait); + function done() { + clearTimeout(timer); + if (signal) signal.removeEventListener('abort', done); + resolve(); } - if (bytes > 0) { - if (signal && signal.aborted) return; - // Wait 50ms for tokens to refill - await new Promise((r) => setTimeout(r, 50)); - } - } - } - - _refill() { - const now = Date.now(); - const elapsed = (now - this.lastRefill) / 1000; - this.tokens = Math.min(this.maxBps, this.tokens + elapsed * this.maxBps); - this.lastRefill = now; + if (signal) signal.addEventListener('abort', done, { once: true }); + }); } updateRate(maxBytesPerSec) { - this.maxBps = maxBytesPerSec || 0; + const next = maxBytesPerSec || 0; + if (next === this.maxBps) return; + this.maxBps = next; + this.nextFreeAt = Math.min(this.nextFreeAt, this.now() + this.burstMs); } } diff --git a/lib/upload-manager.js b/lib/upload-manager.js index 3ee7b80..4d8b3bd 100644 --- a/lib/upload-manager.js +++ b/lib/upload-manager.js @@ -319,9 +319,9 @@ class UploadManager extends EventEmitter { const newKbs = (this.globalSettings.globalMaxSpeedKbs || 0); if (newKbs > 0) { if (this.globalThrottle) { - this.globalThrottle.updateRate(newKbs * 1024); + this.globalThrottle.updateRate(newKbs * 1000); } else { - this.globalThrottle = new Throttle(newKbs * 1024); + this.globalThrottle = new Throttle(newKbs * 1000); } } else { this.globalThrottle = null; @@ -363,9 +363,9 @@ class UploadManager extends EventEmitter { const kbs = Number(this.globalSettings.globalMaxSpeedKbs || 0); if (!Number.isFinite(kbs) || kbs <= 0) return null; if (!this.globalThrottle) { - this.globalThrottle = new Throttle(kbs * 1024); + this.globalThrottle = new Throttle(kbs * 1000); } else { - this.globalThrottle.updateRate(kbs * 1024); + this.globalThrottle.updateRate(kbs * 1000); } return this.globalThrottle; } @@ -730,7 +730,7 @@ class UploadManager extends EventEmitter { }); const hosterThrottle = settings.maxSpeedKbs > 0 - ? new Throttle(settings.maxSpeedKbs * 1024) + ? new Throttle(settings.maxSpeedKbs * 1000) : null; const globalThrottle = this._getGlobalThrottle(); const throttle = hosterThrottle && globalThrottle @@ -770,7 +770,7 @@ class UploadManager extends EventEmitter { const timeDelta = (now - lastSpeedTime) / 1000; if (Number.isFinite(timeDelta) && timeDelta >= 1) { const bytesDelta = bytesUploaded - lastBytes; - currentSpeedKbs = Math.round(bytesDelta / timeDelta / 1024); + currentSpeedKbs = Math.round(bytesDelta / timeDelta / 1000); lastBytes = bytesUploaded; lastSpeedTime = now; } @@ -782,7 +782,7 @@ class UploadManager extends EventEmitter { lastEmitTime = now; const remaining = currentSpeedKbs > 0 - ? Math.round((bytesTotal - bytesUploaded) / (currentSpeedKbs * 1024)) + ? Math.round((bytesTotal - bytesUploaded) / (currentSpeedKbs * 1000)) : 0; this._emitProgress(uploadId, fileName, task.hoster, { accountId: task.accountId, @@ -1105,7 +1105,7 @@ class UploadManager extends EventEmitter { const now = Date.now(); const timeDelta = (now - lastSpeedTime) / 1000; if (timeDelta >= 1) { - currentSpeedKbs = Math.round((bytesUploaded - lastBytes) / timeDelta / 1024); + currentSpeedKbs = Math.round((bytesUploaded - lastBytes) / timeDelta / 1000); lastBytes = bytesUploaded; lastSpeedTime = now; } @@ -1114,7 +1114,7 @@ class UploadManager extends EventEmitter { if (now - lastEmitTime < PROGRESS_EMIT_INTERVAL) return; lastEmitTime = now; const elapsed = Math.round((now - jobStart) / 1000); - const remaining = currentSpeedKbs > 0 ? Math.round((bytesTotal - bytesUploaded) / (currentSpeedKbs * 1024)) : 0; + const remaining = currentSpeedKbs > 0 ? Math.round((bytesTotal - bytesUploaded) / (currentSpeedKbs * 1000)) : 0; this._emitProgress(uploadId, fileName, task.hoster, { accountId: task.accountId, jobId, status: 'uploading', progress: bytesTotal > 0 ? Math.min(1, bytesUploaded / bytesTotal) : 0, @@ -1123,7 +1123,7 @@ class UploadManager extends EventEmitter { }); }; - const hosterThrottle = settings.maxSpeedKbs > 0 ? new Throttle(settings.maxSpeedKbs * 1024) : null; + const hosterThrottle = settings.maxSpeedKbs > 0 ? new Throttle(settings.maxSpeedKbs * 1000) : null; const globalThrottle = this._getGlobalThrottle(); const throttle = hosterThrottle && globalThrottle ? { consume: async (bytes, sig) => { await hosterThrottle.consume(bytes, sig); await globalThrottle.consume(bytes, sig); } } @@ -1259,7 +1259,7 @@ class UploadManager extends EventEmitter { const now = Date.now(); const timeDelta = (now - lastSpeedTime) / 1000; if (timeDelta >= 1) { - currentSpeedKbs = Math.round((bytesUploaded - lastBytes) / timeDelta / 1024); + currentSpeedKbs = Math.round((bytesUploaded - lastBytes) / timeDelta / 1000); lastBytes = bytesUploaded; lastSpeedTime = now; } @@ -1268,7 +1268,7 @@ class UploadManager extends EventEmitter { if (now - lastEmitTime < PROGRESS_EMIT_INTERVAL) return; lastEmitTime = now; const elapsed = Math.round((now - jobStart) / 1000); - const remaining = currentSpeedKbs > 0 ? Math.round((bytesTotal - bytesUploaded) / (currentSpeedKbs * 1024)) : 0; + const remaining = currentSpeedKbs > 0 ? Math.round((bytesTotal - bytesUploaded) / (currentSpeedKbs * 1000)) : 0; this._emitProgress(uploadId, fileName, task.hoster, { accountId: task.accountId, jobId, status: 'uploading', progress: bytesTotal > 0 ? Math.min(1, bytesUploaded / bytesTotal) : 0, @@ -1276,7 +1276,7 @@ class UploadManager extends EventEmitter { elapsed, remaining, error: null, result: null, attempt: 1, maxAttempts: 1 }); }; - const hosterThrottle = settings.maxSpeedKbs > 0 ? new Throttle(settings.maxSpeedKbs * 1024) : null; + const hosterThrottle = settings.maxSpeedKbs > 0 ? new Throttle(settings.maxSpeedKbs * 1000) : null; const globalThrottle = this._getGlobalThrottle(); const throttle = hosterThrottle && globalThrottle ? { consume: async (bytes, sig) => { await hosterThrottle.consume(bytes, sig); await globalThrottle.consume(bytes, sig); } } diff --git a/main.js b/main.js index 642ce48..9f9b15e 100644 --- a/main.js +++ b/main.js @@ -2802,7 +2802,7 @@ ipcMain.handle('start-upload', async (_event, payload) => { if (!data || typeof data !== 'object') return; safeSend('upload-stats', data); if (data.state === 'uploading' && data.activeJobs > 0) { - const speedMb = ((Number(data.globalSpeedKbs) || 0) / 1024).toFixed(1); + const speedMb = ((Number(data.globalSpeedKbs) || 0) / 1000).toFixed(1); updateTrayTooltip(`Upload: ${data.activeJobs} aktiv - ${speedMb} MB/s`); _maybeLogEventLoopDelay(data.activeJobs); } else { diff --git a/renderer/app.js b/renderer/app.js index c12e187..09ca585 100644 --- a/renderer/app.js +++ b/renderer/app.js @@ -1953,7 +1953,7 @@ function _syncMenuSettings() { const sInput = document.getElementById('menuSpeedInput'); const sSpinner = document.getElementById('menuSpeedSpinner'); if (sCheck) sCheck.checked = enabled; - if (sInput) sInput.value = enabled ? String(+(speedKbs / 1024).toFixed(2)) : '0'; + if (sInput) sInput.value = enabled ? String(+(speedKbs / 1000).toFixed(2)) : '0'; if (sSpinner) sSpinner.classList.toggle('disabled', !enabled); } @@ -1968,12 +1968,12 @@ function _menuSaveParallel(v) { } function _menuSaveSpeedMbs(mbs, enabled) { - const kbs = enabled ? Math.max(0, Math.round((parseFloat(mbs) || 0) * 1024)) : 0; + const kbs = enabled ? Math.max(0, Math.round((parseFloat(mbs) || 0) * 1000)) : 0; const gs = { ...(config.globalSettings || {}), globalMaxSpeedKbs: kbs }; config.globalSettings = gs; saveGlobalSettingsTracked(gs).catch(() => {}); const mirror = document.getElementById('globalMaxSpeedMbsInput'); - if (mirror) mirror.value = kbs > 0 ? String(+(kbs / 1024).toFixed(2)) : '0'; + if (mirror) mirror.value = kbs > 0 ? String(+(kbs / 1000).toFixed(2)) : '0'; } function _initMenuSettingsControls() { @@ -6184,7 +6184,7 @@ function drawUploadSpeedSparkline() { function updateUploadSpeedSparkline() { const speedKbs = Math.max(0, Number(lastUploadStats.globalSpeedKbs) || 0); - window.SpeedHistory.updateSpeedHistory(uploadSpeedState, speedKbs * 1024); + window.SpeedHistory.updateSpeedHistory(uploadSpeedState, speedKbs * 1000); updateUploadSpeedDisplays(); drawUploadSpeedSparkline(); } @@ -6215,7 +6215,7 @@ function updateStatusBar() { const stats = _computeQueueStats(); const etaSeconds = lastUploadStats.globalSpeedKbs > 0 - ? Math.round(stats.bytesRemaining / (lastUploadStats.globalSpeedKbs * 1024)) + ? Math.round(stats.bytesRemaining / (lastUploadStats.globalSpeedKbs * 1000)) : 0; _setRollingUploadMetric('uploadTelemetryTotal', stats.total); @@ -6669,7 +6669,7 @@ function renderSettings() {