From 37bd27abf1fdea4f53920ede7eca30a23145d54f Mon Sep 17 00:00:00 2001 From: Marcin Czenko Date: Fri, 24 Oct 2025 05:13:37 +0200 Subject: [PATCH] refactors GotManifest cancellation mechanism to match the pattern used in other places --- .gitignore | 2 ++ communities/codex_index_downloader.go | 35 ++++++++++++--------------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 0eb9dae..d8d1dd4 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,8 @@ output.bin *.coverprofile coverage*.txt coverage*.out +coverage*.html +coverage*.cov # OS files .DS_Store diff --git a/communities/codex_index_downloader.go b/communities/codex_index_downloader.go index 71ca991..fbe5333 100644 --- a/communities/codex_index_downloader.go +++ b/communities/codex_index_downloader.go @@ -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 {