Skip to content

Commit 410ad18

Browse files
committed
parameterizable invocation gate delay
Signed-off-by: Ceki Gulcu <[email protected]>
1 parent 2946e3b commit 410ad18

File tree

7 files changed

+44
-17
lines changed

7 files changed

+44
-17
lines changed

logback-core/src/main/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import ch.qos.logback.core.rolling.helper.CompressionMode;
2424
import ch.qos.logback.core.rolling.helper.FileFilterUtil;
2525
import ch.qos.logback.core.rolling.helper.SizeAndTimeBasedArchiveRemover;
26+
import ch.qos.logback.core.util.Duration;
2627
import ch.qos.logback.core.util.FileSize;
2728
import ch.qos.logback.core.util.DefaultInvocationGate;
2829
import ch.qos.logback.core.util.InvocationGate;
@@ -40,7 +41,7 @@ enum Usage {
4041
volatile int currentPeriodsCounter = 0;
4142
FileSize maxFileSize;
4243

43-
Integer checkIncrement = null;
44+
Duration checkIncrement = null;
4445

4546
static String MISSING_INT_TOKEN = "Missing integer token, that is %i, in FileNamePattern [";
4647
static String MISSING_DATE_TOKEN = "Missing date token, that is %d, in FileNamePattern [";
@@ -185,11 +186,11 @@ private boolean checkSizeBasedTrigger(File activeFile, long currentTime) {
185186
return false;
186187
}
187188

188-
public Integer getCheckIncrement() {
189+
public Duration getCheckIncrement() {
189190
return checkIncrement;
190191
}
191192

192-
public void setCheckIncrement(Integer checkIncrement) {
193+
public void setCheckIncrement(Duration checkIncrement) {
193194
this.checkIncrement = checkIncrement;
194195
}
195196

logback-core/src/main/java/ch/qos/logback/core/rolling/SizeBasedTriggeringPolicy.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import java.io.File;
1717

18+
import ch.qos.logback.core.util.Duration;
1819
import ch.qos.logback.core.util.FileSize;
1920
import ch.qos.logback.core.util.DefaultInvocationGate;
2021
import ch.qos.logback.core.util.InvocationGate;
@@ -41,10 +42,7 @@ public class SizeBasedTriggeringPolicy<E> extends TriggeringPolicyBase<E> {
4142

4243
FileSize maxFileSize = new FileSize(DEFAULT_MAX_FILE_SIZE);
4344
InvocationGate invocationGate = new SimpleInvocationGate();
44-
45-
46-
47-
Integer checkIncrement = null;
45+
Duration checkIncrement = null;
4846

4947
public SizeBasedTriggeringPolicy() {
5048
}
@@ -72,11 +70,11 @@ public void setMaxFileSize(FileSize aMaxFileSize) {
7270
this.maxFileSize = aMaxFileSize;
7371
}
7472

75-
public Integer getCheckIncrement() {
73+
public Duration getCheckIncrement() {
7674
return checkIncrement;
7775
}
7876

79-
public void setCheckIncrement(Integer checkIncrement) {
77+
public void setCheckIncrement(Duration checkIncrement) {
8078
this.checkIncrement = checkIncrement;
8179
}
8280
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Logback: the reliable, generic, fast and flexible logging framework.
3+
* Copyright (C) 1999-2023, 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.ExecutorService;
18+
19+
public class AlternateExecutorServiceUtil {
20+
21+
static public ExecutorService newThreadPoolExecutor() {
22+
return ExecutorServiceUtil.newThreadPoolExecutor();
23+
}
24+
}

logback-core/src/main/java/ch/qos/logback/core/util/SimpleInvocationGate.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ public class SimpleInvocationGate implements InvocationGate {
2929
//volatile long next = 0;
3030

3131
AtomicLong atomicNext = new AtomicLong(0);
32-
final long increment;
33-
final public static int DEFAULT_INCREMENT = 10_000;
32+
final Duration increment;
33+
34+
// 60 seconds by default
35+
final public static Duration DEFAULT_INCREMENT = Duration.buildBySeconds(60);
3436

3537
public SimpleInvocationGate() {
3638
this(DEFAULT_INCREMENT);
3739
}
3840

39-
public SimpleInvocationGate(int anIncrement) {
41+
public SimpleInvocationGate(Duration anIncrement) {
4042
this.increment = anIncrement;
4143
}
4244

@@ -47,7 +49,7 @@ public boolean isTooSoon(long currentTime) {
4749

4850
long localNext = atomicNext.get();
4951
if (currentTime >= localNext) {
50-
long next2 = currentTime+increment;
52+
long next2 = currentTime+increment.getMilliseconds();
5153
// if success, we were able to set the variable, otherwise some other thread beat us to it
5254
boolean success = atomicNext.compareAndSet(localNext, next2);
5355
// while we have crossed 'next', the other thread already returned true. There is

logback-core/src/test/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP_Test.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.concurrent.ExecutionException;
2121
import java.util.function.UnaryOperator;
2222

23+
import ch.qos.logback.core.util.Duration;
2324
import ch.qos.logback.core.util.StatusPrinter;
2425
import org.junit.jupiter.api.BeforeEach;
2526
import org.junit.jupiter.api.Test;
@@ -61,7 +62,7 @@ private void initRollingFileAppender(RollingFileAppender<Object> rfa, String fil
6162
private void initPolicies(RollingFileAppender<Object> rfa, TimeBasedRollingPolicy<Object> tbrp,
6263
String filenamePattern, int sizeThreshold, long givenTime, long lastCheck) {
6364
sizeAndTimeBasedFNATP = new SizeAndTimeBasedFNATP<Object>();
64-
sizeAndTimeBasedFNATP.setCheckIncrement(10);
65+
sizeAndTimeBasedFNATP.setCheckIncrement(Duration.buildByMilliseconds(10));
6566
tbrp.setContext(context);
6667
sizeAndTimeBasedFNATP.setMaxFileSize(new FileSize(sizeThreshold));
6768
tbrp.setTimeBasedFileNamingAndTriggeringPolicy(sizeAndTimeBasedFNATP);

logback-core/src/test/java/ch/qos/logback/core/rolling/SizeBasedRollingTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.io.IOException;
1717
import java.util.List;
1818

19+
import ch.qos.logback.core.util.Duration;
1920
import org.junit.jupiter.api.Assertions;
2021
import org.junit.jupiter.api.BeforeEach;
2122
import org.junit.jupiter.api.Test;
@@ -71,7 +72,7 @@ void generic(String testName, String fileName, String filenamePattern, List<Stri
7172
initRFA(randomOutputDir + fileName);
7273

7374
sizeBasedTriggeringPolicy.setMaxFileSize(new FileSize(100));
74-
sizeBasedTriggeringPolicy.setCheckIncrement(50);
75+
sizeBasedTriggeringPolicy.setCheckIncrement(Duration.buildByMilliseconds(50));
7576
fwrp.setMinIndex(0);
7677
fwrp.setFileNamePattern(randomOutputDir + filenamePattern);
7778

logback-core/src/test/java/ch/qos/logback/core/util/InvocationGateTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class InvocationGateTest {
4343
@Test
4444
public void smoke() {
4545
InvocationGate sig = new SimpleInvocationGate();
46-
int currentTime = SimpleInvocationGate.DEFAULT_INCREMENT + 1;
46+
long currentTime = SimpleInvocationGate.DEFAULT_INCREMENT.getMilliseconds() + 1;
4747
assertFalse(sig.isTooSoon(currentTime));
4848
currentTime++;
4949
assertTrue(sig.isTooSoon(currentTime));
@@ -52,7 +52,7 @@ public void smoke() {
5252
@Disabled
5353
@Test
5454
void checkThreadSafety() throws InterruptedException {
55-
InvocationGate sig = new SimpleInvocationGate(1);
55+
InvocationGate sig = new SimpleInvocationGate(Duration.buildByMilliseconds(1));
5656

5757
long initialTime = currentTime.get();
5858
sig.isTooSoon(initialTime); // sync invocation gate with current time

0 commit comments

Comments
 (0)