-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: http download retries too many times (#849)
- Loading branch information
Showing
5 changed files
with
164 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package http | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"time" | ||
) | ||
|
||
type TimeoutReader struct { | ||
reader io.Reader | ||
timeout time.Duration | ||
} | ||
|
||
func NewTimeoutReader(r io.Reader, timeout time.Duration) *TimeoutReader { | ||
return &TimeoutReader{ | ||
reader: r, | ||
timeout: timeout, | ||
} | ||
} | ||
|
||
func (tr *TimeoutReader) Read(p []byte) (n int, err error) { | ||
ctx, cancel := context.WithTimeout(context.Background(), tr.timeout) | ||
defer cancel() | ||
|
||
done := make(chan struct{}) | ||
var readErr error | ||
var bytesRead int | ||
|
||
go func() { | ||
bytesRead, readErr = tr.reader.Read(p) | ||
close(done) | ||
}() | ||
|
||
select { | ||
case <-done: | ||
return bytesRead, readErr | ||
case <-ctx.Done(): | ||
return 0, ctx.Err() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package http | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"io" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestTimeoutReader_Read(t *testing.T) { | ||
data := []byte("Hello, World!") | ||
reader := bytes.NewReader(data) | ||
timeoutReader := NewTimeoutReader(reader, 1*time.Second) | ||
|
||
buf := make([]byte, len(data)) | ||
n, err := timeoutReader.Read(buf) | ||
if err != nil { | ||
t.Fatalf("expected no error, got %v", err) | ||
} | ||
if n != len(data) { | ||
t.Fatalf("expected to read %d bytes, read %d", len(data), n) | ||
} | ||
if !bytes.Equal(buf, data) { | ||
t.Fatalf("expected %s, got %s", data, buf) | ||
} | ||
} | ||
|
||
func TestTimeoutReader_ReadTimeout(t *testing.T) { | ||
reader := &slowReader{delay: 2 * time.Second} | ||
timeoutReader := NewTimeoutReader(reader, 1*time.Second) | ||
|
||
buf := make([]byte, 8192) | ||
_, err := timeoutReader.Read(buf) | ||
if err == nil { | ||
t.Fatal("expected timeout error, got nil") | ||
} | ||
if !errors.Is(err, context.DeadlineExceeded) { | ||
t.Fatalf("expected %v, got %v", context.DeadlineExceeded, err) | ||
} | ||
} | ||
|
||
type slowReader struct { | ||
delay time.Duration | ||
} | ||
|
||
func (sr *slowReader) Read(p []byte) (n int, err error) { | ||
time.Sleep(sr.delay) | ||
return 0, io.EOF | ||
} |
Oops, something went wrong.