Skip to content

Commit

Permalink
Fixing lock issues. (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 committed Sep 22, 2021
1 parent 0e11d2f commit 58d27b9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pkg/webhooks/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
10 changes: 6 additions & 4 deletions pkg/webhooks/github/actions/aggregate_releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type AggregateReleases struct {
repoURL string
repoName string
pullRequestTitle string

lock *multilock.Lock
}

type AggregateReleaseParams struct {
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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"),
Expand Down
7 changes: 6 additions & 1 deletion pkg/webhooks/github/actions/distribute_releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() })

Expand Down
10 changes: 6 additions & 4 deletions pkg/webhooks/github/actions/yaml_translate_releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type yamlTranslateReleases struct {
repoURL string
repoName string
pullRequestTitle string

lock *multilock.Lock
}

type yamlTranslation struct {
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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"),
Expand Down

0 comments on commit 58d27b9

Please sign in to comment.