15
15
*/
16
16
package com .alibaba .csp .sentinel .adapter .gateway .sc .api ;
17
17
18
+ import com .alibaba .csp .sentinel .adapter .gateway .common .api .ApiDefinition ;
19
+ import com .alibaba .csp .sentinel .adapter .gateway .sc .api .matcher .WebExchangeApiMatcher ;
20
+
18
21
import java .util .Collections ;
22
+ import java .util .HashMap ;
19
23
import java .util .Map ;
20
24
import java .util .Optional ;
21
25
import java .util .Set ;
22
- import java .util .concurrent .ConcurrentHashMap ;
23
26
import java .util .stream .Collectors ;
24
27
25
- import com .alibaba .csp .sentinel .adapter .gateway .common .api .ApiDefinition ;
26
- import com .alibaba .csp .sentinel .adapter .gateway .sc .api .matcher .WebExchangeApiMatcher ;
27
-
28
28
/**
29
29
* @author Eric Zhao
30
30
* @since 1.6.0
31
31
*/
32
32
public final class GatewayApiMatcherManager {
33
33
34
- private static final Map <String , WebExchangeApiMatcher > API_MATCHER_MAP = new ConcurrentHashMap <>();
34
+ private static volatile Map <String , WebExchangeApiMatcher > API_MATCHER_MAP = new HashMap <>();
35
35
36
36
public static Map <String , WebExchangeApiMatcher > getApiMatcherMap () {
37
37
return Collections .unmodifiableMap (API_MATCHER_MAP );
@@ -50,15 +50,12 @@ public static Set<ApiDefinition> getApiDefinitionSet() {
50
50
}
51
51
52
52
static synchronized void loadApiDefinitions (/*@Valid*/ Set <ApiDefinition > definitions ) {
53
- if ( definitions == null || definitions . isEmpty ()) {
54
- API_MATCHER_MAP . clear ();
55
- return ;
53
+ Map < String , WebExchangeApiMatcher > apiMatcherMap = new HashMap <>();
54
+ for ( ApiDefinition definition : definitions ) {
55
+ apiMatcherMap . put ( definition . getApiName (), new WebExchangeApiMatcher ( definition )) ;
56
56
}
57
- definitions .forEach (GatewayApiMatcherManager ::addApiDefinition );
58
- }
59
57
60
- static void addApiDefinition (ApiDefinition definition ) {
61
- API_MATCHER_MAP .put (definition .getApiName (), new WebExchangeApiMatcher (definition ));
58
+ API_MATCHER_MAP = apiMatcherMap ;
62
59
}
63
60
64
61
private GatewayApiMatcherManager () {}
0 commit comments