Optimize Class.getDeclaredField in Feign plugin (#7017)

This commit is contained in:
Lu Jiajing 2021-05-26 19:01:16 +08:00 committed by GitHub
parent f6a0efdecd
commit 7ae2b74a64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 12 deletions

View File

@ -32,6 +32,7 @@ Release Notes.
* Introduce method interceptor API v2
* Fix ClassCast issue for RequestHolder/ResponseHolder.
* fixed `jdk-threading-plugin` memory leak.
* Optimize multiple field reflection opeartion in Fiegn plugin.
#### OAP-Backend
* BugFix: filter invalid Envoy access logs whose socket address is empty.

View File

@ -50,6 +50,17 @@ import static feign.Util.valuesOrEmpty;
public class DefaultHttpClientInterceptor implements InstanceMethodsAroundInterceptor {
private static final String CONTENT_TYPE_HEADER = "Content-Type";
private static Field FIELD_HEADERS_OF_REQUEST;
static {
try {
final Field field = Request.class.getDeclaredField("headers");
field.setAccessible(true);
FIELD_HEADERS_OF_REQUEST = field;
} catch (Exception ignore) {
FIELD_HEADERS_OF_REQUEST = null;
}
}
/**
* Get the {@link feign.Request} from {@link EnhancedInstance}, then create {@link AbstractSpan} and set host, port,
@ -101,19 +112,19 @@ public class DefaultHttpClientInterceptor implements InstanceMethodsAroundInterc
}
}
Field headersField = Request.class.getDeclaredField("headers");
headersField.setAccessible(true);
Map<String, Collection<String>> headers = new LinkedHashMap<String, Collection<String>>();
CarrierItem next = contextCarrier.items();
while (next.hasNext()) {
next = next.next();
List<String> contextCollection = new ArrayList<String>(1);
contextCollection.add(next.getHeadValue());
headers.put(next.getHeadKey(), contextCollection);
}
headers.putAll(request.headers());
if (FIELD_HEADERS_OF_REQUEST != null) {
Map<String, Collection<String>> headers = new LinkedHashMap<String, Collection<String>>();
CarrierItem next = contextCarrier.items();
while (next.hasNext()) {
next = next.next();
List<String> contextCollection = new ArrayList<String>(1);
contextCollection.add(next.getHeadValue());
headers.put(next.getHeadKey(), contextCollection);
}
headers.putAll(request.headers());
headersField.set(request, Collections.unmodifiableMap(headers));
FIELD_HEADERS_OF_REQUEST.set(request, Collections.unmodifiableMap(headers));
}
}
private void collectHttpBody(final Request request, final AbstractSpan span) {