Skip to content

Commit

Permalink
dont instaniate new client for every call to our endpoints (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
dagbay-rh authored Mar 7, 2024
1 parent dfa3b7f commit 6d72078
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
10 changes: 9 additions & 1 deletion controllers/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ import (
"github.com/RedHatInsights/entitlements-api-go/config"
)

var client *http.Client

func getClient() *http.Client {
if client != nil {
return client
}

cfg := config.GetConfig()
timeout := cfg.Options.GetInt(config.Keys.ITServicesTimeoutSeconds)

// Create a HTTPS client that uses the supplied pub/priv mutual TLS certs
return &http.Client{
client = &http.Client{
Timeout: time.Duration(timeout) * time.Second,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
Expand All @@ -22,4 +28,6 @@ func getClient() *http.Client {
},
},
}

return client
}
19 changes: 19 additions & 0 deletions controllers/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package controllers

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("http client", func() {
It("should not create a new client every call", func(){
// given
client1 := getClient()

// when
client2 := getClient()

// then
Expect(client1).To(BeIdenticalTo(client2))
})
})
1 change: 0 additions & 1 deletion controllers/compliance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ var _ = Describe("Compliance Controller", func() {
rr := httptest.NewRecorder()

cfg := config.GetConfig().Options
cfg.Set(config.Keys.ITServicesTimeoutSeconds, 2)
wait := cfg.GetInt(config.Keys.ITServicesTimeoutSeconds) + 1

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expand Down
5 changes: 5 additions & 0 deletions controllers/controllers_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers
import (
"testing"

"github.com/RedHatInsights/entitlements-api-go/config"
. "github.com/RedHatInsights/entitlements-api-go/logger"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -13,3 +14,7 @@ func TestControllers(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Controllers Suite")
}

var _ = BeforeSuite(func() {
config.GetConfig().Options.Set(config.Keys.ITServicesTimeoutSeconds, 2)
})

0 comments on commit 6d72078

Please sign in to comment.