View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2022, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.core.util;
16  
17  import java.util.concurrent.atomic.AtomicLong;
18  import java.util.function.LongBinaryOperator;
19  import java.util.function.LongUnaryOperator;
20  import java.util.function.UnaryOperator;
21  
22  /**
23   * An invocation gate using very simple logic.
24   *
25   * @since 1.3.6/1.4.6
26   */
27  public class SimpleInvocationGate implements InvocationGate {
28  
29      //volatile long next = 0;
30  
31      AtomicLong atomicNext = new AtomicLong(0);
32      final Duration increment;
33  
34      // 60 seconds by default
35      final public static Duration DEFAULT_INCREMENT = Duration.buildBySeconds(60);
36  
37      public SimpleInvocationGate() {
38          this(DEFAULT_INCREMENT);
39      }
40  
41      public SimpleInvocationGate(Duration anIncrement) {
42          this.increment = anIncrement;
43      }
44  
45      @Override
46      public boolean isTooSoon(long currentTime) {
47          if (currentTime == -1)
48              return false;
49  
50          long localNext = atomicNext.get();
51          if (currentTime >= localNext) {
52              long next2 = currentTime+increment.getMilliseconds();
53              // if success, we were able to set the variable, otherwise some other thread beat us to it
54              boolean success = atomicNext.compareAndSet(localNext, next2);
55              // while we have crossed 'next', the other thread already returned true. There is
56              // no point in letting more than one thread per duration.
57              return !success;
58          } else {
59              return true;
60          }
61  
62      }
63  
64  
65  }
66  
67  //    private final boolean isTooSoonSynchronized(long currentTime) {
68  //        if (currentTime == -1)
69  //            return false;
70  //
71  //        synchronized (this) {
72  //            if (currentTime >= next) {
73  //                next = currentTime + increment;
74  //                return false;
75  //            }
76  //        }
77  //        return true;
78  //    }
79