Accept TTL values of 0 in CachePolicy #26427

As per RFC spec https://www.ietf.org/rfc/rfc1035.txt (Section 3.2.1)
This commit is contained in:
Adriaan Groenenboom 2019-03-25 17:28:10 +01:00 committed by Johan Andrén
parent 4f7b78feb0
commit 1fe5505f84
2 changed files with 12 additions and 2 deletions

View file

@ -15,7 +15,8 @@ object CachePolicy {
case object Forever extends CachePolicy case object Forever extends CachePolicy
final class Ttl private (val value: FiniteDuration) extends CachePolicy { final class Ttl private (val value: FiniteDuration) extends CachePolicy {
if (value <= Duration.Zero) throw new IllegalArgumentException(s"TTL values must be a positive value.") if (value < Duration.Zero)
throw new IllegalArgumentException(s"TTL values must be a positive value (zero included).")
import akka.util.JavaDurationConverters._ import akka.util.JavaDurationConverters._
def getValue: java.time.Duration = value.asJava def getValue: java.time.Duration = value.asJava
@ -32,10 +33,16 @@ object CachePolicy {
object Ttl { object Ttl {
def unapply(ttl: Ttl): Option[FiniteDuration] = Some(ttl.value) def unapply(ttl: Ttl): Option[FiniteDuration] = Some(ttl.value)
def fromPositive(value: FiniteDuration): Ttl = { def fromPositive(value: FiniteDuration): Ttl = {
if (value <= Duration.Zero)
throw new IllegalArgumentException(
s"Positive TTL values must be a strictly positive value. Use Ttl.never for zero.")
new Ttl(value) new Ttl(value)
} }
def fromPositive(value: java.time.Duration): Ttl = fromPositive(value.asScala) def fromPositive(value: java.time.Duration): Ttl = fromPositive(value.asScala)
// DNS RFC states that zero values are interpreted to mean that the RR should not be cached
val never: Ttl = new Ttl(0.seconds)
// There's places where only a Ttl makes sense (DNS RFC says TTL is a positive 32 bit integer) // There's places where only a Ttl makes sense (DNS RFC says TTL is a positive 32 bit integer)
// but we know the value can be cached effectively forever (e.g. the Lookup name was the actual IP already) // but we know the value can be cached effectively forever (e.g. the Lookup name was the actual IP already)
val effectivelyForever: Ttl = fromPositive(Int.MaxValue.seconds) val effectivelyForever: Ttl = fromPositive(Int.MaxValue.seconds)

View file

@ -138,7 +138,10 @@ private[dns] object ResourceRecord {
val recType = it.getShort val recType = it.getShort
val recClass = it.getShort val recClass = it.getShort
// According to https://www.ietf.org/rfc/rfc1035.txt: "TTL: positive values of a signed 32 bit number." // According to https://www.ietf.org/rfc/rfc1035.txt: "TTL: positive values of a signed 32 bit number."
val ttl = Ttl.fromPositive(it.getInt.seconds) val ttl = (it.getInt: @switch) match {
case 0 => Ttl.never
case nonZero => Ttl.fromPositive(nonZero.seconds)
}
val rdLength = it.getShort val rdLength = it.getShort
val data = it.clone().take(rdLength) val data = it.clone().take(rdLength)
it.drop(rdLength) it.drop(rdLength)