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

Improving SSE Performance #5832

Open
wants to merge 3 commits into
base: 2.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2025 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,6 +16,8 @@

package org.glassfish.jersey.media.sse;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -43,14 +45,12 @@
*/
class OutboundEventWriter implements MessageBodyWriter<OutboundSseEvent> {

private static final Charset UTF8 = Charset.forName("UTF-8");

// encoding does not matter (lower ASCII characters)
private static final byte[] COMMENT_LEAD = ": ".getBytes(UTF8);
private static final byte[] NAME_LEAD = "event: ".getBytes(UTF8);
private static final byte[] ID_LEAD = "id: ".getBytes(UTF8);
private static final byte[] RETRY_LEAD = "retry: ".getBytes(UTF8);
private static final byte[] DATA_LEAD = "data: ".getBytes(UTF8);
private static final byte[] COMMENT_LEAD = ": ".getBytes(UTF_8);
private static final byte[] NAME_LEAD = "event: ".getBytes(UTF_8);
private static final byte[] ID_LEAD = "id: ".getBytes(UTF_8);
private static final byte[] RETRY_LEAD = "retry: ".getBytes(UTF_8);
private static final byte[] DATA_LEAD = "data: ".getBytes(UTF_8);
private static final byte[] EOL = {'\n'};

private final Provider<MessageBodyWorkers> workersProvider;
Expand Down Expand Up @@ -122,23 +122,26 @@ public void writeTo(final OutboundSseEvent outboundEvent,
annotations,
eventMediaType,
httpHeaders,
new OutputStream() {

private boolean start = true;

@Override
public void write(final int i) throws IOException {
if (start) {
entityStream.write(DATA_LEAD);
start = false;
}
entityStream.write(i);
if (i == '\n') {
entityStream.write(DATA_LEAD);
}
}
});
new DataLeadStream(entityStream));
entityStream.write(EOL);
}
}

private static final class DataLeadStream extends OutputStream {
private final OutputStream entityStream;
private int lastChar = '\n';

DataLeadStream(final OutputStream entityStream) {
this.entityStream = entityStream;
}

@Override
public final void write(final int i) throws IOException {
if (lastChar == '\n') {
entityStream.write(DATA_LEAD);
}
entityStream.write(i);
lastChar = i;
}
}
}
Loading