feat: drains http body if buffer is too small

Signed-off-by: divinerapier <poriter.coco@gmail.com>
This commit is contained in:
divinerapier 2019-12-26 11:28:43 +08:00
parent 84640d07b7
commit 4cbb6fa199
No known key found for this signature in database
GPG key ID: EA114C69199EE3BB

View file

@ -235,8 +235,16 @@ func ReadUrl(fileUrl string, offset int64, size int, buf []byte, isReadRange boo
if err != nil { if err != nil {
return n, err return n, err
} }
if n == int64(len(buf)) {
break
} }
}
// drains the response body to avoid memory leak
data, err := ioutil.ReadAll(reader)
if len(data) != 0 {
err = fmt.Errorf("buffer size is too small. remains %d", len(data))
}
return n, err
} }
func ReadUrlAsStream(fileUrl string, offset int64, size int, fn func(data []byte)) (int64, error) { func ReadUrlAsStream(fileUrl string, offset int64, size int, fn func(data []byte)) (int64, error) {