Skip to content

Commit 062c18a

Browse files
committed
Add DataBuffer management in fragment rendering
See gh-33194
1 parent 8e2b27e commit 062c18a

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandler.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,9 @@ else if (FragmentsRendering.class.isAssignableFrom(clazz)) {
282282
streamHandler.updateResponse(exchange);
283283
}
284284

285-
Flux<Flux<DataBuffer>> renderFlux = render.fragments().concatMap(fragment ->
286-
renderFragment(fragment, streamHandler, locale, bindingContext, exchange));
285+
Flux<Flux<DataBuffer>> renderFlux = render.fragments()
286+
.concatMap(fragment -> renderFragment(fragment, streamHandler, locale, bindingContext, exchange))
287+
.doOnDiscard(DataBuffer.class, DataBufferUtils::release);
287288

288289
return response.writeAndFlushWith(renderFlux);
289290
}
@@ -552,9 +553,16 @@ public Flux<DataBuffer> format(
552553
DataBuffer suffix = encodeText("\n\n", charset, bufferFactory);
553554

554555
Mono<DataBuffer> content = DataBufferUtils.join(fragmentFlux)
555-
.map(dataBuffer -> {
556-
String s = dataBuffer.toString(charset).replace("\n", "\ndata:");
557-
return bufferFactory.wrap(s.getBytes(charset));
556+
.map(buffer -> {
557+
String text;
558+
try {
559+
text = buffer.toString(charset);
560+
}
561+
finally {
562+
DataBufferUtils.release(buffer);
563+
}
564+
text = text.replace("\n", "\ndata:");
565+
return bufferFactory.wrap(text.getBytes(charset));
558566
});
559567

560568
return Flux.concat(Flux.just(prefix), content, Flux.just(suffix));

0 commit comments

Comments
 (0)