Skip to content

GatewayApiMatcherManager BUG #2436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 29, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@
*/
package com.alibaba.csp.sentinel.adapter.gateway.sc.api;

import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
import com.alibaba.csp.sentinel.adapter.gateway.sc.api.matcher.WebExchangeApiMatcher;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
import com.alibaba.csp.sentinel.adapter.gateway.sc.api.matcher.WebExchangeApiMatcher;

/**
* @author Eric Zhao
* @since 1.6.0
*/
public final class GatewayApiMatcherManager {

private static final Map<String, WebExchangeApiMatcher> API_MATCHER_MAP = new ConcurrentHashMap<>();
private static Map<String, WebExchangeApiMatcher> API_MATCHER_MAP = new ConcurrentHashMap<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just volatile HashMap is enough?

Copy link
Contributor Author

@su-yh su-yh Nov 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用volatile 会更好吗?
你这是出于什么考虑,而建议使用它呢?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConcurrentHashMap -> volatile HashMap


public static Map<String, WebExchangeApiMatcher> getApiMatcherMap() {
return Collections.unmodifiableMap(API_MATCHER_MAP);
Expand All @@ -50,11 +50,11 @@ public static Set<ApiDefinition> getApiDefinitionSet() {
}

static synchronized void loadApiDefinitions(/*@Valid*/ Set<ApiDefinition> definitions) {
if (definitions == null || definitions.isEmpty()) {
API_MATCHER_MAP.clear();
return;
Map<String, WebExchangeApiMatcher> apiMatcherMap = new ConcurrentHashMap<>();
for (ApiDefinition definition : definitions) {
apiMatcherMap.put(definition.getApiName(), new WebExchangeApiMatcher(definition));
}
definitions.forEach(GatewayApiMatcherManager::addApiDefinition);
API_MATCHER_MAP = apiMatcherMap;
}

static void addApiDefinition(ApiDefinition definition) {
Expand Down