From 06175932d28bfdf16b8f1cb90a6ea624626c08ca Mon Sep 17 00:00:00 2001 From: Mike Kabischev Date: Fri, 10 Feb 2017 16:55:01 +0300 Subject: [PATCH 1/2] Fix leaking http connections (#147) * fix leaking http connections * remove unused fields --- controller/endpoint/http.go | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/controller/endpoint/http.go b/controller/endpoint/http.go index 07cdc677..5364efad 100644 --- a/controller/endpoint/http.go +++ b/controller/endpoint/http.go @@ -19,41 +19,28 @@ const ( type HTTPEndpointConn struct { mu sync.Mutex ep Endpoint - ex bool - t time.Time client *http.Client } func newHTTPEndpointConn(ep Endpoint) *HTTPEndpointConn { return &HTTPEndpointConn{ ep: ep, - t: time.Now(), } } func (conn *HTTPEndpointConn) Expired() bool { - conn.mu.Lock() - defer conn.mu.Unlock() - if !conn.ex { - if time.Now().Sub(conn.t) > httpExpiresAfter { - conn.ex = true - conn.client = nil - } - } - return conn.ex + return false } func (conn *HTTPEndpointConn) Send(msg string) error { conn.mu.Lock() defer conn.mu.Unlock() - if conn.ex { - return errExpired - } - conn.t = time.Now() + if conn.client == nil { conn.client = &http.Client{ Transport: &http.Transport{ MaxIdleConnsPerHost: httpMaxIdleConnections, + IdleConnTimeout: httpExpiresAfter, }, Timeout: httpRequestTimeout, } From a4705cee230a18aa4d2e9ef6bdef89e30e24770c Mon Sep 17 00:00:00 2001 From: Mike Kabischev Date: Fri, 10 Feb 2017 22:09:07 +0300 Subject: [PATCH 2/2] remove mutex from HTTP endpoint (#148) * fix leaking http connections * remove unused fields * remove mutex from HTTPEndpointConn --- controller/endpoint/http.go | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/controller/endpoint/http.go b/controller/endpoint/http.go index 5364efad..7326aee1 100644 --- a/controller/endpoint/http.go +++ b/controller/endpoint/http.go @@ -6,7 +6,6 @@ import ( "io" "io/ioutil" "net/http" - "sync" "time" ) @@ -17,7 +16,6 @@ const ( ) type HTTPEndpointConn struct { - mu sync.Mutex ep Endpoint client *http.Client } @@ -25,6 +23,13 @@ type HTTPEndpointConn struct { func newHTTPEndpointConn(ep Endpoint) *HTTPEndpointConn { return &HTTPEndpointConn{ ep: ep, + client: &http.Client{ + Transport: &http.Transport{ + MaxIdleConnsPerHost: httpMaxIdleConnections, + IdleConnTimeout: httpExpiresAfter, + }, + Timeout: httpRequestTimeout, + }, } } @@ -33,18 +38,6 @@ func (conn *HTTPEndpointConn) Expired() bool { } func (conn *HTTPEndpointConn) Send(msg string) error { - conn.mu.Lock() - defer conn.mu.Unlock() - - if conn.client == nil { - conn.client = &http.Client{ - Transport: &http.Transport{ - MaxIdleConnsPerHost: httpMaxIdleConnections, - IdleConnTimeout: httpExpiresAfter, - }, - Timeout: httpRequestTimeout, - } - } req, err := http.NewRequest("POST", conn.ep.Original, bytes.NewBufferString(msg)) if err != nil { return err