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 (
"net"
"sync"
"time"
cache "github.com/patrickmn/go-cache"
)
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).
func GetHostsForIP(ip string) ([]string, bool) {
ipToHostsMutex.Lock()
defer ipToHostsMutex.Unlock()
hosts, found := ipToHosts.Get(ip)
if !found {