diff --git a/config.yml.example b/config.yml.example index 263f513..efe3b21 100644 --- a/config.yml.example +++ b/config.yml.example @@ -1,6 +1,12 @@ # Zurg configuration version zurg: v1 +token: YOUR_TOKEN_HERE +port: 9999 +concurrent_workers: 10 +check_for_changes_every_secs: 15 +info_cache_time_hours: 12 + # List of directory definitions and their filtering rules directories: diff --git a/internal/config/load.go b/internal/config/load.go index ec47b17..1d5f94d 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -10,6 +10,9 @@ import ( type ConfigInterface interface { GetVersion() string GetToken() string + GetNumOfWorkers() int + GetRefreshEverySeconds() int + GetCacheTimeHours() int GetPort() string GetDirectories() []string MeetsConditions(directory, fileID, fileName string) bool diff --git a/internal/config/types.go b/internal/config/types.go index e978a25..d455b6f 100644 --- a/internal/config/types.go +++ b/internal/config/types.go @@ -1,7 +1,10 @@ package config type ZurgConfig struct { - Version string `yaml:"zurg"` - Token string `yaml:"token"` - Port string `yaml:"port"` + Version string `yaml:"zurg"` + Token string `yaml:"token"` + Port string `yaml:"port"` + NumOfWorkers int `yaml:"concurrent_workers"` + RefreshEverySeconds int `yaml:"check_for_changes_every_secs"` + CacheTimeHours int `yaml:"info_cache_time_hours"` } diff --git a/internal/config/v1.go b/internal/config/v1.go index a6d949f..63dd9f2 100644 --- a/internal/config/v1.go +++ b/internal/config/v1.go @@ -28,6 +28,18 @@ func (z *ZurgConfigV1) GetPort() string { return z.Port } +func (z *ZurgConfigV1) GetNumOfWorkers() int { + return z.NumOfWorkers +} + +func (z *ZurgConfigV1) GetRefreshEverySeconds() int { + return z.RefreshEverySeconds +} + +func (z *ZurgConfigV1) GetCacheTimeHours() int { + return z.CacheTimeHours +} + func (z *ZurgConfigV1) GetDirectories() []string { var rootDirectories []string for directory := range z.Directories { diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index b2feef6..b8ca63a 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -21,21 +21,21 @@ type TorrentManager struct { checksum string } -func (handler *TorrentManager) refreshTorrents() { +func (t *TorrentManager) refreshTorrents() { log.Println("Starting periodic refresh") for { - <-time.After(15 * time.Second) - checksum := handler.getChecksum() - if checksum == handler.checksum { + <-time.After(time.Duration(t.config.GetRefreshEverySeconds()) * time.Second) + checksum := t.getChecksum() + if checksum == t.checksum { continue } - handler.checksum = checksum - handler.torrents = handler.getAll() - for _, torrent := range handler.torrents { + t.checksum = checksum + t.torrents = t.getAll() + for _, torrent := range t.torrents { go func(id string) { - handler.workerPool <- true - handler.getInfo(id) - <-handler.workerPool + t.workerPool <- true + t.getInfo(id) + <-t.workerPool time.Sleep(1 * time.Second) // sleep for 1 second to avoid rate limiting }(torrent.ID) } @@ -48,7 +48,7 @@ func (handler *TorrentManager) refreshTorrents() { func NewTorrentManager(config config.ConfigInterface) *TorrentManager { handler := &TorrentManager{ token: config.GetToken(), - workerPool: make(chan bool, 10), + workerPool: make(chan bool, config.GetNumOfWorkers()), config: config, } @@ -143,7 +143,7 @@ func (t *TorrentManager) RefreshInfo(torrentID string) { if err == nil { modTime := fileInfo.ModTime() // If the file was modified less than an hour ago, don't refresh - if time.Since(modTime) < time.Hour { + if time.Since(modTime) < time.Duration(t.config.GetCacheTimeHours())*time.Hour { return } err = os.Remove(filePath) @@ -195,7 +195,7 @@ func (t *TorrentManager) getInfo(torrentID string) *Torrent { var wg sync.WaitGroup // Limit concurrency - sem := make(chan struct{}, 10) // e.g., 10 concurrent requests + sem := make(chan struct{}, t.config.GetNumOfWorkers()) for _, link := range info.Links { wg.Add(1)