Skip to content

Commit 327cc35

Browse files
committed
refactor: Name the response threshold for what it measures
minimumGzipResponseBytes becomes minCompressibleResponseBytes, and ResponseRenderer.DEFAULT_MINIMUM_GZIP_BYTES becomes DEFAULT_MIN_COMPRESSIBLE_BYTES. The threshold is compared against the uncompressed body before any coding is chosen, so it neither depends on gzip nor measures a compressed size. "Compressible" says what it does: a body smaller than this is not worth coding. It now mirrors maxDecompressedRequestBytes - min and max, and both measure plain bytes. Neither name has been released, and every push to master publishes to Maven Central, so this is the last point at which the rename is free rather than a breaking change to a public builder method.
1 parent 15d4953 commit 327cc35

7 files changed

Lines changed: 28 additions & 24 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ coded, and neither is `text/event-stream`, which has to stay unbuffered.
497497
OpenApiServer.builder()
498498
.spec(spec)
499499
.handlers(handlers)
500-
.minimumGzipResponseBytes(4096) // raises the 1 KiB default; 0 compresses every eligible body
500+
.minCompressibleResponseBytes(4096) // raises the 1 KiB default; 0 compresses every eligible body
501501
.build();
502502
```
503503

src/main/java/com/retailsvc/http/OpenApiServer.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public static final class Builder {
246246
private final Map<String, SchemeValidator> securityValidators = new LinkedHashMap<>();
247247
private boolean externalAuth = false;
248248
private long maxDecompressedRequestBytes = RequestBodyReader.DEFAULT_MAX_DECOMPRESSED_BYTES;
249-
private long minimumGzipResponseBytes = ResponseRenderer.DEFAULT_MINIMUM_GZIP_BYTES;
249+
private long minCompressibleResponseBytes = ResponseRenderer.DEFAULT_MIN_COMPRESSIBLE_BYTES;
250250
private final List<SpecBinding> bindings = new ArrayList<>();
251251

252252
private Builder() {}
@@ -409,17 +409,18 @@ public Builder maxDecompressedRequestBytes(long maxDecompressedRequestBytes) {
409409
}
410410

411411
/**
412-
* Smallest response body worth gzipping, 1 KiB by default. Below this, the coding costs more
412+
* Smallest response body worth compressing, 1 KiB by default. Below this, the coding costs more
413413
* than it saves. Set it to 0 to compress every compressible body, or high enough to exceed any
414414
* response this server produces to stop compressing altogether — useful when a proxy in front
415415
* already terminates compression.
416416
*/
417-
public Builder minimumGzipResponseBytes(long minimumGzipResponseBytes) {
418-
if (minimumGzipResponseBytes < 0) {
417+
public Builder minCompressibleResponseBytes(long minCompressibleResponseBytes) {
418+
if (minCompressibleResponseBytes < 0) {
419419
throw new IllegalArgumentException(
420-
"minimumGzipResponseBytes must be non-negative, got " + minimumGzipResponseBytes);
420+
"minCompressibleResponseBytes must be non-negative, got "
421+
+ minCompressibleResponseBytes);
421422
}
422-
this.minimumGzipResponseBytes = minimumGzipResponseBytes;
423+
this.minCompressibleResponseBytes = minCompressibleResponseBytes;
423424
return this;
424425
}
425426

@@ -471,7 +472,7 @@ public OpenApiServer build() throws IOException {
471472
externalAuth,
472473
List.copyOf(afterHooks),
473474
new RequestBodyReader(maxDecompressedRequestBytes),
474-
new ResponseRenderer(resolved, minimumGzipResponseBytes));
475+
new ResponseRenderer(resolved, minCompressibleResponseBytes));
475476
int resolvedPort = resolvePort();
476477
SSLContext sslContext =
477478
httpsCertChain != null ? PemSslContext.load(httpsCertChain, httpsPrivateKey) : null;

src/main/java/com/retailsvc/http/internal/ResponseRenderer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
/** Writes a {@link Response} to an {@link HttpExchange}. */
2020
public final class ResponseRenderer {
2121

22-
/** Default smallest body worth gzipping: 1 KiB. */
23-
public static final long DEFAULT_MINIMUM_GZIP_BYTES = 1024;
22+
/** Default smallest body worth compressing: 1 KiB. */
23+
public static final long DEFAULT_MIN_COMPRESSIBLE_BYTES = 1024;
2424

2525
private static final String CONTENT_TYPE = "Content-Type";
2626
private static final String CONTENT_ENCODING = "Content-Encoding";
@@ -34,11 +34,11 @@ public final class ResponseRenderer {
3434
private static final String OCTET_STREAM = "application/octet-stream";
3535

3636
private final Map<String, TypeMapper> mappers;
37-
private final long minimumGzipBytes;
37+
private final long minCompressibleBytes;
3838

39-
public ResponseRenderer(Map<String, TypeMapper> mappers, long minimumGzipBytes) {
39+
public ResponseRenderer(Map<String, TypeMapper> mappers, long minCompressibleBytes) {
4040
this.mappers = Map.copyOf(mappers);
41-
this.minimumGzipBytes = minimumGzipBytes;
41+
this.minCompressibleBytes = minCompressibleBytes;
4242
}
4343

4444
public void render(HttpExchange exchange, Response response) throws IOException {
@@ -112,7 +112,7 @@ private boolean shouldCompress(
112112
return false;
113113
}
114114
addVary(headers);
115-
return (length < 0 || length >= minimumGzipBytes) && acceptsGzip(exchange);
115+
return (length < 0 || length >= minCompressibleBytes) && acceptsGzip(exchange);
116116
}
117117

118118
/** The length a handler declared for a body it did not write, or -1 when absent or unreadable. */

src/test/java/com/retailsvc/http/OpenApiServerBuilderTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ void rejectsOversizedMaxDecompressedRequestBytes() {
7171
}
7272

7373
@Test
74-
void rejectsNegativeMinimumGzipResponseBytes() {
74+
void rejectsNegativeMinCompressibleResponseBytes() {
7575
OpenApiServer.Builder b = OpenApiServer.builder();
7676

77-
assertThatThrownBy(() -> b.minimumGzipResponseBytes(-1))
77+
assertThatThrownBy(() -> b.minCompressibleResponseBytes(-1))
7878
.isInstanceOf(IllegalArgumentException.class)
7979
.hasMessageContaining("-1");
8080
}
@@ -83,7 +83,7 @@ void rejectsNegativeMinimumGzipResponseBytes() {
8383
void acceptsContentCodingLimits() {
8484
OpenApiServer.Builder b = OpenApiServer.builder();
8585

86-
assertThat(b.maxDecompressedRequestBytes(4096).minimumGzipResponseBytes(0)).isSameAs(b);
86+
assertThat(b.maxDecompressedRequestBytes(4096).minCompressibleResponseBytes(0)).isSameAs(b);
8787
}
8888

8989
@Test

src/test/java/com/retailsvc/http/internal/DispatchHandlerTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.retailsvc.http.internal;
22

3-
import static com.retailsvc.http.internal.ResponseRenderer.DEFAULT_MINIMUM_GZIP_BYTES;
3+
import static com.retailsvc.http.internal.ResponseRenderer.DEFAULT_MIN_COMPRESSIBLE_BYTES;
44
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
55
import static java.net.HttpURLConnection.HTTP_OK;
66
import static org.assertj.core.api.Assertions.assertThat;
@@ -46,7 +46,10 @@ private static HttpExchange stubExchange() {
4646

4747
private static DispatchHandler dispatcher(Map<String, RequestHandler> handlers) {
4848
return new DispatchHandler(
49-
handlers, List.of(), List.of(), new ResponseRenderer(Map.of(), DEFAULT_MINIMUM_GZIP_BYTES));
49+
handlers,
50+
List.of(),
51+
List.of(),
52+
new ResponseRenderer(Map.of(), DEFAULT_MIN_COMPRESSIBLE_BYTES));
5053
}
5154

5255
private static DispatchHandler dispatcher(
@@ -57,7 +60,7 @@ private static DispatchHandler dispatcher(
5760
handlers,
5861
interceptors,
5962
decorators,
60-
new ResponseRenderer(Map.of(), DEFAULT_MINIMUM_GZIP_BYTES));
63+
new ResponseRenderer(Map.of(), DEFAULT_MIN_COMPRESSIBLE_BYTES));
6164
}
6265

6366
private static void withRequest(String operationId, ScopedValue.CallableOp<Void, Exception> body)

src/test/java/com/retailsvc/http/internal/ExtrasRouterTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.retailsvc.http.internal;
22

3-
import static com.retailsvc.http.internal.ResponseRenderer.DEFAULT_MINIMUM_GZIP_BYTES;
3+
import static com.retailsvc.http.internal.ResponseRenderer.DEFAULT_MIN_COMPRESSIBLE_BYTES;
44
import static org.assertj.core.api.Assertions.assertThat;
55
import static org.assertj.core.api.Assertions.assertThatThrownBy;
66
import static org.mockito.Mockito.mock;
@@ -146,7 +146,7 @@ private static ExtrasRouter newRouter(Map<String, RequestHandler> extras) {
146146
Map<String, TypeMapper> mappers = Map.of("application/json", new GsonTypeMapper());
147147
return new ExtrasRouter(
148148
extras,
149-
new ResponseRenderer(mappers, DEFAULT_MINIMUM_GZIP_BYTES),
149+
new ResponseRenderer(mappers, DEFAULT_MIN_COMPRESSIBLE_BYTES),
150150
new RequestBodyReader(RequestBodyReader.DEFAULT_MAX_DECOMPRESSED_BYTES));
151151
}
152152

src/test/java/com/retailsvc/http/internal/RequestPreparationFilterTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.retailsvc.http.internal;
22

3-
import static com.retailsvc.http.internal.ResponseRenderer.DEFAULT_MINIMUM_GZIP_BYTES;
3+
import static com.retailsvc.http.internal.ResponseRenderer.DEFAULT_MIN_COMPRESSIBLE_BYTES;
44
import static java.net.HttpURLConnection.HTTP_UNSUPPORTED_TYPE;
55
import static org.assertj.core.api.Assertions.assertThat;
66
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -118,7 +118,7 @@ public byte[] writeTo(Object value) {
118118
new DefaultValidator(spec::resolveSchema),
119119
mappers,
120120
rethrow,
121-
new ResponseRenderer(mappers, DEFAULT_MINIMUM_GZIP_BYTES),
121+
new ResponseRenderer(mappers, DEFAULT_MIN_COMPRESSIBLE_BYTES),
122122
List.of(),
123123
new RequestBodyReader(RequestBodyReader.DEFAULT_MAX_DECOMPRESSED_BYTES));
124124
}

0 commit comments

Comments
 (0)