-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support custom traffic shaping controller
- Loading branch information
Showing
9 changed files
with
291 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...ava/com/alibaba/csp/sentinel/slots/block/flow/LoggingTrafficShapingControllerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 1999-2024 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.csp.sentinel.slots.block.flow; | ||
|
||
import com.alibaba.csp.sentinel.log.RecordLog; | ||
|
||
import java.util.Objects; | ||
|
||
public class LoggingTrafficShapingControllerFactory implements TrafficShapingControllerFactory { | ||
|
||
private final TrafficShapingControllerFactory delegate; | ||
|
||
public LoggingTrafficShapingControllerFactory(TrafficShapingControllerFactory delegate) { | ||
this.delegate = Objects.requireNonNull(delegate, "delegate must be not null."); | ||
} | ||
|
||
@Override | ||
public TrafficShapingController create(FlowRule rule) { | ||
RecordLog.debug("Creating traffic shaping controller '" + delegate.getClass().getName() + "' for rule: " + rule); | ||
return delegate.create(rule); | ||
} | ||
|
||
@Override | ||
public int getControlBehavior() { | ||
return delegate.getControlBehavior(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...ain/java/com/alibaba/csp/sentinel/slots/block/flow/TrafficShapingControllerFactories.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 1999-2024 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.csp.sentinel.slots.block.flow; | ||
|
||
import com.alibaba.csp.sentinel.spi.SpiLoader; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.BinaryOperator; | ||
import java.util.stream.Collectors; | ||
|
||
public class TrafficShapingControllerFactories { | ||
|
||
private static final Map<Integer, TrafficShapingControllerFactory> FACTORIES; | ||
|
||
static { | ||
FACTORIES = initFactories(); | ||
} | ||
|
||
/** | ||
* Using existing factory if the factory with the same control behavior already exists, because the existing factory has higher priority | ||
*/ | ||
private static BinaryOperator<TrafficShapingControllerFactory> usingExisting() { | ||
return (existing, replacement) -> existing; | ||
} | ||
|
||
private static TrafficShapingControllerFactory logging(TrafficShapingControllerFactory factory) { | ||
return new LoggingTrafficShapingControllerFactory(factory); | ||
} | ||
|
||
private static Map<Integer, TrafficShapingControllerFactory> initFactories() { | ||
return SpiLoader.of(TrafficShapingControllerFactory.class) | ||
.loadInstanceListSorted() | ||
.stream() | ||
.collect(Collectors.toMap(TrafficShapingControllerFactory::getControlBehavior, | ||
TrafficShapingControllerFactories::logging, | ||
usingExisting(), | ||
HashMap::new)); | ||
} | ||
|
||
public static TrafficShapingControllerFactory get(int controlBehavior) { | ||
return FACTORIES.get(controlBehavior); | ||
} | ||
|
||
private TrafficShapingControllerFactories() { | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
.../main/java/com/alibaba/csp/sentinel/slots/block/flow/TrafficShapingControllerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 1999-2024 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.csp.sentinel.slots.block.flow; | ||
|
||
/** | ||
* a factory interface to create TrafficShapingController instance | ||
*/ | ||
public interface TrafficShapingControllerFactory { | ||
|
||
/** | ||
* create a TrafficShapingController instance | ||
* @param rule flow rule | ||
* @return a new TrafficShapingController instance | ||
*/ | ||
TrafficShapingController create(FlowRule rule); | ||
|
||
/** | ||
* get the factory control behavior | ||
* @return the control behavior | ||
*/ | ||
int getControlBehavior(); | ||
} |
34 changes: 34 additions & 0 deletions
34
...n/java/com/alibaba/csp/sentinel/slots/block/flow/controller/DefaultControllerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 1999-2024 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.csp.sentinel.slots.block.flow.controller; | ||
|
||
import com.alibaba.csp.sentinel.slots.block.RuleConstant; | ||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; | ||
import com.alibaba.csp.sentinel.slots.block.flow.TrafficShapingController; | ||
import com.alibaba.csp.sentinel.slots.block.flow.TrafficShapingControllerFactory; | ||
|
||
public class DefaultControllerFactory implements TrafficShapingControllerFactory { | ||
|
||
@Override | ||
public TrafficShapingController create(FlowRule rule) { | ||
return new DefaultController(rule.getCount(), rule.getGrade()); | ||
} | ||
|
||
@Override | ||
public int getControlBehavior() { | ||
return RuleConstant.CONTROL_BEHAVIOR_DEFAULT; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...ava/com/alibaba/csp/sentinel/slots/block/flow/controller/ThrottlingControllerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 1999-2024 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.csp.sentinel.slots.block.flow.controller; | ||
|
||
import com.alibaba.csp.sentinel.slots.block.RuleConstant; | ||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; | ||
import com.alibaba.csp.sentinel.slots.block.flow.TrafficShapingController; | ||
import com.alibaba.csp.sentinel.slots.block.flow.TrafficShapingControllerFactory; | ||
|
||
public class ThrottlingControllerFactory implements TrafficShapingControllerFactory { | ||
|
||
@Override | ||
public TrafficShapingController create(FlowRule rule) { | ||
return new ThrottlingController(rule.getMaxQueueingTimeMs(), rule.getCount()); | ||
} | ||
|
||
@Override | ||
public int getControlBehavior() { | ||
return RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...in/java/com/alibaba/csp/sentinel/slots/block/flow/controller/WarmUpControllerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 1999-2024 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.csp.sentinel.slots.block.flow.controller; | ||
|
||
import com.alibaba.csp.sentinel.config.SentinelConfig; | ||
import com.alibaba.csp.sentinel.slots.block.RuleConstant; | ||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; | ||
import com.alibaba.csp.sentinel.slots.block.flow.TrafficShapingController; | ||
import com.alibaba.csp.sentinel.slots.block.flow.TrafficShapingControllerFactory; | ||
|
||
public class WarmUpControllerFactory implements TrafficShapingControllerFactory { | ||
|
||
static volatile int coldFactor = SentinelConfig.coldFactor(); | ||
|
||
@Override | ||
public TrafficShapingController create(FlowRule rule) { | ||
return new WarmUpController(rule.getCount(), rule.getWarmUpPeriodSec(), coldFactor); | ||
} | ||
|
||
@Override | ||
public int getControlBehavior() { | ||
return RuleConstant.CONTROL_BEHAVIOR_WARM_UP; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
.../alibaba/csp/sentinel/slots/block/flow/controller/WarmUpRateLimiterControllerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 1999-2024 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.csp.sentinel.slots.block.flow.controller; | ||
|
||
import com.alibaba.csp.sentinel.slots.block.RuleConstant; | ||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; | ||
import com.alibaba.csp.sentinel.slots.block.flow.TrafficShapingController; | ||
import com.alibaba.csp.sentinel.slots.block.flow.TrafficShapingControllerFactory; | ||
|
||
import static com.alibaba.csp.sentinel.slots.block.flow.controller.WarmUpControllerFactory.coldFactor; | ||
|
||
public class WarmUpRateLimiterControllerFactory implements TrafficShapingControllerFactory { | ||
|
||
@Override | ||
public TrafficShapingController create(FlowRule rule) { | ||
return new WarmUpRateLimiterController(rule.getCount(), rule.getWarmUpPeriodSec(), rule.getMaxQueueingTimeMs(), coldFactor); | ||
} | ||
|
||
@Override | ||
public int getControlBehavior() { | ||
return RuleConstant.CONTROL_BEHAVIOR_WARM_UP_RATE_LIMITER; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...TA-INF/services/com.alibaba.csp.sentinel.slots.block.flow.TrafficShapingControllerFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
com.alibaba.csp.sentinel.slots.block.flow.controller.DefaultControllerFactory | ||
com.alibaba.csp.sentinel.slots.block.flow.controller.WarmUpControllerFactory | ||
com.alibaba.csp.sentinel.slots.block.flow.controller.ThrottlingControllerFactory | ||
com.alibaba.csp.sentinel.slots.block.flow.controller.WarmUpRateLimiterControllerFactory |