Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9c4f900

Browse files
committed
下载速度优化
1 parent c602586 commit 9c4f900

File tree

2 files changed

+814
-476
lines changed

2 files changed

+814
-476
lines changed

src/http/controller/download.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net/http"
1212
"regexp"
1313
"strings"
14+
"time"
1415

1516
"github.com/labstack/echo"
1617
"github.com/polaris1119/config"
@@ -38,28 +39,40 @@ func (DownloadController) GoDl(ctx echo.Context) error {
3839

3940
var filenameReg = regexp.MustCompile(`\d+\.\d[a-z\.]*\d+`)
4041

41-
func (DownloadController) FetchGoInstallPackage(ctx echo.Context) error {
42+
func (self DownloadController) FetchGoInstallPackage(ctx echo.Context) error {
4243
filename := ctx.Param("filename")
4344

4445
officalUrl := GoStoragePrefix + filename
45-
resp, err := http.Head(officalUrl)
46+
resp, err := self.headWithTimeout(officalUrl)
4647
if err == nil && resp.StatusCode == http.StatusOK {
48+
resp.Body.Close()
4749
return ctx.Redirect(http.StatusSeeOther, officalUrl)
4850
}
51+
resp.Body.Close()
4952

5053
goVersion := filenameReg.FindString(filename)
5154
filePath := fmt.Sprintf("go/%s/%s", goVersion, filename)
5255

5356
dlUrls := strings.Split(config.ConfigFile.MustValue("download", "dl_urls"), ",")
5457
for _, dlUrl := range dlUrls {
5558
dlUrl += filePath
56-
resp, err = http.Head(dlUrl)
59+
resp, err = self.headWithTimeout(dlUrl)
5760
if err == nil && resp.StatusCode == http.StatusOK {
61+
resp.Body.Close()
5862
return ctx.Redirect(http.StatusSeeOther, dlUrl)
5963
}
64+
resp.Body.Close()
6065
}
6166

6267
getLogger(ctx).Infoln("download:", filename, "from the site static directory")
6368

6469
return ctx.Redirect(http.StatusSeeOther, "/static/"+filePath)
6570
}
71+
72+
func (DownloadController) headWithTimeout(dlUrl string) (*http.Response, error) {
73+
client := http.Client{
74+
Timeout: 2 * time.Second,
75+
}
76+
77+
return client.Head(dlUrl)
78+
}

0 commit comments

Comments
 (0)