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

no.nav.common.leaderelection.LeaderElectionHttpClient Maven / Gradle / Ivy

There is a newer version: 2.2021.02.03_08.08-9d0c8281f06b
Show newest version
package no.nav.common.leaderelection;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import no.nav.common.json.JsonUtils;
import no.nav.common.utils.EnvironmentUtils;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;

import static java.lang.String.format;

@Slf4j
public class LeaderElectionHttpClient implements LeaderElectionClient {

    private final static Logger LOGGER = LoggerFactory.getLogger(LeaderElectionHttpClient.class);

    private final OkHttpClient client;

    public LeaderElectionHttpClient() {
        this.client = new OkHttpClient();
    }

    @SneakyThrows
    @Override
    public boolean isLeader() {
        String electorPath = EnvironmentUtils.getRequiredProperty("ELECTOR_PATH");

        Request request = new Request.Builder()
                .url("http://" + electorPath)
                .get()
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                LOGGER.error(format("Received unexpected status from leader election sidecar %d:%s", response.code(), response.message()));
                return false;
            }

            LeaderResponse leader = JsonUtils.fromJson(response.body().string(), LeaderResponse.class);

            return InetAddress.getLocalHost().getHostName().equals(leader.getName());
        } catch (Exception e) {
            LOGGER.error("Failed to check if pod is leader", e);
            return false;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy