Fix leaking http connections (#147)

* fix leaking http connections

* remove unused fields
This commit is contained in:
Mike Kabischev 2017-02-10 16:55:01 +03:00 committed by Josh Baker
parent 22f1b1bd81
commit 06175932d2

View File

@ -19,41 +19,28 @@ const (
type HTTPEndpointConn struct { type HTTPEndpointConn struct {
mu sync.Mutex mu sync.Mutex
ep Endpoint ep Endpoint
ex bool
t time.Time
client *http.Client client *http.Client
} }
func newHTTPEndpointConn(ep Endpoint) *HTTPEndpointConn { func newHTTPEndpointConn(ep Endpoint) *HTTPEndpointConn {
return &HTTPEndpointConn{ return &HTTPEndpointConn{
ep: ep, ep: ep,
t: time.Now(),
} }
} }
func (conn *HTTPEndpointConn) Expired() bool { func (conn *HTTPEndpointConn) Expired() bool {
conn.mu.Lock() return false
defer conn.mu.Unlock()
if !conn.ex {
if time.Now().Sub(conn.t) > httpExpiresAfter {
conn.ex = true
conn.client = nil
}
}
return conn.ex
} }
func (conn *HTTPEndpointConn) Send(msg string) error { func (conn *HTTPEndpointConn) Send(msg string) error {
conn.mu.Lock() conn.mu.Lock()
defer conn.mu.Unlock() defer conn.mu.Unlock()
if conn.ex {
return errExpired
}
conn.t = time.Now()
if conn.client == nil { if conn.client == nil {
conn.client = &http.Client{ conn.client = &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
MaxIdleConnsPerHost: httpMaxIdleConnections, MaxIdleConnsPerHost: httpMaxIdleConnections,
IdleConnTimeout: httpExpiresAfter,
}, },
Timeout: httpRequestTimeout, Timeout: httpRequestTimeout,
} }