mirror of
https://github.com/logos-storage/logos-storage-go.git
synced 2026-01-04 06:13:07 +00:00
refactors GotManifest cancellation mechanism to match the pattern used in other places
This commit is contained in:
parent
05ab491bca
commit
37bd27abf1
2
.gitignore
vendored
2
.gitignore
vendored
@ -13,6 +13,8 @@ output.bin
|
|||||||
*.coverprofile
|
*.coverprofile
|
||||||
coverage*.txt
|
coverage*.txt
|
||||||
coverage*.out
|
coverage*.out
|
||||||
|
coverage*.html
|
||||||
|
coverage*.cov
|
||||||
|
|
||||||
# OS files
|
# OS files
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|||||||
@ -42,33 +42,28 @@ func NewCodexIndexDownloader(codexClient CodexClientInterface, indexCid string,
|
|||||||
func (d *CodexIndexDownloader) GotManifest() <-chan struct{} {
|
func (d *CodexIndexDownloader) GotManifest() <-chan struct{} {
|
||||||
ch := make(chan struct{})
|
ch := make(chan struct{})
|
||||||
|
|
||||||
|
// Create cancellable context
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
|
// Monitor for cancellation in separate goroutine
|
||||||
go func() {
|
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
|
// Reset datasetSize to 0 to indicate no successful fetch yet
|
||||||
d.mu.Lock()
|
d.mu.Lock()
|
||||||
d.datasetSize = 0
|
d.datasetSize = 0
|
||||||
d.downloadError = nil
|
d.downloadError = nil
|
||||||
d.mu.Unlock()
|
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
|
// Fetch manifest from Codex
|
||||||
manifest, err := d.codexClient.FetchManifestWithContext(ctx, d.indexCid)
|
manifest, err := d.codexClient.FetchManifestWithContext(ctx, d.indexCid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user