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

@ -29,8 +29,9 @@ type Context struct {
providers map[string]ResourceProviderFactory providers map[string]ResourceProviderFactory
variables map[string]string variables map[string]string
l sync.Mutex // Lock acquired during any task l sync.Mutex // Lock acquired during any task
sl sync.RWMutex // Lock acquired to R/W internal data parCh chan struct{} // Semaphore used to limit parallelism
sl sync.RWMutex // Lock acquired to R/W internal data
runCh <-chan struct{} runCh <-chan struct{}
sh *stopHook sh *stopHook
} }
@ -38,12 +39,13 @@ type Context struct {
// ContextOpts are the user-creatable configuration structure to create // ContextOpts are the user-creatable configuration structure to create
// a context with NewContext. // a context with NewContext.
type ContextOpts struct { type ContextOpts struct {
Config *config.Config Config *config.Config
Diff *Diff Diff *Diff
Hooks []Hook Hooks []Hook
State *State Parallelism int
Providers map[string]ResourceProviderFactory State *State
Variables map[string]string Providers map[string]ResourceProviderFactory
Variables map[string]string
} }
// NewContext creates a new context. // NewContext creates a new context.
@ -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,7 +77,8 @@ func NewContext(opts *ContextOpts) *Context {
providers: opts.Providers, providers: opts.Providers,
variables: opts.Variables, variables: opts.Variables,
sh: sh, parCh: parCh,
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