Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
don't do checks whether socket is available
Browse files Browse the repository at this point in the history
  • Loading branch information
mavilein committed Feb 21, 2018
1 parent 939ca05 commit e2f5089
Showing 1 changed file with 26 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.prisma.metrics

import java.net.{InetAddress, InetSocketAddress, Socket}
import java.net.{DatagramSocket, InetAddress, InetSocketAddress, Socket}
import java.util.concurrent.Callable

import scala.concurrent.Await
Expand All @@ -25,14 +25,15 @@ case class StatsdHostLookup(dnsName: String, port: Int, reachableTimeout: Int) e
resolveAndPutIntoCache()

case Some(inetSocketAddr) =>
val isReachable = doesServerListenOnSocketAddress(inetSocketAddr)
if (isReachable) {
log(s"isReachable: ${pretty(inetSocketAddr)}")
inetSocketAddr
} else {
log(s"socket address was not reachable anymore")
resolveAndPutIntoCache()
}
// val isReachable = doesServerListenOnSocketAddress(inetSocketAddr)
// if (isReachable) {
// log(s"isReachable: ${pretty(inetSocketAddr)}")
// inetSocketAddr
// } else {
// log(s"socket address was not reachable anymore")
// resolveAndPutIntoCache()
// }
inetSocketAddr
}
}

Expand All @@ -46,22 +47,22 @@ case class StatsdHostLookup(dnsName: String, port: Int, reachableTimeout: Int) e

def pretty(socketAddress: InetSocketAddress) = s"IP:${socketAddress.getAddress.getHostAddress} Port:${socketAddress.getPort}"

def doesServerListenOnSocketAddress(socketAddress: InetSocketAddress): Boolean = {
Try {
val socket = new Socket()
socket.connect(socketAddress, 500)
socket
} match {
case Success(socket) =>
Try(socket.close())
true

case Failure(exception) =>
log(s"failed with the following exception")
exception.printStackTrace()
false
}
}
// def doesServerListenOnSocketAddress(socketAddress: InetSocketAddress): Boolean = {
// Try {
// val socket = new DatagramSocket()
// socket.connect(socketAddress)
// socket
// } match {
// case Success(socket) =>
// Try(socket.close())
// true
//
// case Failure(exception) =>
// log(s"failed with the following exception")
// exception.printStackTrace()
// false
// }
// }

def log(msg: String): Unit = println(s"[${this.getClass.getSimpleName}] $msg")
}

0 comments on commit e2f5089

Please sign in to comment.