Skip to content

Commit dc16aa7

Browse files
author
tanq
committed
custom dialer only for high thread mode
1 parent 7b7a9bd commit dc16aa7

File tree

7 files changed

+29
-51
lines changed

7 files changed

+29
-51
lines changed

cmd/root.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ var rootCmd = &cobra.Command{
3838
if len(args) == 0 && urlListFile == "" {
3939
log.Fatal().Msg("No URL or URL list provided")
4040
}
41-
url := args[0]
42-
if _, err := u.Parse(url); err != nil {
43-
log.Fatal().Err(err).Msg("Invalid URL format")
41+
if urlListFile != "" && len(args) > 0 {
42+
log.Fatal().Msg("Cannot specify url argument and --urllist together, choose one")
4443
}
45-
if urlListFile != "" && url != "" {
46-
log.Fatal().Msg("Cannot specify both argument and --urllist, choose one")
44+
url := ""
45+
if len(args) > 1 {
46+
url = args[0]
47+
if _, err := u.Parse(url); err != nil {
48+
log.Fatal().Err(err).Msg("Invalid URL format")
49+
}
4750
}
4851

4952
// Handle single URL download

internal/downloader.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package internal
22

33
import (
44
"fmt"
5+
"net/http"
56
"os"
67
"path/filepath"
78
"sync"
@@ -51,11 +52,11 @@ func BatchDownload(entries []DownloadEntry, numLinks int, connectionsPerLink int
5152
ProxyURL: proxyURL,
5253
}
5354
progressCh := make(chan int64)
54-
client := createHTTPClient(config.Timeout, config.KATimeout, config.ProxyURL)
55+
useHighThreadMode := config.Connections > 6
56+
client := createHTTPClient(config.Timeout, config.KATimeout, config.ProxyURL, useHighThreadMode)
5557
fileSize, err := getFileSize(config.URL, config.UserAgent, client)
5658

5759
if err == ErrRangeRequestsNotSupported {
58-
// file size unknown, so can't show progress; so track bytes downloaded
5960
logger.Debug().Str("url", entry.URL).Msg("Range requests not supported, using simple download")
6061
progressManager.Register(entry.OutputPath, -1) // -1 means unknown size
6162
} else if err != nil {
@@ -82,9 +83,8 @@ func BatchDownload(entries []DownloadEntry, numLinks int, connectionsPerLink int
8283
if err == ErrRangeRequestsNotSupported {
8384
err = performSimpleDownload(entry.URL, entry.OutputPath, client, config.UserAgent, progressCh)
8485
} else {
85-
err = downloadWithProgress(config, fileSize, progressCh)
86+
err = downloadWithProgress(config, client, fileSize, progressCh)
8687
}
87-
// close(progressCh)
8888
progressWg.Wait()
8989

9090
if err != nil {
@@ -110,9 +110,9 @@ func BatchDownload(entries []DownloadEntry, numLinks int, connectionsPerLink int
110110
return nil
111111
}
112112

113-
func downloadWithProgress(config DownloadConfig, fileSize int64, progressCh chan<- int64) error {
113+
func downloadWithProgress(config DownloadConfig, client *http.Client, fileSize int64, progressCh chan<- int64) error {
114114
log := GetLogger("download-worker")
115-
client := createHTTPClient(config.Timeout, config.KATimeout, config.ProxyURL)
115+
// client := createHTTPClient(config.Timeout, config.KATimeout, config.ProxyURL)
116116
log.Debug().Str("size", formatBytes(uint64(fileSize))).Msg("File size determined")
117117
job := DownloadJob{
118118
Config: config,

internal/helpers.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"gopkg.in/yaml.v3"
1616
)
1717

18-
const bufferSize = 1024 * 1024 * 2 // 2MB buffer
18+
const bufferSize = 1024 * 1024 * 8 // 8MB buffer
1919
const ToolUserAgent = "danzo/1337"
2020

2121
var chunkIDRegex = regexp.MustCompile(`\.part(\d+)$`)
@@ -108,7 +108,7 @@ func ReadDownloadList(filePath string) ([]DownloadEntry, error) {
108108
return entries, nil
109109
}
110110

111-
func createHTTPClient(timeout time.Duration, keepAliveTO time.Duration, proxyURL string) *http.Client {
111+
func createHTTPClient(timeout time.Duration, keepAliveTO time.Duration, proxyURL string, highThreadMode bool) *http.Client {
112112
transport := &http.Transport{
113113
MaxIdleConns: 100,
114114
MaxIdleConnsPerHost: 100, // for connection reuse
@@ -118,7 +118,9 @@ func createHTTPClient(timeout time.Duration, keepAliveTO time.Duration, proxyURL
118118
// These two seem to reduce performance drastically with custom dial context
119119
// DisableKeepAlives: false,
120120
// ForceAttemptHTTP2: true,
121-
DialContext: (&net.Dialer{
121+
}
122+
if highThreadMode {
123+
transport.DialContext = (&net.Dialer{
122124
Timeout: 30 * time.Second,
123125
KeepAlive: 30 * time.Second,
124126
DualStack: true,
@@ -128,7 +130,7 @@ func createHTTPClient(timeout time.Duration, keepAliveTO time.Duration, proxyURL
128130
setSocketOptions(fd)
129131
})
130132
},
131-
}).DialContext,
133+
}).DialContext
132134
}
133135
if proxyURL != "" {
134136
proxyURLParsed, err := url.Parse(proxyURL)

internal/operations.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,37 +59,6 @@ func parseContentLength(contentLength string) (int64, error) {
5959
return size, nil
6060
}
6161

62-
// func getFileSize(url string, userAgent string, client *http.Client) (int64, error) {
63-
// log := GetLogger("filesize")
64-
// req, err := http.NewRequest("HEAD", url, nil)
65-
// if err != nil {
66-
// return 0, err
67-
// }
68-
// req.Header.Set("User-Agent", userAgent)
69-
// log.Debug().Str("url", url).Msg("Sending HEAD request")
70-
// resp, err := client.Do(req)
71-
// if err != nil {
72-
// return 0, err
73-
// }
74-
// defer resp.Body.Close()
75-
// if resp.Header.Get("Accept-Ranges") != "bytes" {
76-
// return 0, errors.New("server doesn't support range requests")
77-
// }
78-
// contentLength := resp.Header.Get("Content-Length")
79-
// if contentLength == "" {
80-
// return 0, errors.New("server didn't provide Content-Length header")
81-
// }
82-
// size, err := strconv.ParseInt(contentLength, 10, 64)
83-
// if err != nil {
84-
// return 0, fmt.Errorf("invalid content length: %v", err)
85-
// }
86-
// if size <= 0 {
87-
// return 0, errors.New("invalid file size reported by server")
88-
// }
89-
// log.Debug().Int64("bytes", size).Msg("File size determined")
90-
// return size, nil
91-
// }
92-
9362
func downloadChunk(job *DownloadJob, chunk *DownloadChunk, client *http.Client, wg *sync.WaitGroup, progressCh chan<- int64, mutex *sync.Mutex) {
9463
log := GetLogger("chunk").With().Int("chunkId", chunk.ID).Logger()
9564
defer wg.Done()

internal/simple-downloader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func SimpleDownload(url string, outputPath string) error {
1919
progressManager.ShowSummary()
2020
}()
2121

22-
client := createHTTPClient(3*time.Minute, 90*time.Second, "")
22+
client := createHTTPClient(3*time.Minute, 90*time.Second, "", false)
2323
progressCh := make(chan int64)
2424

2525
req, err := http.NewRequest("HEAD", url, nil)

internal/socket-unix.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
)
88

99
func setSocketOptions(fd uintptr) {
10-
syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_RCVBUF, 1024*1024)
11-
syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_SNDBUF, 1024*1024)
10+
syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_NODELAY, 1) // Disable Nagle's algorithm
11+
// syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_WINDOW_CLAMP, 2*bufferSize)
12+
// syscall.SetsockoptString(int(fd), syscall.IPPROTO_TCP, syscall.TCP_CONGESTION, "cubic")
13+
syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_RCVBUF, bufferSize)
14+
syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_SNDBUF, bufferSize)
1215
}

internal/socket-windows.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
)
88

99
func setSocketOptions(fd uintptr) {
10-
syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_RCVBUF, 1024*1024)
11-
syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_SNDBUF, 1024*1024)
10+
syscall.SetsockoptInt(syscall.Handle(fd), syscall.IPPROTO_TCP, syscall.TCP_NODELAY, 1) // Disable Nagle's algorithm
11+
syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_RCVBUF, bufferSize)
12+
syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_SNDBUF, bufferSize)
1213
}

0 commit comments

Comments
 (0)