forked from gruntwork-io/terragrunt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
running_limit.go
53 lines (46 loc) · 1.43 KB
/
running_limit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package configstack
import (
"fmt"
"time"
"github.com/fatih/color"
)
const progressiveStartDelay = 500 * time.Millisecond
func initWorkers(n int) {
if n <= 0 {
panic(fmt.Errorf("the number of workers must be greater than 0 (%d)", n))
}
if burstyLimiter == nil {
burstyLimiter = make(chan int, n)
go func() {
for i := 1; i <= n; i++ {
burstyLimiter <- i
time.Sleep(progressiveStartDelay) // Start workers progressively to avoid throttling
}
}()
}
}
func nbWorkers() int { return cap(burstyLimiter) }
func waitWorker() int { return <-burstyLimiter }
func freeWorker(token int) { burstyLimiter <- token }
var burstyLimiter chan int
// OutputPeriodicLogs displays current module output for long running request
func (module *runningModule) OutputPeriodicLogs(completed *bool) {
if module.Module.TerragruntOptions.RefreshOutputDelay == 0 {
return
}
writer := module.Module.TerragruntOptions.Logger.Infof
for {
time.Sleep(module.Module.TerragruntOptions.RefreshOutputDelay)
if *completed {
break
}
partialOutput := module.OutStream.String()
if len(partialOutput) > module.bufferIndex {
end := len(partialOutput)
partialOutput = partialOutput[module.bufferIndex:end]
message := color.New(color.FgHiCyan).Sprintf("Still waiting for task to complete\n%s\n%s (partial output):\n", separator, module.displayName())
writer("%s\n%s\n", message, partialOutput)
module.bufferIndex = end
}
}
}