mirror of
https://github.com/Xpl0itU/WiiUDownloader.git
synced 2025-05-09 13:52:02 -04:00
Tweak client
This commit is contained in:
parent
f927c43c1b
commit
7b363b9fc9
3 changed files with 31 additions and 8 deletions
|
@ -44,10 +44,21 @@ func main() {
|
|||
}
|
||||
|
||||
client := &fasthttp.Client{
|
||||
MaxConnsPerHost: 100,
|
||||
MaxConnsPerHost: 1024,
|
||||
MaxIdleConnDuration: 30 * time.Second,
|
||||
TLSConfig: nil,
|
||||
ReadBufferSize: wiiudownloader.BUFFER_SIZE,
|
||||
WriteBufferSize: wiiudownloader.BUFFER_SIZE,
|
||||
MaxConnWaitTimeout: 30 * time.Second,
|
||||
StreamResponseBody: true,
|
||||
ConnPoolStrategy: fasthttp.LIFO,
|
||||
ReadTimeout: 30 * time.Second,
|
||||
WriteTimeout: 30 * time.Second,
|
||||
Name: "WiiUDownloader",
|
||||
DialDualStack: true,
|
||||
RetryIf: func(request *fasthttp.Request) bool {
|
||||
return true
|
||||
},
|
||||
}
|
||||
|
||||
app.Connect("activate", func() {
|
||||
|
|
|
@ -15,9 +15,9 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
maxRetries = 5
|
||||
retryDelay = 5 * time.Second
|
||||
bufferSize = 1048576
|
||||
maxRetries = 5
|
||||
retryDelay = 5 * time.Second
|
||||
BUFFER_SIZE = 1048576
|
||||
)
|
||||
|
||||
type ProgressReporter interface {
|
||||
|
@ -62,7 +62,7 @@ func downloadFile(ctx context.Context, progressReporter ProgressReporter, client
|
|||
|
||||
req.SetRequestURI(downloadURL)
|
||||
req.Header.SetMethod("GET")
|
||||
|
||||
req.Header.Set("Accept", "*/*")
|
||||
req.Header.Set("User-Agent", "WiiUDownloader")
|
||||
req.Header.Set("Connection", "Keep-Alive")
|
||||
req.Header.Set("Accept-Encoding", "*")
|
||||
|
@ -72,12 +72,19 @@ func downloadFile(ctx context.Context, progressReporter ProgressReporter, client
|
|||
resp.ImmediateHeaderFlush = true
|
||||
|
||||
if err := client.Do(req, resp); err != nil {
|
||||
req.CloseBodyStream()
|
||||
resp.CloseBodyStream()
|
||||
fasthttp.ReleaseRequest(req)
|
||||
fasthttp.ReleaseResponse(resp)
|
||||
return err
|
||||
if doRetries && attempt < maxRetries {
|
||||
time.Sleep(retryDelay)
|
||||
continue
|
||||
}
|
||||
return fmt.Errorf("inital request error after %d attempts, status code: %s", attempt, err.Error())
|
||||
}
|
||||
|
||||
if resp.StatusCode() != fasthttp.StatusOK {
|
||||
req.CloseBodyStream()
|
||||
resp.CloseBodyStream()
|
||||
fasthttp.ReleaseRequest(req)
|
||||
fasthttp.ReleaseResponse(resp)
|
||||
|
@ -90,6 +97,7 @@ func downloadFile(ctx context.Context, progressReporter ProgressReporter, client
|
|||
|
||||
file, err := os.Create(dstPath)
|
||||
if err != nil {
|
||||
req.CloseBodyStream()
|
||||
resp.CloseBodyStream()
|
||||
fasthttp.ReleaseRequest(req)
|
||||
fasthttp.ReleaseResponse(resp)
|
||||
|
@ -102,6 +110,7 @@ func downloadFile(ctx context.Context, progressReporter ProgressReporter, client
|
|||
|
||||
customBufferedWriter, err := NewFileWriterWithProgress(file, &downloaded, progressReporter)
|
||||
if err != nil {
|
||||
req.CloseBodyStream()
|
||||
resp.CloseBodyStream()
|
||||
file.Close()
|
||||
fasthttp.ReleaseRequest(req)
|
||||
|
@ -111,6 +120,7 @@ func downloadFile(ctx context.Context, progressReporter ProgressReporter, client
|
|||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
req.CloseBodyStream()
|
||||
resp.CloseBodyStream()
|
||||
file.Close()
|
||||
fasthttp.ReleaseRequest(req)
|
||||
|
@ -119,6 +129,7 @@ func downloadFile(ctx context.Context, progressReporter ProgressReporter, client
|
|||
default:
|
||||
err := resp.BodyWriteTo(customBufferedWriter)
|
||||
if err != nil && err != io.EOF {
|
||||
req.CloseBodyStream()
|
||||
resp.CloseBodyStream()
|
||||
file.Close()
|
||||
fasthttp.ReleaseRequest(req)
|
||||
|
@ -132,6 +143,7 @@ func downloadFile(ctx context.Context, progressReporter ProgressReporter, client
|
|||
}
|
||||
}
|
||||
if !isError {
|
||||
req.CloseBodyStream()
|
||||
resp.CloseBodyStream()
|
||||
file.Close()
|
||||
fasthttp.ReleaseRequest(req)
|
||||
|
|
4
utils.go
4
utils.go
|
@ -43,7 +43,7 @@ func NewFileWriterWithProgress(file *os.File, downloaded *int64, progressReporte
|
|||
file: file,
|
||||
downloaded: downloaded,
|
||||
progressReporter: progressReporter,
|
||||
buffer: make([]byte, bufferSize),
|
||||
buffer: make([]byte, BUFFER_SIZE),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -54,7 +54,7 @@ func (bw *BufferedWriter) Write(data []byte) (int, error) {
|
|||
return written, nil
|
||||
}
|
||||
remaining := len(data) - written
|
||||
toWrite := min(bufferSize, uint64(remaining))
|
||||
toWrite := min(BUFFER_SIZE, uint64(remaining))
|
||||
copy(bw.buffer, data[written:written+int(toWrite)])
|
||||
n, err := bw.file.Write(bw.buffer[:toWrite])
|
||||
if err != nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue