Closed
Description
Description
dnsNameResolver
isn't closed when AlgoliaHttpClient.close()
is invoked. This can cause Caused by: java.net.SocketException: maximum number of DatagramSockets reached
.
This happens when running Playframework locally with its auto-reload magic. It keeps instantiating a new AlgoliaClient
when the code is changed. I use Play's shutdown hook to clean up AlgoliaClient
properly when the code is reloaded. Unfortunately, dnsNameResolver
isn't closed properly.
But, fortunately, dnsNameResolver
is a public member, so I still can close it myself. Thanks you for scoping these members to public.
Steps To Reproduce
Here's a simple code to reproduce it:
package scripts
import algolia.{AlgoliaClient, AlgoliaClientConfiguration, AlgoliaHttpClient}
import io.netty.bootstrap.ChannelFactory
import io.netty.channel.local.LocalEventLoopGroup
import io.netty.channel.socket.oio.OioDatagramChannel
import io.netty.resolver.dns.DnsNameResolverBuilder
object TestAlgolia {
def main(args: Array[String]): Unit = {
println(System.getProperty("sun.net.maxDatagramSockets"))
1.to(300).foreach { i =>
println(s"Create AlgoliaClient ${i}")
try {
val c = new AlgoliaClient("sdfsdfsdfsdf", "sdfsdfsdfsdfs")
c.close()
// Uncomment the below line, and the code will create 300 AlgoliaClient successfully.
// c.httpClient.dnsNameResolver.close()
} catch { case e: Throwable =>
e.printStackTrace()
}
}
}
}
Then, run with sbt -Dsun.net.maxDatagramSockets=3 'runMain scripts.TestAlgolia'
. It will fail after creating 3 AlgoliaClient
s.