summaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/host/ConfigRequestHostLivenessTracker.java
blob: bf28c94caf18283ec2dbe3cef1567baf920bca19 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.host;

import com.google.inject.Inject;
import com.yahoo.config.provision.HostLivenessTracker;

import java.time.Clock;
import java.time.Instant;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Keeps track of the last config request made by each hostname.
 * This always remembers all requests forever since the moment is is constructed.
 * This is the implementation which will be injected to components who request a HostLivenessTracker.
 * 
 * @author bratseth
 */
public class ConfigRequestHostLivenessTracker implements HostLivenessTracker {

    private final Clock clock;
    private final Map<String, Instant> lastRequestFromHost = new ConcurrentHashMap<>();

    @Inject
    @SuppressWarnings("unused")
    public ConfigRequestHostLivenessTracker() {
        this(Clock.systemUTC());
    }
    
    public ConfigRequestHostLivenessTracker(Clock clock) {
        this.clock = clock;
    }
            
    @Override
    public void receivedRequestFrom(String hostname) {
        lastRequestFromHost.put(hostname, clock.instant());
    }

    @Override
    public Optional<Instant> lastRequestFrom(String hostname) {
        return Optional.ofNullable(lastRequestFromHost.get(hostname));
    }

}