]> git.basschouten.com Git - openhab-addons.git/blob
0a71c4b77d888db9c199c611db690fc76c6c5b81
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.tesla.internal.throttler;
14
15 import java.util.Iterator;
16 import java.util.LinkedList;
17 import java.util.ListIterator;
18 import java.util.concurrent.TimeUnit;
19
20 /**
21  * The {@link Rate} defines a rate limiter that accepts a number of calls to be
22  * executed in a given time length. If the quota of calls is used, then calls
23  * are scheduled for the next block of time
24  *
25  * @author Karel Goderis - Initial contribution
26  */
27 public final class Rate {
28
29     private final int numberCalls;
30     private final int timeLength;
31     private final TimeUnit timeUnit;
32     private final LinkedList<Long> callHistory = new LinkedList<>();
33
34     public Rate(int numberCalls, int timeLength, TimeUnit timeUnit) {
35         this.numberCalls = numberCalls;
36         this.timeLength = timeLength;
37         this.timeUnit = timeUnit;
38     }
39
40     public long timeInMillis() {
41         return timeUnit.toMillis(timeLength);
42     }
43
44     void addCall(long callTime) {
45         callHistory.addLast(callTime);
46     }
47
48     private void cleanOld(long now) {
49         ListIterator<Long> i = callHistory.listIterator();
50         long threshold = now - timeInMillis();
51         while (i.hasNext()) {
52             if (i.next() <= threshold) {
53                 i.remove();
54             } else {
55                 break;
56             }
57         }
58     }
59
60     long callTime(long now) {
61         cleanOld(now);
62         if (callHistory.size() < numberCalls) {
63             return now;
64         }
65         long lastStart = callHistory.getLast() - timeInMillis();
66         long firstPeriodCall = lastStart, call;
67         int count = 0;
68         Iterator<Long> i = callHistory.descendingIterator();
69         while (i.hasNext()) {
70             call = i.next();
71             if (call < lastStart) {
72                 break;
73             } else {
74                 count++;
75                 firstPeriodCall = call;
76             }
77         }
78         if (count < numberCalls) {
79             return firstPeriodCall + 1;
80         } else {
81             return firstPeriodCall + timeInMillis() + 1;
82         }
83     }
84 }