Skip to content

Commit

Permalink
Optimize the use of buffers in health reporting
Browse files Browse the repository at this point in the history
This is done by leveraging Vert.x's Buffer directly instead
of relying on a ByteArrayOutputStream which results in a lot
of unnecessary copying of data
  • Loading branch information
geoand committed Aug 1, 2022
1 parent 20280ff commit 5f67ced
Showing 1 changed file with 23 additions and 3 deletions.
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);
}
}
}

0 comments on commit 5f67ced

Please sign in to comment.