All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.dropwizard.health.tcp.TcpHealthCheck Maven / Gradle / Ivy

Go to download

Provides a health check implementation that performs ongoing monitoring of an application's dependencies and includes an endpoint that can be called by a load balancer to determine if the application is healthy and thus able to retrieve traffic.

There is a newer version: 1.7.3
Show newest version
package io.dropwizard.health.tcp;

import com.codahale.metrics.health.HealthCheck;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.time.Duration;
import java.util.Objects;

import javax.annotation.Nonnull;

public class TcpHealthCheck extends HealthCheck {
    private static final Logger log = LoggerFactory.getLogger(TcpHealthCheck.class);

    private static final Duration DEFAULT_CONNECTION_TIMEOUT = Duration.ofSeconds(2);

    @Nonnull
    private final String host;
    private final int port;
    private final Duration connectionTimeout;

    public TcpHealthCheck(@Nonnull final String host,
                          final int port) {
        this(host, port, DEFAULT_CONNECTION_TIMEOUT);
    }

    public TcpHealthCheck(@Nonnull final String host,
                          final int port,
                          final Duration connectionTimeout) {
        this.host = Objects.requireNonNull(host);
        this.port = port;
        Preconditions.checkState(!connectionTimeout.isNegative(), "connectionTimeout must be a non-negative value.");
        Preconditions.checkState(connectionTimeout.toMillis() <= Integer.MAX_VALUE,
                "Cannot configure a connectionTimeout greater than the max integer value");
        this.connectionTimeout = connectionTimeout;
    }

    @Override
    protected Result check() throws IOException {
        final boolean isHealthy = tcpCheck(host, port);

        if (isHealthy) {
            log.debug("Health check against url={}:{} successful", host, port);
            return Result.healthy();
        }

        log.debug("Health check against url={}:{} failed", host, port);
        return Result.unhealthy("TCP health check against host=%s port=%s failed", host, port);
    }

    /**
     * Performs a health check via TCP against an external dependency.
     * By default uses the Java {@link java.net.Socket} API, but can be overridden to allow for different behavior.
     * @param host the host to check.
     * @param port the port to check.
     * @return whether the check was successful or not.
     */
    protected boolean tcpCheck(final String host, final int port) throws IOException {
        try (Socket socket = new Socket()) {
            socket.connect(new InetSocketAddress(host, port), (int) connectionTimeout.toMillis());
            return socket.isConnected();
        }
    }

    @VisibleForTesting
    Duration getConnectionTimeout() {
        return connectionTimeout;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy