Use ctxio package for context aware downloads

This commit is contained in:
Xpl0itU 2024-04-14 12:37:25 +02:00
parent 770d8e45f9
commit a30fb575b2
3 changed files with 13 additions and 1 deletions

View file

@ -12,6 +12,7 @@ import (
"strings" "strings"
"time" "time"
ctxio "github.com/jbenet/go-context/io"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"golang.org/x/sync/semaphore" "golang.org/x/sync/semaphore"
) )
@ -82,7 +83,9 @@ func downloadFileWithSemaphore(ctx context.Context, progressReporter ProgressRep
progressReporter.SetTotalDownloadedForFile(basePath, 0) progressReporter.SetTotalDownloadedForFile(basePath, 0)
writerProgress := newWriterProgress(file, progressReporter, basePath) writerProgress := newWriterProgress(file, progressReporter, basePath)
_, err = io.Copy(writerProgress, resp.Body) writerProgressWithContext := ctxio.NewWriter(ctx, writerProgress)
bodyReaderWithContext := ctxio.NewReader(ctx, resp.Body)
_, err = io.Copy(writerProgressWithContext, bodyReaderWithContext)
if err != nil { if err != nil {
file.Close() file.Close()
resp.Body.Close() resp.Body.Close()

5
go.mod
View file

@ -9,6 +9,11 @@ require (
golang.org/x/crypto v0.22.0 golang.org/x/crypto v0.22.0
) )
require (
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99
golang.org/x/net v0.24.0 // indirect
)
require ( require (
github.com/TheTitanrain/w32 v0.0.0-20200114052255-2654d97dbd3d // indirect github.com/TheTitanrain/w32 v0.0.0-20200114052255-2654d97dbd3d // indirect
golang.org/x/sync v0.7.0 golang.org/x/sync v0.7.0

4
go.sum
View file

@ -6,7 +6,11 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/gotk3/gotk3 v0.6.3 h1:+Ke4WkM1TQUNOlM2TZH6szqknqo+zNbX3BZWVXjSHYw= github.com/gotk3/gotk3 v0.6.3 h1:+Ke4WkM1TQUNOlM2TZH6szqknqo+zNbX3BZWVXjSHYw=
github.com/gotk3/gotk3 v0.6.3/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q= github.com/gotk3/gotk3 v0.6.3/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=