Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizable DNS resolution #461

Open
13h3r opened this issue Jan 22, 2022 · 0 comments
Open

Customizable DNS resolution #461

13h3r opened this issue Jan 22, 2022 · 0 comments

Comments

@13h3r
Copy link

13h3r commented Jan 22, 2022

The current implementation of name resolution relies on the JVM name resolution implementation and in case of temporary DNS unavailability can't establish a connection to a remote host when the cached DNS record expires. Returning the latest know IP address might not be the right solution for all cases, but for some cases, it could be a reasonable fallback in case of DNS unavailability.

The primary use-case for this is API clients that are connecting to a limited number of endpoints and would like to have these connections reliable.

This issue is to discuss the need for such functionality in zio-nio. The new layer will be a simple String => IO[..., SocketAddress] and provide a few built-in building blocks like a fallback to the latest know address, background refresh (to avoid blocking calls when cache TTL expires), and others.

The API could be like (PoC)

package zio.nio

import zio.{Ref, UIO, ZIO}

import java.net.UnknownHostException

trait ZNameResolver[R, E] {
  def addr(): ZIO[R, E, InetAddress]
}

object ZNameResolver {
  type UNameResolver = ZNameResolver[Any, Nothing]
  type URNameResolver[R] = ZNameResolver[R, Nothing]
  type NameResolver[E] = ZNameResolver[Any, E]

  def regular(name: String): NameResolver[UnknownHostException] = () => InetAddress.byName(name)

  def onceAndForever[R, E](nameResolver: ZNameResolver[R, E]): ZIO[R, E, UNameResolver] = {
    nameResolver.addr().map(addr => () => UIO.succeed(addr))
  }

  def cacheLatestSuccessful[R, E](resolver: ZNameResolver[R, E]): ZIO[R, E, URNameResolver[R]] = {
    for {
      intialAddr <- resolver.addr()
      ref <- Ref.make(intialAddr)
    } yield () => resolver
      .addr()
      .tap(addr => ref.set(addr))
      .catchAll(_ => ref.get)
  }
}

This layer can also provide additional aspects:

  • rotation among multiple addresses that corresponds to a single hostname
  • periodical checking of network reachability and eviction of unreachable addresses from addresses pool
  • support for SRV type of DNS records
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant