Skip to content

Commit

Permalink
Merge pull request hudl#27 from CtrlZvi/fix_connection_leak
Browse files Browse the repository at this point in the history
Plug RPC connection leak

Since `http.Transport.Close` was a no-op, RPC calls were leaking sockets. Fixing this moved the transport to a global variable and may make RPC faster by reducing connection overhead.
  • Loading branch information
ryansb committed Sep 30, 2015
2 parents 1a8ede3 + 7501423 commit fec96e9
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ package fargo

import (
"bytes"
"github.com/mreiferson/go-httpclient"
"io/ioutil"
"net"
"net/http"
"net/url"
"time"
)

var transport = &http.Transport{
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
}).Dial,
ResponseHeaderTimeout: 10 * time.Second,
}

func postBody(reqURL string, reqBody []byte, isJson bool) ([]byte, int, error) {
req, err := http.NewRequest("POST", reqURL, bytes.NewReader(reqBody))
if err != nil {
Expand Down Expand Up @@ -88,15 +94,11 @@ func netReqTyped(req *http.Request, isJson bool) ([]byte, int, error) {
}

func netReq(req *http.Request) ([]byte, int, error) {
transport := &httpclient.Transport{
ConnectTimeout: 5 * time.Second,
RequestTimeout: 30 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
}
defer transport.Close()

// Send the request via a client
client := &http.Client{Transport: transport}
client := &http.Client{
Transport: transport,
Timeout: 30 * time.Second,
}
var resp *http.Response
var err error
for i := 0; i < 3; i++ {
Expand Down

0 comments on commit fec96e9

Please sign in to comment.