Commit 758185ea
758185ea74d1Merged
Svyatoslav Dumchenko <[email protected]> ยท 4 years ago ยท 1 file ยท ~1 modified
Description
Fix flock usage
Details
- Commit
- 758185ea74d1
- Parent
- a9e680663f21
pkg/plan/apply.gomodifiedView file
@@ -62,6 +62,18 @@
6262
func syncRepositories(repositories repoConfigs) (err error) {6363
log.Trace("๐ helm repository.yaml: ", helper.Helm.RepositoryConfig)6464
65+
// we need to get a flock first66+
lockPath := helper.Helm.RepositoryConfig + ".lock"67+
fileLock := flock.New(lockPath)68+
lockCtx, cancel := context.WithTimeout(context.Background(), 20*time.Second)69+
defer cancel()70+
// We need to unlock in deferred mode in case of any other errors returned71+
defer fileLock.Unlock() //nolint:errcheck // TODO: add error checking72+
locked, err := fileLock.TryLockContext(lockCtx, time.Second)73+
if err != nil && !locked {74+
return fmt.Errorf("failed to get lock %s: %w", fileLock.Path(), err)75+
}76+
6577
var f *helmRepo.File6678
// Create if not exits6779
if !helper.IsExists(helper.Helm.RepositoryConfig) {@@ -78,17 +90,6 @@
7890
}7991
}8092
81-
// Flock82-
lockPath := helper.Helm.RepositoryConfig + ".lock"83-
fileLock := flock.New(lockPath)84-
lockCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)85-
defer cancel()86-
87-
locked, err := fileLock.TryLockContext(lockCtx, time.Second)88-
if err != nil && !locked {89-
return fmt.Errorf("failed to get lock %s: %w", fileLock.Path(), err)90-
}91-
9293
// We cannot parallel repositories installation as helm manages single repositories.yaml.9394
// To prevent data race we need either make helm use futex or not parallel at all9495
for i := range repositories {@@ -103,7 +104,7 @@
103104
return fmt.Errorf("failed to write repositories file: %w", err)104105
}105106
106-
// Unlock107+
// If we haven't met any errors yet unlock the repository file. Deferred unlock will exit quickly after this.107108
if err := fileLock.Unlock(); err != nil {108109
return fmt.Errorf("failed to unlock %s: %w", fileLock.Path(), err)109110
}