Giter Site home page Giter Site logo

dnscache's Introduction

A DNS cache for Go

CGO is used to lookup domain names. Given enough concurrent requests and the slightest hiccup in name resolution, it's quite easy to end up with blocked/leaking goroutines.

The issue is documented at https://code.google.com/p/go/issues/detail?id=5625

The Go team's singleflight solution (which isn't in stable yet) is rather elegant. However, it only eliminates concurrent lookups (thundering herd problems). Many systems can live with slightly stale resolve names, which means we can cacne DNS lookups and refresh them in the background.

Installation

Install using the "go get" command:

go get github.com/viki-org/dnscache

Usage

The cache is thread safe. Create a new instance by specifying how long each entry should be cached (in seconds). Items will be refreshed in the background.

//refresh items every 5 minutes
resolver := dnscache.New(time.Minute * 5)

//get an array of net.IP
ips, _ := resolver.Fetch("api.viki.io")

//get the first net.IP
ip, _ := resolver.FetchOne("api.viki.io")

//get the first net.IP as string
ip, _ := resolver.FetchOneString("api.viki.io")

// get the random net.IP
ip, _ := resolver.FetchRandomOne("api.viki.io")

// get the random net.IP as string
ip, _ := resolver.FetchRandomOneString("api.viki.io")

If you are using an http.Transport, you can use this cache by speficifying a Dial function:

transport := &http.Transport {
  MaxIdleConnsPerHost: 64,
  Dial: func(network string, address string) (net.Conn, error) {
    separator := strings.LastIndex(address, ":")
    ip, _ := dnscache.FetchRandomOneString(address[:separator])
    return net.Dial("tcp", ip + address[separator:])
  },
}

dnscache's People

Contributors

starsz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.