From ad06d8fea3e5540337c96ef457d7b9a1f7dcf6a9 Mon Sep 17 00:00:00 2001 From: Ben Adrian Sarmiento Date: Mon, 17 Jun 2024 17:27:07 +0200 Subject: [PATCH] Add new config - number_of_hosts --- internal/app.go | 4 +-- internal/config/types.go | 61 +++++++++++++++++++++++----------------- pkg/http/ip.go | 13 ++------- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/internal/app.go b/internal/app.go index 22c1440..2f5a74b 100644 --- a/internal/app.go +++ b/internal/app.go @@ -86,7 +86,7 @@ func MainApp(configPath string) { log.Named("unrestrict_client"), ) - hosts := repo.GetOptimalHosts(config.ShouldForceIPv6()) + hosts := repo.GetOptimalHosts(config.GetNumberOfHosts(), config.ShouldForceIPv6()) zurglog.Debugf("Optimal hosts (%d): %v", len(hosts), hosts) downloadClient := http.NewHTTPClient( @@ -99,7 +99,7 @@ func MainApp(configPath string) { log.Named("download_client"), ) - workerPool, err := ants.NewPool(config.GetNumOfWorkers()) + workerPool, err := ants.NewPool(config.GetNumberOfWorkers()) if err != nil { zurglog.Errorf("Failed to create worker pool: %v", err) os.Exit(1) diff --git a/internal/config/types.go b/internal/config/types.go index 1f46879..6c60d0f 100644 --- a/internal/config/types.go +++ b/internal/config/types.go @@ -7,34 +7,35 @@ import ( ) type ConfigInterface interface { - GetConfig() ZurgConfig - GetVersion() string - GetToken() string - GetNumOfWorkers() int - GetRefreshEverySecs() int - GetRepairEveryMins() int EnableRepair() bool - GetHost() string - GetPort() string - GetUsername() string - GetPassword() string - GetDirectories() []string - MeetsConditions(directory, torrentName string, torrentSize int64, torrentIDs, fileNames []string, fileSizes []int64, mediaInfos []*ffprobe.ProbeData) bool - GetOnLibraryUpdate() string - GetNetworkBufferSize() int EnableRetainFolderNameExtension() bool EnableRetainRDTorrentName() bool + GetApiTimeoutSecs() int + GetConfig() ZurgConfig + GetDirectories() []string + GetDownloadsEveryMins() int + GetDownloadTimeoutSecs() int + GetDumpTorrentsEveryMins() int + GetHost() string + GetNetworkBufferSize() int + GetNumberOfHosts() int + GetNumberOfWorkers() int + GetOnLibraryUpdate() string + GetPassword() string + GetPlayableExtensions() []string + GetPort() string + GetProxy() string + GetRarAction() string + GetRefreshEverySecs() int + GetRepairEveryMins() int + GetRetriesUntilFailed() int + GetToken() string + GetUsername() string + GetVersion() string + MeetsConditions(directory, torrentName string, torrentSize int64, torrentIDs, fileNames []string, fileSizes []int64, mediaInfos []*ffprobe.ProbeData) bool + ShouldForceIPv6() bool ShouldIgnoreRenames() bool ShouldServeFromRclone() bool - ShouldForceIPv6() bool - GetApiTimeoutSecs() int - GetDownloadTimeoutSecs() int - GetRetriesUntilFailed() int - GetRarAction() string - GetDownloadsEveryMins() int - GetDumpTorrentsEveryMins() int - GetPlayableExtensions() []string - GetProxy() string } type ZurgConfig struct { @@ -44,18 +45,20 @@ type ZurgConfig struct { ApiTimeoutSecs int `yaml:"api_timeout_secs" json:"api_timeout_secs"` CanRepair bool `yaml:"enable_repair" json:"enable_repair"` DownloadsEveryMins int `yaml:"downloads_every_mins" json:"downloads_every_mins"` - DumpTorrentsEveryMins int `yaml:"dump_torrents_every_mins" json:"dump_torrents_every_mins"` DownloadTimeoutSecs int `yaml:"download_timeout_secs" json:"download_timeout_secs"` + DumpTorrentsEveryMins int `yaml:"dump_torrents_every_mins" json:"dump_torrents_every_mins"` ForceIPv6 bool `yaml:"force_ipv6" json:"force_ipv6"` Host string `yaml:"host" json:"host"` IgnoreRenames bool `yaml:"ignore_renames" json:"ignore_renames"` NetworkBufferSize int `yaml:"network_buffer_size" json:"network_buffer_size"` + NumberOfHosts int `yaml:"number_of_hosts" json:"number_of_hosts"` NumOfWorkers int `yaml:"concurrent_workers" json:"concurrent_workers"` OnLibraryUpdate string `yaml:"on_library_update" json:"on_library_update"` Password string `yaml:"password" json:"password"` PlayableExtensions []string `yaml:"addl_playable_extensions" json:"addl_playable_extensions"` Port string `yaml:"port" json:"port"` Proxy string `yaml:"proxy" json:"proxy"` + RarAction string `yaml:"rar_action" json:"rar_action"` RefreshEverySecs int `yaml:"check_for_changes_every_secs" json:"check_for_changes_every_secs"` RepairEveryMins int `yaml:"repair_every_mins" json:"repair_every_mins"` RetainFolderNameExtension bool `yaml:"retain_folder_name_extension" json:"retain_folder_name_extension"` @@ -63,7 +66,6 @@ type ZurgConfig struct { RetriesUntilFailed int `yaml:"retries_until_failed" json:"retries_until_failed"` ServeFromRclone bool `yaml:"serve_from_rclone" json:"serve_from_rclone"` Username string `yaml:"username" json:"username"` - RarAction string `yaml:"rar_action" json:"rar_action"` } func (z *ZurgConfig) GetConfig() ZurgConfig { @@ -99,7 +101,7 @@ func (z *ZurgConfig) GetPassword() string { return z.Password } -func (z *ZurgConfig) GetNumOfWorkers() int { +func (z *ZurgConfig) GetNumberOfWorkers() int { if z.NumOfWorkers == 0 { return 20 } @@ -211,3 +213,10 @@ func (z *ZurgConfig) GetPlayableExtensions() []string { func (z *ZurgConfig) GetProxy() string { return z.Proxy } + +func (z *ZurgConfig) GetNumberOfHosts() int { + if z.NumberOfHosts == 0 { + return 20 + } + return z.NumberOfHosts +} diff --git a/pkg/http/ip.go b/pkg/http/ip.go index f65e3ef..11c2896 100644 --- a/pkg/http/ip.go +++ b/pkg/http/ip.go @@ -69,13 +69,12 @@ func (r *IPRepository) NetworkTest(forceRun bool) { r.writeLatencyFile(ipv6latencyFile, r.ipv6latencyMap) } -func (r *IPRepository) GetOptimalHosts(ipv6 bool) []string { +func (r *IPRepository) GetOptimalHosts(numberOfHosts int, ipv6 bool) []string { latencyMap := r.ipv4latencyMap if ipv6 { latencyMap = r.ipv6latencyMap } - // Convert the latency map to a slice of key-value pairs type kv struct { Key string Value float64 @@ -86,20 +85,12 @@ func (r *IPRepository) GetOptimalHosts(ipv6 bool) []string { kvList = append(kvList, kv{k, v}) } - // Sort the slice by latency values sort.Slice(kvList, func(i, j int) bool { return kvList[i].Value < kvList[j].Value }) - // Calculate the number of hosts to return (top 50%) - n := len(kvList) / 5 - if len(kvList)%5 != 0 { - n++ - } - - // Collect the keys of the top 50% hosts var optimalHosts []string - for i := 0; i < n; i++ { + for i := 0; i < numberOfHosts && i < len(kvList); i++ { optimalHosts = append(optimalHosts, kvList[i].Key) }