aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/container/logging/CircularArrayAccessLogKeeper.java
blob: ba1437947286efc58d4fd65007d4bf2f9e565a99 (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
46
47
48
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.logging;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * This class keeps some information from the access log from the requests in memory. It is thread-safe.
 *
 * @author dybis
 */
public class CircularArrayAccessLogKeeper {
    public static final int SIZE = 1000;
    private final Deque<String> uris = new ArrayDeque<>(SIZE);
    private final Object monitor = new Object();

    /**
     *  This class is intended to be used with injection so it can be shared between other classes.
     */
    public CircularArrayAccessLogKeeper() {}

    /**
     * Creates a list of Uris.
     * @return URIs as string
     */
    public List<String> getUris() {
        final List<String> uriList = new ArrayList<>();
        synchronized (monitor) {
            uris.iterator().forEachRemaining(uri -> uriList.add(uri));
        }
        return uriList;
    }

    /**
     * Add a new URI. It might remove an old entry to make space for new entry.
     * @param uri uri as string
     */
    public void addUri(String uri) {
        synchronized (monitor) {
            if (uris.size() == SIZE) {
                uris.pop();
            }
            uris.add(uri);
        }
    }
}