Skip to content

Commit

Permalink
Merge pull request #15 from customerio/http_keepalive
Browse files Browse the repository at this point in the history
Reuse the same HTTP client and connections
  • Loading branch information
huihuiy02 authored Oct 24, 2019
2 parents 88fa401 + 7b622b1 commit 8d95e97
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions customerio.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type CustomerIO struct {
apiKey string
Host string
SSL bool
client *http.Client
}

// CustomerIOError is returned by any method that fails at the API level
Expand All @@ -32,7 +33,13 @@ func (e *CustomerIOError) Error() string {

// NewCustomerIO creates a new CustomerIO object to perform requests on the supplied credentials
func NewCustomerIO(siteID, apiKey string) *CustomerIO {
return &CustomerIO{siteID, apiKey, "track.customer.io", true}
tr := &http.Transport{
MaxIdleConnsPerHost: 100,
}
client := &http.Client{
Transport: tr,
}
return &CustomerIO{siteID, apiKey, "track.customer.io", true, client}
}

// Identify identifies a customer and sets their attributes
Expand Down Expand Up @@ -251,7 +258,7 @@ func (c *CustomerIO) request(method, url string, body []byte) (status int, respo
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Content-Length", strconv.Itoa(len(body)))

resp, err := http.DefaultClient.Do(req)
resp, err := c.client.Do(req)
if err != nil {
return 0, nil, err
}
Expand Down

0 comments on commit 8d95e97

Please sign in to comment.