Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions go/cmd/gitter/gitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,11 @@ func prepareCmd(ctx context.Context, dir string, env []string, name string, args
}
// Use SIGINT instead of SIGKILL for graceful shutdown of subprocesses
cmd.Cancel = func() error {
logger.DebugContext(ctx, "SIGINT sent to command", slog.String("cmd", name), slog.Any("args", args))
return cmd.Process.Signal(syscall.SIGINT)
}
// Ensure it eventually dies if it ignores SIGINT
cmd.WaitDelay = shutdownTimeout / 2

return cmd
}
Expand Down Expand Up @@ -369,8 +372,15 @@ func gitHandler(w http.ResponseWriter, r *http.Request) {
}
}

semaphore <- struct{}{}
defer func() { <-semaphore }()
select {
case semaphore <- struct{}{}:
defer func() { <-semaphore }()
case <-ctx.Done():
logger.WarnContext(ctx, "Request cancelled while waiting for semaphore")
http.Error(w, "Server context cancelled", http.StatusServiceUnavailable)

return
}
logger.DebugContext(ctx, "Concurrent requests", slog.Int("count", len(semaphore)))

// Fetch repo first
Expand Down Expand Up @@ -441,8 +451,15 @@ func cacheHandler(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), urlKey, url)
logger.InfoContext(ctx, "Received request: /cache")

semaphore <- struct{}{}
defer func() { <-semaphore }()
select {
case semaphore <- struct{}{}:
defer func() { <-semaphore }()
case <-ctx.Done():
logger.WarnContext(ctx, "Request cancelled while waiting for semaphore")
http.Error(w, "Server context cancelled", http.StatusServiceUnavailable)

return
}
logger.DebugContext(ctx, "Concurrent requests", slog.Int("count", len(semaphore)))

// Fetch repo if it's not fresh
Expand Down
12 changes: 12 additions & 0 deletions go/cmd/gitter/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ func (r *Repository) buildCommitGraph(ctx context.Context, cache *pb.RepositoryC

scanner := bufio.NewScanner(file)
for scanner.Scan() {
// Handle context cancel within the loop to exit faster if we're processing a very large repo
if ctx.Err() != nil {
return nil, ctx.Err()
}
// Example of a line of git log output
// 4c9bdbf0e2d45a5297cc080c3ebe809c0cca3581 bc0e4b4c4dbf7932fab7d264929d4d820e82c817 65be82edcdbc5aa6eeea23655cc96b5c84547d3b upstream/master, upstream/HEAD\n
// Corresponds to: commit hash \t parent hashes (space delimited) \t refs (comma delimited)
Expand Down Expand Up @@ -316,6 +320,10 @@ func (r *Repository) calculatePatchIDsWorker(ctx context.Context, chunk []SHA1)
go func() {
defer in.Close()
for _, hash := range chunk {
// Handle context cancel
if ctx.Err() != nil {
return
}
fmt.Fprintf(in, "%s\n", hex.EncodeToString(hash[:]))
}
}()
Expand All @@ -330,6 +338,10 @@ func (r *Repository) calculatePatchIDsWorker(ctx context.Context, chunk []SHA1)
// Read results from stdout of git patch-id
scanner := bufio.NewScanner(out)
for scanner.Scan() {
// Handle context cancel to exit faster as this can be a long process
if ctx.Err() != nil {
return ctx.Err()
}
// Format of git patch-id result: <patch ID> <commit hash>
line := scanner.Text()

Expand Down
Loading