From 87423ad3dfa52832bdb5f3d805e2df9d510d68bd Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Thu, 29 Aug 2024 11:55:12 +0200 Subject: [PATCH] Limit the amount of goroutnes started --- main.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/main.go b/main.go index 49619cf..e73b022 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "log" "os" "path/filepath" + "runtime" "strings" "sync" ) @@ -40,7 +41,10 @@ func main() { to := flag.String("to", ".jpg", "Extension to transcode to") nosafe := flag.Bool("nosafe", false, "Prevents backup of the original before encoding") rm := flag.Bool("rm", false, "Removes the original after transcoding") + w := flag.Int("w", 8, "Workers") flag.Parse() + + workers := make(chan struct{}, *w) if _, ok := codecs[*to]; !ok { Error.Println("Invalid extension") @@ -55,7 +59,9 @@ func main() { var wg sync.WaitGroup for _, file := range flag.Args() { wg.Add(1) + workers <- struct{}{} go func(file string) { + defer func() { <-workers }() defer wg.Done() file = filepath.ToSlash(file) file = filepath.Clean(file) @@ -138,3 +144,27 @@ func main() { } wg.Wait() } + +func instrument() { + numGoroutines := runtime.NumGoroutine() + + var m runtime.MemStats + runtime.ReadMemStats(&m) + // log.Printf("%+v", m) + + sys := float64(m.Sys) + ramUsedMB := sys / 1024 / 1024 + kbPerGoroutine := sys / 1024 / float64(numGoroutines) + + var numGoroutinesPretty string + switch { + case numGoroutines >= 1_000_000: + numGoroutinesPretty = fmt.Sprintf("%.2fM", float64(numGoroutines)/1_000_000) + case numGoroutines >= 1_000: + numGoroutinesPretty = fmt.Sprintf("%.2fk", float64(numGoroutines)/1_000) + default: + numGoroutinesPretty = fmt.Sprintf("%d", numGoroutines) + } + + fmt.Printf("\rNumber of active goroutines: %d (%s); RAM used: %.2f MB; KB per goroutine: %.2f", numGoroutines, numGoroutinesPretty, ramUsedMB, kbPerGoroutine) +} \ No newline at end of file