konakona/nget
something happened
Commit 4b8576d281cb · konakona · 2026-10-22 17:49 +0700
Comments
No comments yet.
Diff
diff --git a/download.go b/download.go
--- a/download.go
+++ b/download.go
@@ -3,21 +3,45 @@
import (
"fmt"
"io"
+ "net"
"net/http"
"os"
+ "time"
)
+func getOptimizedClient() *http.Client {
+ transport := &http.Transport{
+ Proxy: http.ProxyFromEnvironment,
+ DialContext: (&net.Dialer{
+ Timeout: 10 * time.Second,
+ KeepAlive: 30 * time.Second,
+ }).DialContext,
+ MaxIdleConns: 100,
+ IdleConnTimeout: 90 * time.Second,
+ TLSHandshakeTimeout: 10 * time.Second,
+ ExpectContinueTimeout: 1 * time.Second,
+ ForceAttemptHTTP2: true,
+ }
+
+ return &http.Client{
+ Transport: transport,
+ Timeout: 0,
+ }
+}
+
func DownloadFile(rawURL string) error {
fmt.Printf("Connecting to: %s\n", rawURL)
- resp, err := http.Get(rawURL)
+ client := getOptimizedClient()
+
+ resp, err := client.Get(rawURL)
if err != nil {
- return fmt.Errorf("can't connect to URL: %v", err)
+ return fmt.Errorf("Can't connect to wifi: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
- return fmt.Errorf("server returned an HTTP error status: %s", resp.Status)
+ return fmt.Errorf("Server returned an HTTP error status: %s", resp.Status)
}
filename := ExtractFilename(rawURL)
@@ -25,7 +49,7 @@
out, err := os.Create(filename)
if err != nil {
- return fmt.Errorf("Can't create file: %v", err)
+ return fmt.Errorf("Can't creating file on system: %v", err)
}
defer out.Close()
@@ -36,7 +60,7 @@
_, err = io.Copy(counter, resp.Body)
if err != nil {
- return fmt.Errorf("Error while download file: %v", err)
+ return fmt.Errorf("Error while downloading file: %v", err)
}
fmt.Println("\n[Success] File downloaded")