Enforce sane upper limit for TTL in DefaultDnsCache. (#7907)

Motivation:

In b47fb81799 we limited the max supported delay to match what our internal implementat can support. Because of this it was possible that DefaultDnsCache produced an IllegalArgumentException when it tried to schedule a expiration > 3 years.

Modifications:

Limit the max supported TTL to 2 years which is safe for all our EventLoop implementations.

Result:

No more exceptions when adding records to the cache with a huge TTL.
This commit is contained in:
Norman Maurer 2018-05-04 13:38:08 +02:00 committed by GitHub
parent 095dadcbec
commit a598c3b69b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View File

@ -43,6 +43,9 @@ public class DefaultDnsCache implements DnsCache {
private final ConcurrentMap<String, Entries> resolveCache = PlatformDependent.newConcurrentHashMap();
// Two years are supported by all our EventLoop implementations and so safe to use as maximum.
// See also: https://github.com/netty/netty/commit/b47fb817991b42ec8808c7d26538f3f2464e1fa6
private static final int MAX_SUPPORTED_TTL_SECS = (int) TimeUnit.DAYS.toSeconds(365 * 2);
private final int minTtl;
private final int maxTtl;
private final int negativeTtl;
@ -52,7 +55,7 @@ public class DefaultDnsCache implements DnsCache {
* and doesn't cache negative responses.
*/
public DefaultDnsCache() {
this(0, Integer.MAX_VALUE, 0);
this(0, MAX_SUPPORTED_TTL_SECS, 0);
}
/**
@ -141,7 +144,7 @@ public class DefaultDnsCache implements DnsCache {
if (maxTtl == 0 || !emptyAdditionals(additionals)) {
return e;
}
cache0(e, Math.max(minTtl, (int) Math.min(maxTtl, originalTtl)), loop);
cache0(e, Math.max(minTtl, Math.min(MAX_SUPPORTED_TTL_SECS, (int) Math.min(maxTtl, originalTtl))), loop);
return e;
}
@ -156,7 +159,7 @@ public class DefaultDnsCache implements DnsCache {
return e;
}
cache0(e, negativeTtl, loop);
cache0(e, Math.min(MAX_SUPPORTED_TTL_SECS, negativeTtl), loop);
return e;
}

View File

@ -18,6 +18,7 @@ package io.netty.resolver.dns;
import io.netty.channel.DefaultEventLoopGroup;
import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.NetUtil;
import org.junit.Assert;
import org.junit.Test;
@ -55,4 +56,23 @@ public class DefaultDnsCacheTest {
group.shutdownGracefully();
}
}
@Test
public void testExpireWithDifferentTTLs() {
testExpireWithTTL0(1);
testExpireWithTTL0(1000);
testExpireWithTTL0(1000000);
}
private static void testExpireWithTTL0(int days) {
EventLoopGroup group = new NioEventLoopGroup(1);
try {
EventLoop loop = group.next();
final DefaultDnsCache cache = new DefaultDnsCache();
Assert.assertNotNull(cache.cache("netty.io", null, NetUtil.LOCALHOST, days, loop));
} finally {
group.shutdownGracefully();
}
}
}