dnscache

A DNS Cache for Go

  • Owner: viki-org/dnscache
  • Platform:
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

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")

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.FetchString(address[:separator])
    return net.Dial("tcp", ip + address[separator:])
  },
}

Main metrics

Overview
Name With Ownerviki-org/dnscache
Primary LanguageGo
Program languageGo (Language Count: 1)
Platform
License:MIT License
所有者活动
Created At2013-07-19 02:03:30
Pushed At2022-06-18 22:17:40
Last Commit At2013-07-20 10:35:26
Release Count0
用户参与
Stargazers Count127
Watchers Count2
Fork Count23
Commits Count4
Has Issues Enabled
Issues Count5
Issue Open Count5
Pull Requests Count0
Pull Requests Open Count2
Pull Requests Close Count2
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private