-
Notifications
You must be signed in to change notification settings - Fork 42
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
feat: make staleness check more robust #74
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
76f2f45
feat: make staleness check more robust
hugomrdias d879cba
fix: fix the mtime handling
hugomrdias c857a79
chore: remove node latest from travis
hugomrdias a66dcaf
fix: feedback
hugomrdias b4e4944
Merge branch 'master' into fix/improve-stale-detection
satazor 1b5f614
fix: feedback
hugomrdias 7ab8b4a
fix: remove tests
hugomrdias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ function acquireLock(file, options, callback) { | |
options.fs.mkdir(getLockFile(file, options), (err) => { | ||
// If successful, we are done | ||
if (!err) { | ||
return callback(); | ||
return options.fs.stat(getLockFile(file, options), callback); | ||
} | ||
|
||
// If error is not EEXIST then some other error occurred while locking | ||
|
@@ -93,33 +93,17 @@ function updateLock(file, options) { | |
|
||
lock.updateDelay = lock.updateDelay || options.update; | ||
lock.updateTimeout = setTimeout(() => { | ||
const mtime = Date.now() / 1000; | ||
|
||
lock.updateTimeout = null; | ||
|
||
options.fs.utimes(getLockFile(file, options), mtime, mtime, (err) => { | ||
// Ignore if the lock was released | ||
if (lock.released) { | ||
return; | ||
} | ||
|
||
// Verify if we are within the stale threshold | ||
if (lock.lastUpdate <= Date.now() - options.stale && lock.lastUpdate > Date.now() - (options.stale * 2)) { | ||
const err = Object.assign( | ||
new Error(lock.updateError || 'Unable to update lock within the stale threshold'), | ||
{ code: 'ECOMPROMISED' } | ||
); | ||
|
||
return setLockAsCompromised(file, lock, err); | ||
} | ||
|
||
// If the file is older than (stale * 2), we assume the clock is moved manually, | ||
// which we consider a valid case | ||
// Check if mtime is still ours if it is we can still recover from a system sleep or a busy event loop | ||
options.fs.stat(getLockFile(file, options), (err, stat) => { | ||
const isMtimeOurs = lock.mtime.getTime() === (stat ? stat.mtime.getTime() : null); | ||
const isOverThreshold = lock.lastUpdate + options.stale < Date.now(); | ||
|
||
// If it failed to update the lockfile, keep trying unless | ||
// the lockfile was deleted! | ||
// the lockfile was deleted or we are over the threshold | ||
if (err) { | ||
if (err.code === 'ENOENT') { | ||
if (err.code === 'ENOENT' || isOverThreshold) { | ||
return setLockAsCompromised(file, lock, Object.assign(err, { code: 'ECOMPROMISED' })); | ||
} | ||
|
||
|
@@ -129,11 +113,46 @@ function updateLock(file, options) { | |
return updateLock(file, options); | ||
} | ||
|
||
// All ok, keep updating.. | ||
lock.lastUpdate = Date.now(); | ||
lock.updateError = null; | ||
lock.updateDelay = null; | ||
updateLock(file, options); | ||
if (!isMtimeOurs) { | ||
return setLockAsCompromised( | ||
file, | ||
lock, | ||
Object.assign( | ||
new Error('Unable to update lock within the stale threshold'), | ||
{ code: 'ECOMPROMISED' } | ||
)); | ||
} | ||
|
||
const mtime = new Date(); | ||
|
||
options.fs.utimes(getLockFile(file, options), mtime, mtime, (err) => { | ||
const isOverThreshold = lock.lastUpdate + options.stale < Date.now(); | ||
|
||
// Ignore if the lock was released | ||
if (lock.released) { | ||
return; | ||
} | ||
|
||
// If it failed to update the lockfile, keep trying unless | ||
// the lockfile was deleted or we are over the threshold | ||
if (err) { | ||
if (err.code === 'ENOENT' || isOverThreshold) { | ||
return setLockAsCompromised(file, lock, Object.assign(err, { code: 'ECOMPROMISED' })); | ||
} | ||
|
||
lock.updateError = err; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are assigned to but never used... |
||
lock.updateDelay = 1000; | ||
|
||
return updateLock(file, options); | ||
} | ||
|
||
// All ok, keep updating.. | ||
lock.mtime = mtime; | ||
lock.lastUpdate = Date.now(); | ||
lock.updateError = null; | ||
lock.updateDelay = null; | ||
updateLock(file, options); | ||
}); | ||
}); | ||
}, lock.updateDelay); | ||
|
||
|
@@ -198,7 +217,7 @@ function lock(file, options, callback) { | |
const operation = retry.operation(options.retries); | ||
|
||
operation.attempt(() => { | ||
acquireLock(file, options, (err) => { | ||
acquireLock(file, options, (err, stat) => { | ||
if (operation.retry(err)) { | ||
return; | ||
} | ||
|
@@ -209,6 +228,7 @@ function lock(file, options, callback) { | |
|
||
// We now own the lock | ||
const lock = locks[file] = { | ||
mtime: stat.mtime, | ||
options, | ||
lastUpdate: Date.now(), | ||
}; | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: personally I'd move this after the error handling block so you don't need to use a ternary 😄