terraform: ContextOpts can set a max parallelism

This commit is contained in:
Mitchell Hashimoto 2014-07-08 11:42:03 -07:00
parent 7f86bcb873
commit 3318fe97dc
1 changed files with 25 additions and 9 deletions

View File

@ -30,6 +30,7 @@ type Context struct {
variables map[string]string variables map[string]string
l sync.Mutex // Lock acquired during any task 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 sl sync.RWMutex // Lock acquired to R/W internal data
runCh <-chan struct{} runCh <-chan struct{}
sh *stopHook sh *stopHook
@ -41,6 +42,7 @@ type ContextOpts struct {
Config *config.Config Config *config.Config
Diff *Diff Diff *Diff
Hooks []Hook Hooks []Hook
Parallelism int
State *State State *State
Providers map[string]ResourceProviderFactory Providers map[string]ResourceProviderFactory
Variables map[string]string Variables map[string]string
@ -60,6 +62,13 @@ func NewContext(opts *ContextOpts) *Context {
copy(hooks, opts.Hooks) copy(hooks, opts.Hooks)
hooks[len(opts.Hooks)] = sh hooks[len(opts.Hooks)] = sh
// Make the parallelism channel
par := opts.Parallelism
if par == 0 {
par = 10
}
parCh := make(chan struct{}, par)
return &Context{ return &Context{
config: opts.Config, config: opts.Config,
diff: opts.Diff, diff: opts.Diff,
@ -68,6 +77,7 @@ func NewContext(opts *ContextOpts) *Context {
providers: opts.Providers, providers: opts.Providers,
variables: opts.Variables, variables: opts.Variables,
parCh: parCh,
sh: sh, sh: sh,
} }
} }
@ -682,6 +692,12 @@ func (c *Context) genericWalkFn(cb genericWalkFunc) depgraph.WalkFunc {
return nil return nil
} }
// Limit parallelism
c.parCh <- struct{}{}
defer func() {
<-c.parCh
}()
switch m := n.Meta.(type) { switch m := n.Meta.(type) {
case *GraphNodeResource: case *GraphNodeResource:
// Continue, we care about this the most // Continue, we care about this the most