terraform: Switch to using semaphore

This commit is contained in:
Armon Dadgar 2014-10-16 10:04:24 -07:00
parent ad31023252
commit 422b82648c
1 changed files with 11 additions and 14 deletions

View File

@ -36,8 +36,8 @@ type Context struct {
variables map[string]string
uiInput UIInput
parallelSem Semaphore // Semaphore used to limit parallelism
l sync.Mutex // Lock acquired during any task
parCh chan struct{} // Semaphore used to limit parallelism
sl sync.RWMutex // Lock acquired to R/W internal data
runCh <-chan struct{}
sh *stopHook
@ -93,7 +93,6 @@ func NewContext(opts *ContextOpts) *Context {
if par == 0 {
par = 10
}
parCh := make(chan struct{}, par)
return &Context{
diff: opts.Diff,
@ -106,7 +105,7 @@ func NewContext(opts *ContextOpts) *Context {
variables: opts.Variables,
uiInput: opts.UIInput,
parCh: parCh,
parallelSem: NewSemaphore(par),
sh: sh,
}
}
@ -1152,12 +1151,6 @@ func (c *walkContext) genericWalkFn(cb genericWalkFunc) depgraph.WalkFunc {
return nil
}
// Limit parallelism
c.Context.parCh <- struct{}{}
defer func() {
<-c.Context.parCh
}()
switch m := n.Meta.(type) {
case *GraphNodeModule:
// Build another walkContext for this module and walk it.
@ -1239,6 +1232,10 @@ func (c *walkContext) genericWalkFn(cb genericWalkFunc) depgraph.WalkFunc {
}
}()
// Limit parallelism
c.Context.parallelSem.Acquire()
defer c.Context.parallelSem.Release()
// Call the callack
log.Printf(
"[INFO] Module %s walking: %s (Graph node: %s)",