Add disposable key clean logic

pull/430/head
misselvexu 3 years ago
parent 6d595e4375
commit cb7dbffc5b

@ -63,15 +63,10 @@ public class DecodeTransferMetadataServletFilter extends OncePerRequestFilter {
mergedTransitiveMetadata.putAll(internalTransitiveMetadata); mergedTransitiveMetadata.putAll(internalTransitiveMetadata);
mergedTransitiveMetadata.putAll(customTransitiveMetadata); mergedTransitiveMetadata.putAll(customTransitiveMetadata);
try {
MetadataContextHolder.init(mergedTransitiveMetadata); MetadataContextHolder.init(mergedTransitiveMetadata);
filterChain.doFilter(httpServletRequest, httpServletResponse); filterChain.doFilter(httpServletRequest, httpServletResponse);
} }
catch (IOException | ServletException | RuntimeException e) {
throw e;
}
}
private Map<String, String> getInternalTransitiveMetadata(HttpServletRequest httpServletRequest) { private Map<String, String> getInternalTransitiveMetadata(HttpServletRequest httpServletRequest) {
// Get custom metadata string from http header. // Get custom metadata string from http header.

@ -20,12 +20,21 @@ package com.tencent.cloud.common.metadata;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils; import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import static com.tencent.cloud.common.constant.ContextConstant.UTF_8;
import static com.tencent.cloud.common.constant.MetadataConstant.INTERNAL_METADATA_DISPOSABLE;
import static com.tencent.cloud.common.util.JacksonUtils.deserialize2Map;
import static java.net.URLDecoder.decode;
/** /**
* Metadata Context Holder. * Metadata Context Holder.
@ -34,6 +43,8 @@ import org.springframework.util.CollectionUtils;
*/ */
public final class MetadataContextHolder { public final class MetadataContextHolder {
private static final Logger LOGGER = LoggerFactory.getLogger(MetadataContextHolder.class);
private static final ThreadLocal<MetadataContext> METADATA_CONTEXT = new InheritableThreadLocal<>(); private static final ThreadLocal<MetadataContext> METADATA_CONTEXT = new InheritableThreadLocal<>();
private static MetadataLocalProperties metadataLocalProperties; private static MetadataLocalProperties metadataLocalProperties;
@ -89,6 +100,34 @@ public final class MetadataContextHolder {
// Save transitive metadata to ThreadLocal. // Save transitive metadata to ThreadLocal.
if (!CollectionUtils.isEmpty(dynamicTransitiveMetadata)) { if (!CollectionUtils.isEmpty(dynamicTransitiveMetadata)) {
// processing disposable keys
if (dynamicTransitiveMetadata.containsKey(INTERNAL_METADATA_DISPOSABLE)) {
String disposableKeyStatus = dynamicTransitiveMetadata.get(INTERNAL_METADATA_DISPOSABLE);
try {
if (StringUtils.hasText(disposableKeyStatus)) {
Map<String, String> keyStatus = deserialize2Map(decode(disposableKeyStatus, UTF_8));
Iterator<Map.Entry<String, String>> it = keyStatus.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
String key = entry.getKey();
boolean status = Boolean.parseBoolean(entry.getValue());
if (!status) {
keyStatus.put(key, "true");
}
else {
// removed disposable key
dynamicTransitiveMetadata.remove(key);
it.remove();
}
}
}
}
catch (Exception e) {
LOGGER.error("Runtime system does not support utf-8 coding.", e);
}
}
Map<String, String> staticTransitiveMetadata = Map<String, String> staticTransitiveMetadata =
metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
Map<String, String> mergedTransitiveMetadata = new HashMap<>(); Map<String, String> mergedTransitiveMetadata = new HashMap<>();

Loading…
Cancel
Save