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

Optimize the use of buffers in health reporting #27056

Merged
merged 1 commit into from
Aug 1, 2022
Merged
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,7 +1,7 @@
package io.quarkus.smallrye.health.runtime;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;

import io.quarkus.arc.Arc;
Expand Down Expand Up @@ -47,11 +47,31 @@ private void doHandle(RoutingContext ctx) {
resp.setStatusCode(503);
}
resp.headers().set(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8");
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
Buffer buffer = Buffer.buffer(256); // this size seems to cover the basic health checks
try (BufferOutputStream outputStream = new BufferOutputStream(buffer);) {
reporter.reportHealth(outputStream, health);
resp.end(Buffer.buffer(outputStream.toByteArray()));
resp.end(buffer);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private static class BufferOutputStream extends OutputStream {

private final Buffer buffer;

private BufferOutputStream(Buffer buffer) {
this.buffer = buffer;
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
buffer.appendBytes(b, off, len);
}

@Override
public void write(int b) throws IOException {
buffer.appendInt(b);
}
}
}