Skip to content

Commit 098c09c

Browse files
committed
Remove Guava dependency by copying the RateLimiter.
1 parent c6b1a09 commit 098c09c

File tree

13 files changed

+2477
-40
lines changed

13 files changed

+2477
-40
lines changed

appender-fatjar/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@
4848
<pattern>org.apache.commons.pool2</pattern>
4949
<shadedPattern>${project.groupId}.redis.appender.commons.pool2</shadedPattern>
5050
</relocation>
51-
<relocation>
52-
<pattern>com.google</pattern>
53-
<shadedPattern>${project.groupId}.redis.appender.google</shadedPattern>
54-
</relocation>
5551
<relocation>
5652
<pattern>com.twitter</pattern>
5753
<shadedPattern>${project.groupId}.redis.appender.twitter</shadedPattern>

appender/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@
2020
<scope>provided</scope>
2121
</dependency>
2222

23-
<dependency>
24-
<groupId>com.google.guava</groupId>
25-
<artifactId>guava</artifactId>
26-
</dependency>
27-
2823
<dependency>
2924
<groupId>com.twitter</groupId>
3025
<artifactId>jsr166e</artifactId>

appender/src/main/java/com/vlkan/log4j2/redis/appender/RedisThrottler.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.vlkan.log4j2.redis.appender;
22

3-
import com.google.common.util.concurrent.RateLimiter;
3+
import com.vlkan.log4j2.redis.appender.guava.GuavaRateLimiter;
44

55
import javax.management.InstanceAlreadyExistsException;
66
import javax.management.JMX;
@@ -17,8 +17,6 @@
1717
import java.util.concurrent.TimeUnit;
1818
import java.util.concurrent.atomic.AtomicReference;
1919

20-
import static com.google.common.base.MoreObjects.firstNonNull;
21-
2220
class RedisThrottler implements AutoCloseable {
2321

2422
/**
@@ -43,9 +41,9 @@ class RedisThrottler implements AutoCloseable {
4341

4442
private final Thread flushTrigger;
4543

46-
private final RateLimiter eventRateLimiter;
44+
private final GuavaRateLimiter eventRateLimiter;
4745

48-
private final RateLimiter byteRateLimiter;
46+
private final GuavaRateLimiter byteRateLimiter;
4947

5048
private final DebugLogger logger;
5149

@@ -66,19 +64,20 @@ class RedisThrottler implements AutoCloseable {
6664
this.buffer = new ArrayBlockingQueue<>(config.getBufferSize());
6765
this.batch = new byte[config.getBatchSize()][];
6866
this.flushTrigger = createFlushTrigger();
69-
this.eventRateLimiter = config.getMaxEventCountPerSecond() > 0 ? RateLimiter.create(config.getMaxEventCountPerSecond()) : null;
70-
this.byteRateLimiter = config.getMaxByteCountPerSecond() > 0 ? RateLimiter.create(config.getMaxByteCountPerSecond()) : null;
67+
this.eventRateLimiter = config.getMaxEventCountPerSecond() > 0 ? GuavaRateLimiter.create(config.getMaxEventCountPerSecond()) : null;
68+
this.byteRateLimiter = config.getMaxByteCountPerSecond() > 0 ? GuavaRateLimiter.create(config.getMaxByteCountPerSecond()) : null;
7169
this.logger = new DebugLogger(RedisThrottler.class, debugEnabled);
7270
this.jmxBeanName = createJmxBeanName();
7371
}
7472

7573
private ObjectName createJmxBeanName() {
76-
String beanName = firstNonNull(
77-
config.getJmxBeanName(),
78-
String.format(
79-
"org.apache.logging.log4j2:type=%s,component=Appenders,name=%s,subtype=RedisThrottler",
80-
appender.getConfig().getLoggerContext().getName(),
81-
appender.getName()));
74+
String beanName = config.getJmxBeanName();
75+
if (beanName == null) {
76+
beanName = String.format(
77+
"org.apache.logging.log4j2:type=%s,component=Appenders,name=%s,subtype=RedisThrottler",
78+
appender.getConfig().getLoggerContext().getName(),
79+
appender.getName());
80+
}
8281
try {
8382
return new ObjectName(beanName);
8483
} catch (MalformedObjectNameException error) {

appender/src/main/java/com/vlkan/log4j2/redis/appender/RedisThrottlerInternalJmxBean.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.vlkan.log4j2.redis.appender;
22

3-
import com.google.common.base.MoreObjects;
43
import com.twitter.jsr166e.LongAdder;
54

65
public class RedisThrottlerInternalJmxBean implements RedisThrottlerJmxBean {
@@ -95,16 +94,15 @@ public void incrementRedisPushSuccessCount(int increment) {
9594

9695
@Override
9796
public String toString() {
98-
return MoreObjects
99-
.toStringHelper(this)
100-
.add("totalEventCount", totalEventCount.sum())
101-
.add("ignoredEventCount", ignoredEventCount.sum())
102-
.add("eventRateLimitFailureCount", eventRateLimitFailureCount.sum())
103-
.add("byteRateLimitFailureCount", byteRateLimitFailureCount.sum())
104-
.add("unavailableBufferSpaceFailureCount", unavailableBufferSpaceFailureCount.sum())
105-
.add("redisPushFailureCount", redisPushFailureCount.sum())
106-
.add("redisPushSuccessCount", redisPushSuccessCount.sum())
107-
.toString();
97+
return "RedisThrottlerInternalJmxBean{" +
98+
"totalEventCount=" + totalEventCount.sum() +
99+
", ignoredEventCount=" + ignoredEventCount.sum() +
100+
", eventRateLimitFailureCount=" + eventRateLimitFailureCount.sum() +
101+
", byteRateLimitFailureCount=" + byteRateLimitFailureCount.sum() +
102+
", unavailableBufferSpaceFailureCount=" + unavailableBufferSpaceFailureCount.sum() +
103+
", redisPushFailureCount=" + redisPushFailureCount.sum() +
104+
", redisPushSuccessCount=" + redisPushSuccessCount.sum() +
105+
'}';
108106
}
109107

110108
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (C) 2007 The Guava Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.vlkan.log4j2.redis.appender.guava;
16+
17+
public enum GuavaLongMath {;
18+
19+
/**
20+
* Returns the sum of {@code a} and {@code b} unless it would overflow or underflow in which case
21+
* {@code Long.MAX_VALUE} or {@code Long.MIN_VALUE} is returned, respectively.
22+
*/
23+
public static long saturatedAdd(long a, long b) {
24+
long naiveSum = a + b;
25+
if ((a ^ b) < 0 | (a ^ naiveSum) >= 0) {
26+
// If a and b have different signs or a has the same sign as the result then there was no
27+
// overflow, return.
28+
return naiveSum;
29+
}
30+
// we did over/under flow, if the sign is negative we should return MAX otherwise MIN
31+
return Long.MAX_VALUE + ((naiveSum >>> (Long.SIZE - 1)) ^ 1);
32+
}
33+
34+
}

0 commit comments

Comments
 (0)