Prevent multiple IP lookups

This commit is contained in:
Eduard Urbach 2018-07-07 13:23:00 +09:00
parent 25b7e39d75
commit 339bac921f

View File

@ -2,15 +2,20 @@ package middleware
import ( import (
"net" "net"
"sync"
"time" "time"
cache "github.com/patrickmn/go-cache" cache "github.com/patrickmn/go-cache"
) )
var ipToHosts = cache.New(60*time.Minute, 30*time.Minute) var ipToHosts = cache.New(60*time.Minute, 30*time.Minute)
var ipToHostsMutex sync.Mutex
// GetHostsForIP returns all host names for the given IP (if cached). // GetHostsForIP returns all host names for the given IP (if cached).
func GetHostsForIP(ip string) ([]string, bool) { func GetHostsForIP(ip string) ([]string, bool) {
ipToHostsMutex.Lock()
defer ipToHostsMutex.Unlock()
hosts, found := ipToHosts.Get(ip) hosts, found := ipToHosts.Get(ip)
if !found { if !found {