dnscache

A DNS Cache for Go

  • 所有者: viki-org/dnscache
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

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:])
  },
}

主要指標

概覽
名稱與所有者viki-org/dnscache
主編程語言Go
編程語言Go (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2013-07-19 02:03:30
推送於2022-06-18 22:17:40
最后一次提交2013-07-20 10:35:26
發布數0
用户参与
星數127
關注者數2
派生數23
提交數4
已啟用問題?
問題數5
打開的問題數5
拉請求數0
打開的拉請求數2
關閉的拉請求數2
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?