refactors GotManifest cancellation mechanism to match the pattern used in other places

This commit is contained in:
Marcin Czenko 2025-10-24 05:13:37 +02:00
parent 05ab491bca
commit 37bd27abf1
No known key found for this signature in database
GPG Key ID: A0449219BDBA98AE
2 changed files with 17 additions and 20 deletions

2
.gitignore vendored
View File

@ -13,6 +13,8 @@ output.bin
*.coverprofile
coverage*.txt
coverage*.out
coverage*.html
coverage*.cov
# OS files
.DS_Store

View File

@ -42,33 +42,28 @@ func NewCodexIndexDownloader(codexClient CodexClientInterface, indexCid string,
func (d *CodexIndexDownloader) GotManifest() <-chan struct{} {
ch := make(chan struct{})
// Create cancellable context
ctx, cancel := context.WithCancel(context.Background())
// Monitor for cancellation in separate goroutine
go func() {
select {
case <-d.cancelChan:
cancel() // Cancel fetch immediately
case <-ctx.Done():
// Context already cancelled, nothing to do
}
}()
go func() {
defer cancel() // Ensure context is cancelled when fetch completes or fails
// Reset datasetSize to 0 to indicate no successful fetch yet
d.mu.Lock()
d.datasetSize = 0
d.downloadError = nil
d.mu.Unlock()
// Check for cancellation before starting
select {
case <-d.cancelChan:
return // Exit without closing channel - cancellation
default:
}
// Create cancellable context for HTTP request
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Monitor for cancellation in separate goroutine
go func() {
select {
case <-d.cancelChan:
cancel()
case <-ctx.Done():
}
}()
// Fetch manifest from Codex
manifest, err := d.codexClient.FetchManifestWithContext(ctx, d.indexCid)
if err != nil {