From 58d27b9afa781221be1e6eed01c126d5ad80ea88 Mon Sep 17 00:00:00 2001 From: Gerrit Date: Wed, 22 Sep 2021 13:55:22 +0200 Subject: [PATCH] Fixing lock issues. (#47) --- pkg/webhooks/constants/constants.go | 2 +- pkg/webhooks/github/actions/aggregate_releases.go | 10 ++++++---- pkg/webhooks/github/actions/distribute_releases.go | 7 ++++++- pkg/webhooks/github/actions/yaml_translate_releases.go | 10 ++++++---- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/pkg/webhooks/constants/constants.go b/pkg/webhooks/constants/constants.go index 9db968b..f3e87b8 100644 --- a/pkg/webhooks/constants/constants.go +++ b/pkg/webhooks/constants/constants.go @@ -6,5 +6,5 @@ const ( // WebhookHandleTimeout is the duration after which a webhook handle function context times out // the entire thing is asynchronuous anyway, so the VCS will get an immediate response, this is just // that we do not have processing of events hanging internally - WebhookHandleTimeout = 60 * time.Second + WebhookHandleTimeout = 120 * time.Second ) diff --git a/pkg/webhooks/github/actions/aggregate_releases.go b/pkg/webhooks/github/actions/aggregate_releases.go index b513d16..f62c4bd 100644 --- a/pkg/webhooks/github/actions/aggregate_releases.go +++ b/pkg/webhooks/github/actions/aggregate_releases.go @@ -28,6 +28,8 @@ type AggregateReleases struct { repoURL string repoName string pullRequestTitle string + + lock *multilock.Lock } type AggregateReleaseParams struct { @@ -90,6 +92,7 @@ func NewAggregateReleases(logger *zap.SugaredLogger, client *clients.Github, raw repoURL: typedConfig.TargetRepositoryURL, repoName: typedConfig.TargetRepositoryName, pullRequestTitle: pullRequestTitle, + lock: multilock.New(typedConfig.TargetRepositoryName), }, nil } @@ -111,9 +114,8 @@ func (r *AggregateReleases) AggregateRelease(ctx context.Context, p *AggregateRe // preventing concurrent git repo modifications var once sync.Once - lock := multilock.New(r.repoName) - lock.Lock() - defer once.Do(func() { lock.Unlock() }) + r.lock.Lock() + defer once.Do(func() { r.lock.Unlock() }) token, err := r.client.GitToken(ctx) if err != nil { @@ -158,7 +160,7 @@ func (r *AggregateReleases) AggregateRelease(ctx context.Context, p *AggregateRe r.logger.Infow("pushed to aggregate target repo", "target-repo", p.RepositoryName, "source-repo", p.RepositoryName, "release", tag, "branch", r.branch, "hash", hash) - once.Do(func() { lock.Unlock() }) + once.Do(func() { r.lock.Unlock() }) pr, _, err := r.client.GetV3Client().PullRequests.Create(ctx, r.client.Organization(), r.repoName, &v3.NewPullRequest{ Title: v3.String("Next release"), diff --git a/pkg/webhooks/github/actions/distribute_releases.go b/pkg/webhooks/github/actions/distribute_releases.go index c30de85..824272d 100644 --- a/pkg/webhooks/github/actions/distribute_releases.go +++ b/pkg/webhooks/github/actions/distribute_releases.go @@ -117,6 +117,12 @@ func (d *distributeReleases) DistributeRelease(ctx context.Context, p *distribut return errors.Wrap(err, "error creating git token") } + var targetRepos []string + for targetRepoName := range d.targetRepos { + targetRepos = append(targetRepos, targetRepoName) + } + lock := multilock.New(targetRepos...) + g, ctx := errgroup.WithContext(ctx) for targetRepoName, targetRepo := range d.targetRepos { targetRepoName := targetRepoName @@ -132,7 +138,6 @@ func (d *distributeReleases) DistributeRelease(ctx context.Context, p *distribut // preventing concurrent git repo modifications var once sync.Once - lock := multilock.New(targetRepoName) lock.Lock() defer once.Do(func() { lock.Unlock() }) diff --git a/pkg/webhooks/github/actions/yaml_translate_releases.go b/pkg/webhooks/github/actions/yaml_translate_releases.go index 7d3cd04..ecb7ba7 100644 --- a/pkg/webhooks/github/actions/yaml_translate_releases.go +++ b/pkg/webhooks/github/actions/yaml_translate_releases.go @@ -28,6 +28,8 @@ type yamlTranslateReleases struct { repoURL string repoName string pullRequestTitle string + + lock *multilock.Lock } type yamlTranslation struct { @@ -116,6 +118,7 @@ func newYAMLTranslateReleases(logger *zap.SugaredLogger, client *clients.Github, repoURL: typedConfig.TargetRepositoryURL, repoName: typedConfig.TargetRepositoryName, pullRequestTitle: pullRequestTitle, + lock: multilock.New(typedConfig.TargetRepositoryName), }, nil } @@ -137,9 +140,8 @@ func (r *yamlTranslateReleases) translateRelease(ctx context.Context, p *yamlTra // preventing concurrent git repo modifications var once sync.Once - lock := multilock.New(r.repoName, p.RepositoryName) - lock.Lock() - defer once.Do(func() { lock.Unlock() }) + r.lock.Lock() + defer once.Do(func() { r.lock.Unlock() }) token, err := r.client.GitToken(ctx) if err != nil { @@ -207,7 +209,7 @@ func (r *yamlTranslateReleases) translateRelease(ctx context.Context, p *yamlTra r.logger.Infow("pushed to translate target repo", "target-repo", p.RepositoryName, "source-repo", p.RepositoryName, "release", tag, "branch", r.branch, "hash", hash) - once.Do(func() { lock.Unlock() }) + once.Do(func() { r.lock.Unlock() }) pr, _, err := r.client.GetV3Client().PullRequests.Create(ctx, r.client.Organization(), r.repoName, &v3.NewPullRequest{ Title: v3.String("Next release"),