Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting DNS lookup's TimeToLive in Scala Play

I am trying to set the TimeToLive setting for DNS Lookup in my Scala-Play application. I use Play 2.5.9 and Scala 2.11.8 and follow the AWS guide. I tried the following ways:

  • in application.conf

    // Set DNS lookup time-to-live to one minute
    networkaddress.cache.ttl=1
    networkaddress.cache.negative.ttl=1
    
  • in AppModule or EagerSingleton (the code would be similar)

    class AppModule() extends AbstractModule {
      Security.setProperty("networkaddress.cache.ttl", "1")
      Security.setProperty("networkaddress.cache.negative.ttl", "1")
      ...
    }
    
  • passing as environment variable:

    sbt -Dsun.net.inetaddr.ttl=1 clean run
    

I have the following piece of test code in the application:

for (i <- 1 to 25) {
  System.out.println(java.net.InetAddress.getByName("google.com").getHostAddress())
  Thread.sleep(1000)
}

This always prints the same IP address, e.g. 216.58.212.206. To me it looks like none of the approaches specified above have any effect. However, maybe I am testing something else and not actually the value of TTL. Therefore, I have two questions:

  • what is the correct way to pass a security variable into a Play application?
  • how to test it?
like image 503
Oleksandr Volynets Avatar asked Nov 29 '16 20:11

Oleksandr Volynets


People also ask

How do I fix slow DNS lookups?

One of the easiest ways to reduce DNS lookups is to move as many resources as you can to your CDN provider. If you run your site through Pingdom, you can see the total number of requests by domain. As you can see in this example, 93.8% of the requests are to the CDN URL.


1 Answers

To change the settings for DNS cache via java.security.Security you have to provide a custom application loader.

package modules
class ApplicationLoader extends GuiceApplicationLoader {
  override protected def builder(context: Context): GuiceApplicationBuilder = {
    java.security.Security.setProperty("networkaddress.cache.ttl", "1")
    super.builder(context)
  }
}

When you build this application loader you can enable it in your application.conf

play.application.loader = "modules.ApplicationLoader"

after that you could use your code above and check if the DNS cache is behaving like you set it up. But keep in mind that your system is accessing a DNS server which is caching itself so you wont see change then. If you want to be sure that you get different addresses for google.com you should use an authority name server like ns1.google.com

If you want to write a test on that you could maybe write a test which requests the address and then waits for the specified amount of time until it resolves again. But with a DNS system out of your control like google.com this could be a problem, if you hit a DNS server with caching. If you want to write such a check you could do it with

@RunWith(classOf[JUnitRunner])
class DnsTests extends FlatSpec with Matchers {

  "DNS Cache ttl" should "refresh after 1 second" 
    in new WithApplicationLoader(new modules.ApplicationLoader) {

    // put your test code here

  }
}

As you can see you can put the custom application loader in the context of the application starting behind your test.

like image 59
Björn Köster Avatar answered Oct 25 '22 04:10

Björn Köster