diff options
author | zeripath <art27@cantab.net> | 2021-11-30 20:06:32 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-30 20:06:32 +0000 |
commit | 01087e9eef21ff5ea1cebbb1e84933954671fdf2 (patch) | |
tree | ae618785a3bd46e012096683e2fd2309f87c571d /modules/git/blame.go | |
parent | d894c90b703ce215e2319ae2e2bf95989f77805d (diff) | |
download | gitea-01087e9eef21ff5ea1cebbb1e84933954671fdf2.tar.gz gitea-01087e9eef21ff5ea1cebbb1e84933954671fdf2.zip |
Make Requests Processes and create process hierarchy. Associate OpenRepository with context. (#17125)
This PR registers requests with the process manager and manages hierarchy within the processes.
Git repos are then associated with a context, (usually the request's context) - with sub commands using this context as their base context.
Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'modules/git/blame.go')
-rw-r--r-- | modules/git/blame.go | 39 |
1 files changed, 19 insertions, 20 deletions
diff --git a/modules/git/blame.go b/modules/git/blame.go index fcbf183981..b30124594d 100644 --- a/modules/git/blame.go +++ b/modules/git/blame.go @@ -24,12 +24,12 @@ type BlamePart struct { // BlameReader returns part of file blame one by one type BlameReader struct { - cmd *exec.Cmd - pid int64 - output io.ReadCloser - reader *bufio.Reader - lastSha *string - cancel context.CancelFunc + cmd *exec.Cmd + output io.ReadCloser + reader *bufio.Reader + lastSha *string + cancel context.CancelFunc // Cancels the context that this reader runs in + finished process.FinishedFunc // Tells the process manager we're finished and it can remove the associated process from the process table } var shaLineRegex = regexp.MustCompile("^([a-z0-9]{40})") @@ -100,8 +100,8 @@ func (r *BlameReader) NextPart() (*BlamePart, error) { // Close BlameReader - don't run NextPart after invoking that func (r *BlameReader) Close() error { - defer process.GetManager().Remove(r.pid) - r.cancel() + defer r.finished() // Only remove the process from the process table when the underlying command is closed + r.cancel() // However, first cancel our own context early _ = r.output.Close() @@ -114,7 +114,7 @@ func (r *BlameReader) Close() error { // CreateBlameReader creates reader for given repository, commit and file func CreateBlameReader(ctx context.Context, repoPath, commitID, file string) (*BlameReader, error) { - gitRepo, err := OpenRepository(repoPath) + gitRepo, err := OpenRepositoryCtx(ctx, repoPath) if err != nil { return nil, err } @@ -125,32 +125,31 @@ func CreateBlameReader(ctx context.Context, repoPath, commitID, file string) (*B func createBlameReader(ctx context.Context, dir string, command ...string) (*BlameReader, error) { // Here we use the provided context - this should be tied to the request performing the blame so that it does not hang around. - ctx, cancel := context.WithCancel(ctx) + ctx, cancel, finished := process.GetManager().AddContext(ctx, fmt.Sprintf("GetBlame [repo_path: %s]", dir)) + cmd := exec.CommandContext(ctx, command[0], command[1:]...) cmd.Dir = dir cmd.Stderr = os.Stderr stdout, err := cmd.StdoutPipe() if err != nil { - defer cancel() + defer finished() return nil, fmt.Errorf("StdoutPipe: %v", err) } if err = cmd.Start(); err != nil { - defer cancel() + defer finished() + _ = stdout.Close() return nil, fmt.Errorf("Start: %v", err) } - pid := process.GetManager().Add(fmt.Sprintf("GetBlame [repo_path: %s]", dir), cancel) - reader := bufio.NewReader(stdout) return &BlameReader{ - cmd, - pid, - stdout, - reader, - nil, - cancel, + cmd: cmd, + output: stdout, + reader: reader, + cancel: cancel, + finished: finished, }, nil } |