terraform: limit parallelism

This commit is contained in:
Mitchell Hashimoto 2015-02-16 20:27:29 -08:00
parent 0175d1babc
commit fa222a44c3
2 changed files with 25 additions and 8 deletions

View File

@ -59,6 +59,7 @@ type Context struct {
variables map[string]string
l sync.Mutex // Lock acquired during any task
parallelSem Semaphore
providerInputConfig map[string]map[string]interface{}
runCh <-chan struct{}
}
@ -82,17 +83,27 @@ func NewContext(opts *ContextOpts) *Context {
state.init()
}
// Determine parallelism, default to 10. We do this both to limit
// CPU pressure but also to have an extra guard against rate throttling
// from providers.
par := opts.Parallelism
if par == 0 {
par = 10
}
return &Context{
diff: opts.Diff,
hooks: hooks,
module: opts.Module,
providers: opts.Providers,
diff: opts.Diff,
hooks: hooks,
module: opts.Module,
providers: opts.Providers,
provisioners: opts.Provisioners,
state: state,
uiInput: opts.UIInput,
variables: opts.Variables,
parallelSem: NewSemaphore(par),
providerInputConfig: make(map[string]map[string]interface{}),
provisioners: opts.Provisioners,
sh: sh,
state: state,
uiInput: opts.UIInput,
variables: opts.Variables,
}
}

View File

@ -84,6 +84,9 @@ func (w *ContextGraphWalker) EnterGraph(g *Graph) EvalContext {
}
func (w *ContextGraphWalker) EnterEvalTree(v dag.Vertex, n EvalNode) EvalNode {
// Acquire a lock on the semaphore
w.Context.parallelSem.Acquire()
// We want to filter the evaluation tree to only include operations
// that belong in this operation.
return EvalFilter(n, EvalNodeFilterOp(w.Operation))
@ -91,6 +94,9 @@ func (w *ContextGraphWalker) EnterEvalTree(v dag.Vertex, n EvalNode) EvalNode {
func (w *ContextGraphWalker) ExitEvalTree(
v dag.Vertex, output interface{}, err error) error {
// Release the semaphore
w.Context.parallelSem.Release()
if err == nil {
return nil
}