Skip to content

Commit

Permalink
Polish.
Browse files Browse the repository at this point in the history
  • Loading branch information
nstdio committed Mar 18, 2022
1 parent 3835742 commit 1afe6a6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.github.nstdio.http.ext;

import io.github.nstdio.http.ext.Cache.CacheEntry;

import javax.net.ssl.SSLSession;
import java.net.URI;
import java.net.http.HttpClient;
Expand All @@ -27,11 +29,11 @@
final class CachedHttpResponse<T> implements HttpResponse<T> {
private final BodyHandler<T> bodyHandler;
private final HttpRequest request;
private final Cache.CacheEntry entry;
private final CacheEntry entry;

private T body;

CachedHttpResponse(BodyHandler<T> bodyHandler, HttpRequest request, Cache.CacheEntry entry) {
CachedHttpResponse(BodyHandler<T> bodyHandler, HttpRequest request, CacheEntry entry) {
this.bodyHandler = bodyHandler;
this.request = request;
this.entry = entry;
Expand Down
31 changes: 17 additions & 14 deletions src/main/java/io/github/nstdio/http/ext/CachingInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
import static java.util.stream.Collectors.toList;
import static lombok.Lombok.sneakyThrow;

import io.github.nstdio.http.ext.Cache.CacheEntry;
import lombok.RequiredArgsConstructor;

import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandler;
import java.net.http.HttpResponse.BodySubscriber;
import java.time.Clock;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -79,7 +82,7 @@ public <T> Chain<T> intercept(Chain<T> in) {
RequestContext ctx = in.ctx();

if (ctx.isCacheable()) {
final Cache.CacheEntry entry = cacheEntry(ctx);
final CacheEntry entry = cacheEntry(ctx);

if (ctx.cacheControl().onlyIfCached()) {
return in.withResponse(forcedCacheResponse(ctx, entry));
Expand All @@ -103,9 +106,9 @@ public <T> Chain<T> intercept(Chain<T> in) {
}
}

private <T> HttpResponse<T> forcedCacheResponse(RequestContext ctx, Cache.CacheEntry entry) {
private <T> HttpResponse<T> forcedCacheResponse(RequestContext ctx, CacheEntry entry) {
final var request = ctx.request();
Cache.CacheEntry forcedEntry = entry == null ? getCacheEntry(request) : entry;
CacheEntry forcedEntry = entry == null ? getCacheEntry(request) : entry;
final HttpResponse<T> response;
if (forcedEntry == null || !forcedEntry.metadata().isFresh(ctx.cacheControl())) {
response = gatewayTimeoutResponse(request);
Expand All @@ -117,8 +120,8 @@ private <T> HttpResponse<T> forcedCacheResponse(RequestContext ctx, Cache.CacheE

}

private <T> Chain<T> sendAndCache(Chain<T> in, Cache.CacheEntry entry) {
var metadata = Optional.ofNullable(entry).map(Cache.CacheEntry::metadata);
private <T> Chain<T> sendAndCache(Chain<T> in, CacheEntry entry) {
var metadata = Optional.ofNullable(entry).map(CacheEntry::metadata);
var newCtx = metadata
.map(m -> applyConditions(in.ctx().request(), m))
.map(in.ctx()::withRequest)
Expand All @@ -143,10 +146,10 @@ && hasConditions(newCtx.request())) {
return Chain.of(newCtx.withBodyHandler(bodyHandler), in.futureHandler().andThen(handler));
}

private <T> HttpResponse.BodyHandler<T> cacheAware(RequestContext ctx) {
private <T> BodyHandler<T> cacheAware(RequestContext ctx) {
return info -> {
ctx.responseTime().set(clock.millis());
HttpResponse.BodySubscriber<T> sub = ctx.<T>bodyHandler().apply(info);
ctx.responseTime().compareAndSet(0, clock.millis());
BodySubscriber<T> sub = ctx.<T>bodyHandler().apply(info);

if (isCacheable(info)) {
var metadata = CacheEntryMetadata.of(ctx.requestTime().get(), ctx.responseTime().get(),
Expand Down Expand Up @@ -189,7 +192,7 @@ private boolean isUnsafe(HttpRequest request) {
|| "DELETE".equalsIgnoreCase(method);
}

private Cache.CacheEntry cacheEntry(RequestContext ctx) {
private CacheEntry cacheEntry(RequestContext ctx) {
if (ctx.cacheControl().noCache()) {
// TODO: check if entry is stale and remove it
return null;
Expand All @@ -198,18 +201,18 @@ private Cache.CacheEntry cacheEntry(RequestContext ctx) {
return getCacheEntry(ctx.request());
}

private Cache.CacheEntry getCacheEntry(HttpRequest request) {
private CacheEntry getCacheEntry(HttpRequest request) {
return cache.get(request);
}

private boolean isFresh(RequestContext ctx, Cache.CacheEntry entry) {
private boolean isFresh(RequestContext ctx, CacheEntry entry) {
return Optional.ofNullable(entry)
.map(Cache.CacheEntry::metadata)
.map(CacheEntry::metadata)
.filter(m -> m.isFresh(ctx.cacheControl()))
.isPresent();
}

private <T> HttpResponse<T> possiblyCached(RequestContext ctx, Cache.CacheEntry entry, HttpResponse<T> r) {
private <T> HttpResponse<T> possiblyCached(RequestContext ctx, CacheEntry entry, HttpResponse<T> r) {
CacheEntryMetadata metadata = entry != null ? entry.metadata() : null;
if (metadata != null) {
switch (r.statusCode()) {
Expand Down Expand Up @@ -238,7 +241,7 @@ private <T> HttpResponse<T> possiblyCached(RequestContext ctx, Cache.CacheEntry
return r;
}

private <T> HttpResponse<T> createCachedResponse(RequestContext ctx, Cache.CacheEntry entry) {
private <T> HttpResponse<T> createCachedResponse(RequestContext ctx, CacheEntry entry) {
return new CachedHttpResponse<>(ctx.bodyHandler(), ctx.request(), entry);
}
}
5 changes: 3 additions & 2 deletions src/main/java/io/github/nstdio/http/ext/NullCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
class NullCache implements Cache {
static final NullCache INSTANCE = new NullCache();

private static final Consumer<Object> NOOP_CONSUMER = t -> {
};
private static final Writer<Object> WRITER = new Writer<>() {
@Override
public HttpResponse.BodySubscriber<Object> subscriber() {
Expand All @@ -32,8 +34,7 @@ public HttpResponse.BodySubscriber<Object> subscriber() {

@Override
public Consumer<Object> finisher() {
return t -> {
};
return NOOP_CONSUMER;
}
};

Expand Down

0 comments on commit 1afe6a6

Please sign in to comment.