Skip to content
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

[SPR-13223] ResponseBodyEmitter skips same messages during initialization #837

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -17,8 +17,8 @@
package org.springframework.web.servlet.mvc.method.annotation;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.ArrayDeque;
import java.util.Queue;

import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpResponse;
Expand Down Expand Up @@ -63,7 +63,7 @@ public class ResponseBodyEmitter {
private volatile Handler handler;

/* Cache for objects sent before handler is set. */
private final Map<Object, MediaType> initHandlerCache = new LinkedHashMap<Object, MediaType>(10);
private final Queue<PendingEvent> initHandlerCache = new ArrayDeque<>(10);

private volatile boolean complete;

Expand Down Expand Up @@ -113,11 +113,11 @@ protected void extendResponse(ServerHttpResponse outputMessage) {
void initialize(Handler handler) throws IOException {
synchronized (this) {
this.handler = handler;
for (Map.Entry<Object, MediaType> entry : this.initHandlerCache.entrySet()) {
while (!initHandlerCache.isEmpty()) {
final PendingEvent event = initHandlerCache.remove();
try {
sendInternal(entry.getKey(), entry.getValue());
}
catch (Throwable ex) {
sendInternal(event.getObject(), event.getType());
} catch (Throwable ex) {
return;
}
}
Expand Down Expand Up @@ -171,7 +171,7 @@ private void sendInternal(Object object, MediaType mediaType) throws IOException
if (this.handler == null) {
synchronized (this) {
if (this.handler == null) {
this.initHandlerCache.put(object, mediaType);
this.initHandlerCache.add(new PendingEvent(object, mediaType));
return;
}
}
Expand Down Expand Up @@ -263,4 +263,23 @@ interface Handler {
void onCompletion(Runnable callback);
}

}
private static class PendingEvent {

private final Object object;
private final MediaType type;

public PendingEvent(Object object, MediaType type) {
this.object = object;
this.type = type;
}

public Object getObject() {
return object;
}

public MediaType getType() {
return type;
}
}

}
Expand Up @@ -62,6 +62,19 @@ public void sendBeforeHandlerInitialized() throws Exception {
verifyNoMoreInteractions(this.handler);
}

@Test
public void sendDuplicateBeforeHandlerInitialized() throws Exception {
this.emitter.send("foo", MediaType.TEXT_PLAIN);
this.emitter.send("foo", MediaType.TEXT_PLAIN);
this.emitter.complete();
verifyNoMoreInteractions(this.handler);

this.emitter.initialize(this.handler);
verify(this.handler, times(2)).send("foo", MediaType.TEXT_PLAIN);
verify(this.handler).complete();
verifyNoMoreInteractions(this.handler);
}

@Test
public void sendBeforeHandlerInitializedWithError() throws Exception {
IllegalStateException ex = new IllegalStateException();
Expand Down