Skip to content

Commit aa5cfb7

Browse files
committed
Add kinds of fault-tolerance strategies and add TimeUnitUtils for time unit conversion
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
1 parent bd151bc commit aa5cfb7

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

src/main/java/io/opensergo/ConfigKind.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,26 @@ public enum ConfigKind {
2323
/**
2424
* FaultToleranceRule
2525
*/
26-
FAULT_TOLERANCE_RULE("fault-tolerance.opensergo.io/v1alpha1/FaultToleranceRule"),
27-
RATE_LIMIT_STRATEGY("fault-tolerance.opensergo.io/v1alpha1/RateLimitStrategy");
26+
FAULT_TOLERANCE_RULE("fault-tolerance.opensergo.io/v1alpha1/FaultToleranceRule", "FaultToleranceRule"),
27+
RATE_LIMIT_STRATEGY("fault-tolerance.opensergo.io/v1alpha1/RateLimitStrategy", "RateLimitStrategy"),
28+
THROTTLING_STRATEGY("fault-tolerance.opensergo.io/v1alpha1/ThrottlingStrategy", "ThrottlingStrategy"),
29+
CONCURRENCY_LIMIT_STRATEGY("fault-tolerance.opensergo.io/v1alpha1/ConcurrencyLimitStrategy",
30+
"ConcurrencyLimitStrategy"),
31+
CIRCUIT_BREAKER_STRATEGY("fault-tolerance.opensergo.io/v1alpha1/CircuitBreakerStrategy", "CircuitBreakerStrategy");
2832

2933
private final String kindName;
34+
private final String simpleKindName;
3035

31-
ConfigKind(String kindName) {
36+
ConfigKind(String kindName, String simpleKindName) {
3237
this.kindName = kindName;
38+
this.simpleKindName = simpleKindName;
3339
}
3440

3541
public String getKindName() {
3642
return kindName;
3743
}
44+
45+
public String getSimpleKindName() {
46+
return simpleKindName;
47+
}
3848
}

src/main/java/io/opensergo/OpenSergoConfigKindRegistry.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
import java.util.concurrent.ConcurrentHashMap;
1919
import java.util.concurrent.ConcurrentMap;
2020

21+
import io.opensergo.proto.fault_tolerance.v1.CircuitBreakerStrategy;
22+
import io.opensergo.proto.fault_tolerance.v1.ConcurrencyLimitStrategy;
2123
import io.opensergo.proto.fault_tolerance.v1.FaultToleranceRule;
2224
import io.opensergo.proto.fault_tolerance.v1.RateLimitStrategy;
25+
import io.opensergo.proto.fault_tolerance.v1.ThrottlingStrategy;
2326
import io.opensergo.util.StringUtils;
2427

2528
/**
@@ -36,6 +39,9 @@ public final class OpenSergoConfigKindRegistry {
3639

3740
registerConfigKind(ConfigKind.FAULT_TOLERANCE_RULE, FaultToleranceRule.class);
3841
registerConfigKind(ConfigKind.RATE_LIMIT_STRATEGY, RateLimitStrategy.class);
42+
registerConfigKind(ConfigKind.THROTTLING_STRATEGY, ThrottlingStrategy.class);
43+
registerConfigKind(ConfigKind.CONCURRENCY_LIMIT_STRATEGY, ConcurrencyLimitStrategy.class);
44+
registerConfigKind(ConfigKind.CIRCUIT_BREAKER_STRATEGY, CircuitBreakerStrategy.class);
3945
}
4046

4147
public static ConfigKindMetadata getKindMetadata(ConfigKind kind) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2022, OpenSergo Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.opensergo.util;
17+
18+
import io.opensergo.proto.common.v1.TimeUnit;
19+
20+
/**
21+
* @author Eric Zhao
22+
*/
23+
public final class TimeUnitUtils {
24+
25+
public static long convertToMillis(long t, TimeUnit fromUnit) {
26+
AssertUtils.assertNotNull(fromUnit, "fromUnit cannot be null");
27+
switch (fromUnit) {
28+
case DAY:
29+
return java.util.concurrent.TimeUnit.DAYS.toMillis(t);
30+
case HOUR:
31+
return java.util.concurrent.TimeUnit.HOURS.toMillis(t);
32+
case MINUTE:
33+
return java.util.concurrent.TimeUnit.MINUTES.toMillis(t);
34+
case SECOND:
35+
return java.util.concurrent.TimeUnit.SECONDS.toMillis(t);
36+
case MILLISECOND:
37+
return java.util.concurrent.TimeUnit.MILLISECONDS.toMillis(t);
38+
case UNKNOWN:
39+
default:
40+
throw new IllegalArgumentException("unknown time unit: " + fromUnit);
41+
}
42+
}
43+
44+
private TimeUnitUtils() {}
45+
}

0 commit comments

Comments
 (0)