Fix negative parallelism and negative semaphore (#23902)

* Throw an error when parallelism <=0

* Panic in case of negative semaphore
This commit is contained in:
Pierre Carles 2020-02-12 16:10:52 +01:00 committed by GitHub
parent 851e6dcdbb
commit b956e8ef35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View File

@ -138,7 +138,17 @@ func NewContext(opts *ContextOpts) (*Context, tfdiags.Diagnostics) {
// 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.
// We throw an error in case of negative parallelism
par := opts.Parallelism
if par < 0 {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Invalid parallelism value",
fmt.Sprintf("The parallelism must be a positive value. Not %d.", par),
))
return nil, diags
}
if par == 0 {
par = 10
}

View File

@ -12,8 +12,8 @@ type Semaphore chan struct{}
// NewSemaphore creates a semaphore that allows up
// to a given limit of simultaneous acquisitions
func NewSemaphore(n int) Semaphore {
if n == 0 {
panic("semaphore with limit 0")
if n <= 0 {
panic("semaphore with limit <=0")
}
ch := make(chan struct{}, n)
return Semaphore(ch)