forked from alibaba/Sentinel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support extensible slot chain builder using SPI mechanism (alibaba#145)
- Support extensible `SlotChainBuilder` using SPI mechanism - Add a `SlotChainProvider` to load slot chain builder and create new slot chains Signed-off-by: Eric Zhao <sczyh16@gmail.com>
- Loading branch information
Showing
6 changed files
with
91 additions
and
11 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
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
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
78 changes: 78 additions & 0 deletions
78
sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/SlotChainProvider.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,78 @@ | ||
/* | ||
* Copyright 1999-2018 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 | ||
* | ||
* http://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.slotchain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.ServiceLoader; | ||
|
||
import com.alibaba.csp.sentinel.log.RecordLog; | ||
import com.alibaba.csp.sentinel.slots.DefaultSlotChainBuilder; | ||
|
||
/** | ||
* A provider for creating slot chains via resolved slot chain builder SPI. | ||
* | ||
* @author Eric Zhao | ||
* @since 0.2.0 | ||
*/ | ||
public final class SlotChainProvider { | ||
|
||
private static volatile SlotChainBuilder builder = null; | ||
|
||
private static final ServiceLoader<SlotChainBuilder> LOADER = ServiceLoader.load(SlotChainBuilder.class); | ||
|
||
/** | ||
* The load and pick process is not thread-safe, but it's okay since the method should be only invoked | ||
* via {@code lookProcessChain} in {@link com.alibaba.csp.sentinel.CtSph} under lock. | ||
* | ||
* @return new created slot chain | ||
*/ | ||
public static ProcessorSlotChain newSlotChain() { | ||
if (builder != null) { | ||
return builder.build(); | ||
} | ||
|
||
resolveSlotChainBuilder(); | ||
|
||
if (builder == null) { | ||
RecordLog.warn("[SlotChainProvider] Wrong state when resolving slot chain builder, using default"); | ||
builder = new DefaultSlotChainBuilder(); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
private static void resolveSlotChainBuilder() { | ||
List<SlotChainBuilder> list = new ArrayList<SlotChainBuilder>(); | ||
boolean hasOther = false; | ||
for (SlotChainBuilder builder : LOADER) { | ||
if (builder.getClass() != DefaultSlotChainBuilder.class) { | ||
hasOther = true; | ||
list.add(builder); | ||
} | ||
} | ||
if (hasOther) { | ||
builder = list.get(0); | ||
} else { | ||
// No custom builder, using default. | ||
builder = new DefaultSlotChainBuilder(); | ||
} | ||
|
||
RecordLog.info("[SlotChainProvider] Global slot chain builder resolved: " | ||
+ builder.getClass().getCanonicalName()); | ||
} | ||
|
||
private SlotChainProvider() {} | ||
} |
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
2 changes: 2 additions & 0 deletions
2
.../src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.SlotChainBuilder
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,2 @@ | ||
# Default slot chain builder | ||
com.alibaba.csp.sentinel.slots.DefaultSlotChainBuilder |