<commit_before>package org.springframework.cloud.consul.discovery;

import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.agent.model.NewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by nicu on 11.03.2015.
 */
@Slf4j
public class TtlScheduler implements ApplicationContextAware {
    private static final int DEFAULT_TTL = 3; // must be > 1
    public static final int HEARTBEAT_INTERVAL_RATIO = 2 / 3;

    private final Map<String, Long> serviceHeartbeats = new ConcurrentHashMap<>();
    private final AtomicBoolean heartbeatingNow = new AtomicBoolean();
    private volatile int ttl;
    private volatile int heartbeatInterval;

    @Autowired
    private ConsulClient client;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        ttl = context.getEnvironment().getProperty("consul.ttl", Integer.class, DEFAULT_TTL);
        ttl = Math.min(2, ttl);
        // heartbeat at 2/3 ttl, but no later than ttl -1s and, (under lesser priority), no sooner than 1s from now
        heartbeatInterval = Math.max(ttl - 1, Math.min(ttl * HEARTBEAT_INTERVAL_RATIO, 1));
    }

    /**
     * Add a service to the checks loop.
     */
    public void add(final NewService service) {
        serviceHeartbeats.put(service.getId(), 0L);
    }

    public void remove(String serviceId) {
        serviceHeartbeats.remove(serviceId);
    }

    public int getTTL() {
        return ttl;
    }

    @Scheduled(initialDelay = 0, fixedRate = 100)
    void heartbeatServices() {
        if (heartbeatingNow.compareAndSet(false, true)) {
            for (String serviceId : serviceHeartbeats.keySet()) {
                long latestHeartbeatDoneForService = serviceHeartbeats.get(serviceId);
                if(latestHeartbeatDoneForService + heartbeatInterval <= System.currentTimeMillis()) {
                    client.agentCheckPass(serviceId);
                    serviceHeartbeats.put(serviceId, System.currentTimeMillis());
                }
            }
        }
    }
}<commit_msg>@Value for properties<commit_after>package org.springframework.cloud.consul.discovery;

import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.agent.model.NewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by nicu on 11.03.2015.
 */
@Slf4j
@ConfigurationProperties
public class TtlScheduler {

	private final Map<String, Long> serviceHeartbeats = new ConcurrentHashMap<>();
	private final AtomicBoolean heartbeatingNow = new AtomicBoolean();

    @Value("${consul.ttl:3}")
	private volatile int ttl;

    @Value("${consul.heartbeatIntervalRatio:0.6}")
    private volatile int heartbeatIntervalRatio;

	private volatile int heartbeatInterval;

	@Autowired
	private ConsulClient client;

	@PostConstruct
    public void computeHeartbeatInterval() {
        // heartbeat rate at ratio * ttl, but no later than ttl -1s and, (under lesser priority), no sooner than 1s from now
        heartbeatInterval = Math.max(ttl - 1, Math.min(ttl * heartbeatIntervalRatio, 1));
    }

	/**
	 * Add a service to the checks loop.
	 */
	public void add(final NewService service) {
		serviceHeartbeats.put(service.getId(), 0L);
	}

	public void remove(String serviceId) {
		serviceHeartbeats.remove(serviceId);
	}

	public int getTTL() {
		return ttl;
	}

	@Scheduled(initialDelay = 0, fixedRate = 100)
	private void heartbeatServices() {
		if (heartbeatingNow.compareAndSet(false, true)) {
			for (String serviceId : serviceHeartbeats.keySet()) {
				long latestHeartbeatDoneForService = serviceHeartbeats.get(serviceId);
				if (latestHeartbeatDoneForService + heartbeatInterval <= System
						.currentTimeMillis()) {
					client.agentCheckPass(serviceId);
					serviceHeartbeats.put(serviceId, System.currentTimeMillis());
				}
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.0969557964970809, 'fraction_numerical': 0.011259382819015847, 'mean_word_length': 3.996875, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2410787', 'n_tokens_mistral': 1480, 'n_tokens_neox': 1375, 'n_words': 315}