Skip to content

Commit 1ef8cad

Browse files
committed
Make use of Reactor Netty's ChannelOperationsId
Closes gh-26649
1 parent 86902d2 commit 1ef8cad

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

build.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ configure(allprojects) { project ->
2828
imports {
2929
mavenBom "com.fasterxml.jackson:jackson-bom:2.12.2"
3030
mavenBom "io.netty:netty-bom:4.1.59.Final"
31-
mavenBom "io.projectreactor:reactor-bom:2020.0.4"
31+
mavenBom "io.projectreactor:reactor-bom:2020.0.5-SNAPSHOT"
3232
mavenBom "io.r2dbc:r2dbc-bom:Arabba-SR9"
3333
mavenBom "io.rsocket:rsocket-bom:1.1.0"
3434
mavenBom "org.eclipse.jetty:jetty-bom:9.4.38.v20210224"
@@ -292,6 +292,7 @@ configure(allprojects) { project ->
292292
repositories {
293293
mavenCentral()
294294
maven { url "https://repo.spring.io/libs-spring-framework-build" }
295+
maven { url "https://repo.spring.io/snapshot" } // Reactor
295296
}
296297
}
297298
configurations.all {

spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,15 +27,18 @@
2727
import io.netty.handler.codec.http.HttpHeaderNames;
2828
import io.netty.handler.codec.http.cookie.Cookie;
2929
import io.netty.handler.ssl.SslHandler;
30+
import org.apache.commons.logging.Log;
3031
import reactor.core.publisher.Flux;
3132
import reactor.netty.Connection;
3233
import reactor.netty.http.server.HttpServerRequest;
3334

3435
import org.springframework.core.io.buffer.DataBuffer;
3536
import org.springframework.core.io.buffer.NettyDataBufferFactory;
3637
import org.springframework.http.HttpCookie;
38+
import org.springframework.http.HttpLogging;
3739
import org.springframework.lang.Nullable;
3840
import org.springframework.util.Assert;
41+
import org.springframework.util.ClassUtils;
3942
import org.springframework.util.LinkedMultiValueMap;
4043
import org.springframework.util.MultiValueMap;
4144

@@ -48,6 +51,13 @@
4851
*/
4952
class ReactorServerHttpRequest extends AbstractServerHttpRequest {
5053

54+
/** Reactor Netty 1.0.5+. */
55+
static final boolean reactorNettyRequestChannelOperationsIdPresent = ClassUtils.isPresent(
56+
"reactor.netty.ChannelOperationsId", ReactorServerHttpRequest.class.getClassLoader());
57+
58+
private static final Log logger = HttpLogging.forLogName(ReactorServerHttpRequest.class);
59+
60+
5161
private static final AtomicLong logPrefixIndex = new AtomicLong();
5262

5363

@@ -187,11 +197,28 @@ public <T> T getNativeRequest() {
187197
@Override
188198
@Nullable
189199
protected String initId() {
200+
if (reactorNettyRequestChannelOperationsIdPresent) {
201+
return (ChannelOperationsIdHelper.getId(this.request));
202+
}
190203
if (this.request instanceof Connection) {
191204
return ((Connection) this.request).channel().id().asShortText() +
192205
"-" + logPrefixIndex.incrementAndGet();
193206
}
194207
return null;
195208
}
196209

210+
211+
private static class ChannelOperationsIdHelper {
212+
213+
@Nullable
214+
public static String getId(HttpServerRequest request) {
215+
if (request instanceof reactor.netty.ChannelOperationsId) {
216+
return (logger.isDebugEnabled() ?
217+
((reactor.netty.ChannelOperationsId) request).asLongText() :
218+
((reactor.netty.ChannelOperationsId) request).asShortText());
219+
}
220+
return null;
221+
}
222+
}
223+
197224
}

spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpResponse.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@
2626
import org.reactivestreams.Publisher;
2727
import reactor.core.publisher.Flux;
2828
import reactor.core.publisher.Mono;
29+
import reactor.netty.ChannelOperationsId;
2930
import reactor.netty.http.server.HttpServerResponse;
3031

3132
import org.springframework.core.io.buffer.DataBuffer;
@@ -125,11 +126,30 @@ private Publisher<ByteBuf> toByteBufs(Publisher<? extends DataBuffer> dataBuffer
125126
@Override
126127
protected void touchDataBuffer(DataBuffer buffer) {
127128
if (logger.isDebugEnabled()) {
129+
if (ReactorServerHttpRequest.reactorNettyRequestChannelOperationsIdPresent) {
130+
if (ChannelOperationsIdHelper.touch(buffer, this.response)) {
131+
return;
132+
}
133+
}
128134
this.response.withConnection(connection -> {
129135
ChannelId id = connection.channel().id();
130136
DataBufferUtils.touch(buffer, "Channel id: " + id.asShortText());
131137
});
132138
}
133139
}
134140

141+
142+
private static class ChannelOperationsIdHelper {
143+
144+
public static boolean touch(DataBuffer dataBuffer, HttpServerResponse response) {
145+
if (response instanceof reactor.netty.ChannelOperationsId) {
146+
String id = ((ChannelOperationsId) response).asLongText();
147+
DataBufferUtils.touch(dataBuffer, "Channel id: " + id);
148+
return true;
149+
}
150+
return false;
151+
}
152+
}
153+
154+
135155
}

0 commit comments

Comments
 (0)