|
| 1 | +/* |
| 2 | + * Copyright 1999-2018 Alibaba Group Holding Ltd. |
| 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 | + * http://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 com.alibaba.csp.sentinel.slotchain; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | +import java.util.ServiceLoader; |
| 21 | + |
| 22 | +import com.alibaba.csp.sentinel.log.RecordLog; |
| 23 | +import com.alibaba.csp.sentinel.slots.DefaultSlotChainBuilder; |
| 24 | + |
| 25 | +/** |
| 26 | + * A provider for creating slot chains via resolved slot chain builder SPI. |
| 27 | + * |
| 28 | + * @author Eric Zhao |
| 29 | + * @since 0.2.0 |
| 30 | + */ |
| 31 | +public final class SlotChainProvider { |
| 32 | + |
| 33 | + private static volatile SlotChainBuilder builder = null; |
| 34 | + |
| 35 | + private static final ServiceLoader<SlotChainBuilder> LOADER = ServiceLoader.load(SlotChainBuilder.class); |
| 36 | + |
| 37 | + /** |
| 38 | + * The load and pick process is not thread-safe, but it's okay since the method should be only invoked |
| 39 | + * via {@code lookProcessChain} in {@link com.alibaba.csp.sentinel.CtSph} under lock. |
| 40 | + * |
| 41 | + * @return new created slot chain |
| 42 | + */ |
| 43 | + public static ProcessorSlotChain newSlotChain() { |
| 44 | + if (builder != null) { |
| 45 | + return builder.build(); |
| 46 | + } |
| 47 | + |
| 48 | + resolveSlotChainBuilder(); |
| 49 | + |
| 50 | + if (builder == null) { |
| 51 | + RecordLog.warn("[SlotChainProvider] Wrong state when resolving slot chain builder, using default"); |
| 52 | + builder = new DefaultSlotChainBuilder(); |
| 53 | + } |
| 54 | + return builder.build(); |
| 55 | + } |
| 56 | + |
| 57 | + private static void resolveSlotChainBuilder() { |
| 58 | + List<SlotChainBuilder> list = new ArrayList<SlotChainBuilder>(); |
| 59 | + boolean hasOther = false; |
| 60 | + for (SlotChainBuilder builder : LOADER) { |
| 61 | + if (builder.getClass() != DefaultSlotChainBuilder.class) { |
| 62 | + hasOther = true; |
| 63 | + list.add(builder); |
| 64 | + } |
| 65 | + } |
| 66 | + if (hasOther) { |
| 67 | + builder = list.get(0); |
| 68 | + } else { |
| 69 | + // No custom builder, using default. |
| 70 | + builder = new DefaultSlotChainBuilder(); |
| 71 | + } |
| 72 | + |
| 73 | + RecordLog.info("[SlotChainProvider] Global slot chain builder resolved: " |
| 74 | + + builder.getClass().getCanonicalName()); |
| 75 | + } |
| 76 | + |
| 77 | + private SlotChainProvider() {} |
| 78 | +} |
0 commit comments