You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add TryLock for use with optional tasks like ARI updates to reduce lock contention (#357)
* Implement `TryLock` for use with optional tasks like ARI updates to reduce lock contention
* Update maintain.go
---------
Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
@@ -204,7 +217,7 @@ func (s *FileStorage) Lock(ctx context.Context, name string) error {
204
217
select {
205
218
case<-time.After(250*time.Millisecond):
206
219
case<-ctx.Done():
207
-
returnctx.Err()
220
+
returnfalse, ctx.Err()
208
221
}
209
222
continue
210
223
} else {
@@ -215,7 +228,7 @@ func (s *FileStorage) Lock(ctx context.Context, name string) error {
215
228
defaultLogger.Sugar().Infof("[%s] %s: Empty lockfile (%v) - likely previous process crashed or storage medium failure; treating as stale", s, filename, err2)
// lock file is stale - delete it and try again to obtain lock
@@ -237,7 +250,7 @@ func (s *FileStorage) Lock(ctx context.Context, name string) error {
237
250
defaultLogger.Sugar().Infof("[%s] Lock for '%s' is stale (created: %s, last update: %s); removing then retrying: %s", s, name, meta.Created, meta.Updated, filename)
238
251
iferr=os.Remove(filename); err!=nil { // hopefully we can replace the lock file quickly!
239
252
if!errors.Is(err, fs.ErrNotExist) {
240
-
returnfmt.Errorf("unable to delete stale lockfile; deadlocked: %w", err)
253
+
returnfalse, fmt.Errorf("unable to delete stale lockfile; deadlocked: %w", err)
241
254
}
242
255
}
243
256
continue
@@ -249,12 +262,30 @@ func (s *FileStorage) Lock(ctx context.Context, name string) error {
249
262
select {
250
263
case<-time.After(fileLockPollInterval):
251
264
case<-ctx.Done():
252
-
returnctx.Err()
265
+
returnfalse, ctx.Err()
253
266
}
254
267
}
255
268
}
256
269
}
257
270
271
+
// Lock obtains a lock named by the given name. It blocks
272
+
// until the lock can be obtained or an error is returned.
0 commit comments