Skip to content

Commit 10b56e1

Browse files
committed
Make Queue requests cancelable
Signed-off-by: Andrew Thornton <[email protected]>
1 parent 326cb03 commit 10b56e1

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

modules/process/manager.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ func (pm *Manager) Add(description string, cancel context.CancelFunc) int64 {
7070
return pid
7171
}
7272

73+
// AddContext adds a process to the ProcessManager using a base context and returns a context, cancel func and its PID
74+
func (pm *Manager) AddContext(baseCtx context.Context, description string) (context.Context, context.CancelFunc, int64) {
75+
ctx, cancel := context.WithCancel(baseCtx)
76+
pid := pm.Add(description, cancel)
77+
78+
return ctx, func() {
79+
defer pm.Remove(pid)
80+
cancel()
81+
}, pid
82+
}
83+
7384
// Remove a process from the ProcessManager.
7485
func (pm *Manager) Remove(pid int64) {
7586
pm.mutex.Lock()

modules/repository/last_commit_queue_gogit.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
package repository
88

99
import (
10+
"context"
11+
"fmt"
1012
"path"
1113
"path/filepath"
1214
"sync"
@@ -15,6 +17,7 @@ import (
1517
"code.gitea.io/gitea/modules/git"
1618
"code.gitea.io/gitea/modules/graceful"
1719
"code.gitea.io/gitea/modules/log"
20+
"code.gitea.io/gitea/modules/process"
1821
"code.gitea.io/gitea/modules/queue"
1922
"code.gitea.io/gitea/modules/setting"
2023
)
@@ -33,6 +36,9 @@ type CommitCacheRequest struct {
3336

3437
// Do runs the cache request uniquely ensuring that only one cache request is running for this request triple
3538
func (req *CommitCacheRequest) Do() error {
39+
ctx, cancel, _ := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("Cache: %s:%s:%s:%t", req.Repo, req.CommitID, req.TreePath, req.Recursive))
40+
defer cancel()
41+
3642
recursive := req.Recursive
3743
req.Recursive = false
3844

@@ -52,8 +58,14 @@ func (req *CommitCacheRequest) Do() error {
5258

5359
directories := []string{req.TreePath}
5460
for len(directories) > 0 {
61+
select {
62+
case <-ctx.Done():
63+
return ctx.Err()
64+
default:
65+
}
66+
5567
req.TreePath = directories[len(directories)-1]
56-
next, err := req.doTree(repo, commit, recursive, lccache)
68+
next, err := req.doTree(ctx, repo, commit, recursive, lccache)
5769
if err != nil {
5870
return err
5971
}
@@ -62,8 +74,7 @@ func (req *CommitCacheRequest) Do() error {
6274
return nil
6375
}
6476

65-
func (req *CommitCacheRequest) doTree(repo *git.Repository, commit *git.Commit, recursive bool, lccache *git.LastCommitCache) ([]string, error) {
66-
77+
func (req *CommitCacheRequest) doTree(ctx context.Context, repo *git.Repository, commit *git.Commit, recursive bool, lccache *git.LastCommitCache) ([]string, error) {
6778
tree, err := commit.Tree.SubTree(req.TreePath)
6879
if err != nil {
6980
if git.IsErrNotExist(err) {
@@ -125,7 +136,7 @@ func (req *CommitCacheRequest) doTree(repo *git.Repository, commit *git.Commit,
125136
return directories, nil
126137
}
127138

128-
commits, err := git.GetLastCommitForPaths(graceful.GetManager().HammerContext(), commitNode, req.TreePath, entryPaths)
139+
commits, err := git.GetLastCommitForPaths(ctx, commitNode, req.TreePath, entryPaths)
129140
if err != nil {
130141
return nil, err
131142
}

modules/repository/last_commit_queue_nogogit.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
package repository
88

99
import (
10+
"context"
11+
"fmt"
1012
"path"
1113
"path/filepath"
1214
"sync"
@@ -15,6 +17,7 @@ import (
1517
"code.gitea.io/gitea/modules/git"
1618
"code.gitea.io/gitea/modules/graceful"
1719
"code.gitea.io/gitea/modules/log"
20+
"code.gitea.io/gitea/modules/process"
1821
"code.gitea.io/gitea/modules/queue"
1922
"code.gitea.io/gitea/modules/setting"
2023
)
@@ -33,6 +36,9 @@ type CommitCacheRequest struct {
3336

3437
// Do runs the cache request uniquely ensuring that only one cache request is running for this request triple
3538
func (req *CommitCacheRequest) Do() error {
39+
ctx, cancel, _ := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("Cache: %s:%s:%s:%t", req.Repo, req.CommitID, req.TreePath, req.Recursive))
40+
defer cancel()
41+
3642
recursive := req.Recursive
3743
req.Recursive = false
3844

@@ -52,8 +58,14 @@ func (req *CommitCacheRequest) Do() error {
5258

5359
directories := []string{req.TreePath}
5460
for len(directories) > 0 {
61+
select {
62+
case <-ctx.Done():
63+
return ctx.Err()
64+
default:
65+
}
66+
5567
req.TreePath = directories[len(directories)-1]
56-
next, err := req.doTree(repo, commit, recursive, lccache)
68+
next, err := req.doTree(ctx, repo, commit, recursive, lccache)
5769
if err != nil {
5870
return err
5971
}
@@ -62,7 +74,7 @@ func (req *CommitCacheRequest) Do() error {
6274
return nil
6375
}
6476

65-
func (req *CommitCacheRequest) doTree(repo *git.Repository, commit *git.Commit, recursive bool, lccache *git.LastCommitCache) ([]string, error) {
77+
func (req *CommitCacheRequest) doTree(ctx context.Context, repo *git.Repository, commit *git.Commit, recursive bool, lccache *git.LastCommitCache) ([]string, error) {
6678

6779
tree, err := commit.Tree.SubTree(req.TreePath)
6880
if err != nil {
@@ -115,7 +127,7 @@ func (req *CommitCacheRequest) doTree(repo *git.Repository, commit *git.Commit,
115127
return directories, nil
116128
}
117129

118-
commits, err := git.GetLastCommitForPaths(graceful.GetManager().HammerContext(), commit, req.TreePath, entryPaths)
130+
commits, err := git.GetLastCommitForPaths(ctx, commit, req.TreePath, entryPaths)
119131
if err != nil {
120132
return nil, err
121133
}

0 commit comments

Comments
 (0)