Skip to content

Commit

Permalink
added dnscache
Browse files Browse the repository at this point in the history
  • Loading branch information
John Muth committed Sep 12, 2017
1 parent 50f4b8d commit 2d33d40
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
FROM golang:1.8

RUN go get -u github.com/alecthomas/gometalinter && gometalinter --install

WORKDIR /go/src/app
COPY . .

RUN go get -u github.com/alecthomas/gometalinter && gometalinter --install
RUN go-wrapper download # "go get -d -v ./..."
RUN go-wrapper install # "go install -v ./..."

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ To make sense of the detailed log messages when the application is handling lots

To do the load testing I'm using [locust](http://locust.io), because it looks nice, I like Python, and I'm bored of [Gatling](http://gatling.io) (maybe just bored of apologising for Scala!)

## DNS caching

In my testing I found that some requests were timing out during DNS lookup.

Since the application hits the same URL over and over again it seemed ridiculous to be doing the same DNS lookup repeatedly.

Go does not cache DNS lookups by default - unlike, say, Java.

I introduced [dnscache](https://github.com/viki-org/dnscache) and defined a custom Dial function when instantiating the http client in [app.go](app.go) - that got rid of all timeouts during the DNS lookup phase.



15 changes: 15 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"net/http"
"os"
"time"
"github.com/viki-org/dnscache"
"strings"
)

func main() {
Expand All @@ -21,9 +23,22 @@ func main() {

log.Info("Listening on", config.Port)

resolver := dnscache.New(time.Second * 60) //how often to refresh cached dns records, happens in background

httpClient := &http.Client{
Transport: &http.Transport{
MaxIdleConnsPerHost: config.HTTPClientMaxIdleConnsPerHost,
// Go does not cache DNS lookups, so we define a custom Dial function that does.
// This fixed a problem where requests were timing out during DNS lookup
// even though we were hitting the same hostname over and over.
Dial: func(network string, address string) (net.Conn, error) {
separator := strings.LastIndex(address, ":")
ip, err := resolver.FetchOneString(address[:separator])
if err != nil {
return nil, err
}
return net.Dial("tcp", ip + address[separator:])
},
DialContext: (&net.Dialer{
Timeout: time.Duration(config.HTTPClientDialerTimeoutMS) * time.Millisecond,
KeepAlive: time.Duration(config.HTTPClientDialerKeepAliveMS) * time.Millisecond,
Expand Down
4 changes: 4 additions & 0 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func (svc Service) Call(serviceRequest ServiceRequest) (serviceResponse ServiceR
"err": err,
"requestid": serviceRequest.RequestID,
}).Error("Error parsing response from Service.")
} else {
log.WithFields(map[string]interface{}{
"requestid": serviceRequest.RequestID,
}).Info("Done with request")
}
return
}
Expand Down
77 changes: 77 additions & 0 deletions vendor/github.com/viki-org/dnscache/dnscache.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/viki-org/dnscache/license.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions vendor/github.com/viki-org/dnscache/readme.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
"revision": "89742aefa4b206dcf400792f3bd35b542998eb3b",
"revisionTime": "2017-08-22T13:27:46Z"
},
{
"checksumSHA1": "QITz/x1CJ8ylMdObGcdiL68xK8k=",
"path": "github.com/viki-org/dnscache",
"revision": "c70c1f23c5d84ab39054ec6b418a5ed0fa51cade",
"revisionTime": "2013-07-20T02:35:26Z"
},
{
"checksumSHA1": "nqWNlnMmVpt628zzvyo6Yv2CX5Q=",
"path": "golang.org/x/crypto/ssh/terminal",
Expand Down

0 comments on commit 2d33d40

Please sign in to comment.