Don't name different things "ttl" to avoid confusion

This commit is contained in:
Frank Denis 2019-10-21 18:40:47 +02:00
parent ffd60d21db
commit a0614510e9
1 changed files with 7 additions and 6 deletions

View File

@ -34,7 +34,7 @@ const (
type CachedIPItem struct { type CachedIPItem struct {
ip net.IP ip net.IP
ttl time.Time expiration *time.Time
} }
type CachedIPs struct { type CachedIPs struct {
@ -84,12 +84,13 @@ func ParseIP(ipStr string) net.IP {
// If ttl < 0, never expire // If ttl < 0, never expire
// Otherwise, ttl is set to max(ttl, xTransport.timeout) // Otherwise, ttl is set to max(ttl, xTransport.timeout)
func (xTransport *XTransport) saveCachedIP(host string, ip net.IP, ttl time.Duration) { func (xTransport *XTransport) saveCachedIP(host string, ip net.IP, ttl time.Duration) {
item := &CachedIPItem{ip: ip, ttl: time.Time{}} item := &CachedIPItem{ip: ip, expiration: nil}
if ttl >= 0 { if ttl >= 0 {
if ttl < xTransport.timeout { if ttl < xTransport.timeout {
ttl = xTransport.timeout ttl = xTransport.timeout
} }
item.ttl = time.Now().Add(ttl) expiration := time.Now().Add(ttl)
item.expiration = &expiration
} }
xTransport.cachedIPs.Lock() xTransport.cachedIPs.Lock()
xTransport.cachedIPs.cache[host] = item xTransport.cachedIPs.cache[host] = item
@ -103,8 +104,8 @@ func (xTransport *XTransport) loadCachedIP(host string, deleteIfExpired bool) (n
if !ok { if !ok {
return nil, false return nil, false
} }
ttl := item.ttl expiration := item.expiration
if deleteIfExpired && !ttl.IsZero() && time.Until(ttl) < 0 { if deleteIfExpired && expiration != nil && time.Until(*expiration) < 0 {
xTransport.cachedIPs.Lock() xTransport.cachedIPs.Lock()
delete(xTransport.cachedIPs.cache, host) delete(xTransport.cachedIPs.cache, host)
xTransport.cachedIPs.Unlock() xTransport.cachedIPs.Unlock()