From df6e775a63d6661eba6fd01b0631f24b7953041e Mon Sep 17 00:00:00 2001 From: Cedric Staub Date: Wed, 17 Feb 2016 13:10:18 -0800 Subject: [PATCH 1/2] Fix leak from repeated time.Tick calls --- client.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index 3191f4b..d10359e 100644 --- a/client.go +++ b/client.go @@ -79,10 +79,11 @@ func NewClient(certFile, keyFile, caFile string, serverURL *url.URL, timeout tim // Asynchronously updates client and owns current reference. go func() { - var current = *initial + current := *initial + ticker := time.Tick(clientRefresh) for { select { - case t := <-time.Tick(clientRefresh): // Periodically update client. + case t := <-ticker: // Periodically update client. logger.Infof("Updating http client at %v", t) if c, err := params.buildClient(); err != nil { logger.Errorf("Error refreshing http client: %v", err) From a40ae56db5749515411d8744ea68d534e904e28c Mon Sep 17 00:00:00 2001 From: Cedric Staub Date: Wed, 17 Feb 2016 14:13:00 -0800 Subject: [PATCH 2/2] Avoid copying http.Client around, just pass reference --- client.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/client.go b/client.go index d10359e..55efbcb 100644 --- a/client.go +++ b/client.go @@ -64,12 +64,12 @@ func NewClient(certFile, keyFile, caFile string, serverURL *url.URL, timeout tim logger := klog.New("kwfs_client", logConfig) params := httpClientParams{certFile, keyFile, caFile, timeout} - reqc := make(chan http.Client) + reqc := make(chan *http.Client) // Getter from channel. getClient := func() *http.Client { client := <-reqc - return &(client) + return client } initial, err := params.buildClient() @@ -79,16 +79,16 @@ func NewClient(certFile, keyFile, caFile string, serverURL *url.URL, timeout tim // Asynchronously updates client and owns current reference. go func() { - current := *initial + current := initial ticker := time.Tick(clientRefresh) for { select { case t := <-ticker: // Periodically update client. logger.Infof("Updating http client at %v", t) - if c, err := params.buildClient(); err != nil { + if client, err := params.buildClient(); err != nil { logger.Errorf("Error refreshing http client: %v", err) } else { - current = *c + current = client } case reqc <- current: // Service request for current client. }