From 3d75c95baab308752cf8e4b5bc3c707e58d9dde3 Mon Sep 17 00:00:00 2001 From: Alexey Kostin Date: Thu, 20 Sep 2018 00:27:48 +0300 Subject: [PATCH] Thread limiter --- bt2qbt.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/bt2qbt.go b/bt2qbt.go index b391dde..fbe84c0 100644 --- a/bt2qbt.go +++ b/bt2qbt.go @@ -14,6 +14,7 @@ import ( "log" "os" "path/filepath" + "runtime" "runtime/debug" "strconv" "strings" @@ -374,8 +375,11 @@ func (newstructure *NewTorrentStructure) fillsavepaths() { } func logic(key string, value map[string]interface{}, bitdir *string, with_label *bool, with_tags *bool, - qbitdir *string, comChannel chan string, errChannel chan string, position int, wg *sync.WaitGroup) error { + qbitdir *string, comChannel chan string, errChannel chan string, position int, wg *sync.WaitGroup, boundedChannel chan bool) error { defer wg.Done() + defer func() { + <- boundedChannel + }() defer func() { if r := recover(); r != nil { errChannel <- fmt.Sprintf( @@ -537,6 +541,7 @@ func main() { var wg sync.WaitGroup comChannel := make(chan string, totaljobs) errChannel := make(chan string, totaljobs) + boundedChannel := make(chan bool, runtime.GOMAXPROCS(0) * 2) positionnum := 0 for key, value := range resumefile { if key != ".fileguard" && key != "rec" { @@ -553,8 +558,9 @@ func main() { } } wg.Add(1) + boundedChannel <- true go logic(key, value.(map[string]interface{}), &bitdir, &with_label, &with_tags, &qbitdir, comChannel, - errChannel, positionnum, &wg) + errChannel, positionnum, &wg, boundedChannel) } } go func() {