Skip to content

Commit

Permalink
Fix concurrent closure of shared api clients (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
oboukili authored Jul 16, 2020
1 parent 7386dec commit 0d84db7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 24 deletions.
19 changes: 5 additions & 14 deletions argocd/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

var apiClientConnOpts apiclient.ClientOptions

func Provider(doneCh chan bool) terraform.ResourceProvider {
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"server_addr": {
Expand Down Expand Up @@ -113,34 +113,25 @@ func Provider(doneCh chan bool) terraform.ResourceProvider {
if err != nil {
return nil, err
}
pcCloser, projectClient, err := apiClient.NewProjectClient()
_, projectClient, err := apiClient.NewProjectClient()
if err != nil {
return nil, err
}

acCloser, applicationClient, err := apiClient.NewApplicationClient()
_, applicationClient, err := apiClient.NewApplicationClient()
if err != nil {
return nil, err
}

rcCloser, repositoryClient, err := apiClient.NewRepoClient()
_, repositoryClient, err := apiClient.NewRepoClient()
if err != nil {
return nil, err
}

rcredsCloser, repoCredsClient, err := apiClient.NewRepoCredsClient()
_, repoCredsClient, err := apiClient.NewRepoCredsClient()
if err != nil {
return nil, err
}

// Clients connection pooling, close when the provider execution ends
go func(done chan bool) {
<-done
util.Close(pcCloser)
util.Close(acCloser)
util.Close(rcCloser)
util.Close(rcredsCloser)
}(doneCh)
return initServerInterface(
apiClient,
projectClient,
Expand Down
8 changes: 3 additions & 5 deletions argocd/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,22 @@ import (

var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *schema.Provider
var testDoneCh = make(chan bool, 1)

func init() {
testAccProvider = Provider(testDoneCh).(*schema.Provider)
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"argocd": testAccProvider,
}
testDoneCh <- true
}

func TestProvider(t *testing.T) {
if err := Provider(testDoneCh).(*schema.Provider).InternalValidate(); err != nil {
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}

func TestProvider_impl(t *testing.T) {
var _ = Provider(testDoneCh)
var _ = Provider()
}

func testAccPreCheck(t *testing.T) {
Expand Down
6 changes: 1 addition & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ import (
)

func main() {
// ArgoCD services connection pool closing channel
var doneCh = make(chan bool, 1)

plugin.Serve(&plugin.ServeOpts{
ProviderFunc: func() terraform.ResourceProvider {
return argocd.Provider(doneCh)
return argocd.Provider()
},
})
doneCh <- true
}

0 comments on commit 0d84db7

Please sign in to comment.