Skip to content

Commit

Permalink
Guarantee that RequestContext is accessible during converting a respo…
Browse files Browse the repository at this point in the history
…nse by ResponseConverter. (line#698)

Motivation:

An annotated method may return a future instance which runs over common ForkJoinPool, so we may get an error like ‘java.lang.IllegalStateException: RequestContext unavailable’  when accessing the context of a request in a RequestConverter. So we should guarantee that RequestContext is accessible during converting a response by ResponseConverter.

Modifications:

Call ‘RequestContext.push’ before converting a response by a ResponseConverter.

Result:

We are able to guarantee that RequestContext is accessible during converting a response by ResponseConverter.
  • Loading branch information
hyangtack authored and trustin committed Jul 25, 2017
1 parent 8eefeca commit 553f6d2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,22 @@ public Object apply(ServiceRequestContext ctx, HttpRequest req) {

BiFunction<ServiceRequestContext, HttpRequest, Object> withConverter(ResponseConverter converter) {
return (ctx, req) ->
executeSyncOrAsync(ctx, req).thenApply(obj -> convertResponse(obj, converter));
executeSyncOrAsync(ctx, req).thenApply(obj -> {
try (SafeCloseable ignored = RequestContext.push(ctx, false)) {
return convertResponse(obj, converter);
}
});
}

BiFunction<ServiceRequestContext, HttpRequest, Object> withConverters(
Map<Class<?>, ResponseConverter> converters) {

return (ctx, req) ->
executeSyncOrAsync(ctx, req).thenApply(obj -> convertResponse(obj, converters));
executeSyncOrAsync(ctx, req).thenApply(obj -> {
try (SafeCloseable ignored = RequestContext.push(ctx, false)) {
return convertResponse(obj, converters);
}
});
}

private CompletionStage<?> executeSyncOrAsync(ServiceRequestContext ctx, HttpRequest req) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
import com.linecorp.armeria.common.HttpHeaders;
import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.common.HttpStatus;
import com.linecorp.armeria.common.RequestContext;
import com.linecorp.armeria.server.annotation.ResponseConverter;

final class TestConverters {

public static class NaiveIntConverter implements ResponseConverter {
@Override
public HttpResponse convert(Object resObj) throws Exception {
assert RequestContext.current() != null;
final DefaultHttpResponse res = new DefaultHttpResponse();
final HttpData data = HttpData.ofUtf8(String.format("Integer: %d", resObj));
final long current = System.currentTimeMillis();
Expand All @@ -46,6 +48,7 @@ public HttpResponse convert(Object resObj) throws Exception {
public static class NaiveNumberConverter implements ResponseConverter {
@Override
public HttpResponse convert(Object resObj) throws Exception {
assert RequestContext.current() != null;
final DefaultHttpResponse res = new DefaultHttpResponse();
final HttpData data = HttpData.ofUtf8(String.format("Number: %d", resObj));
final long current = System.currentTimeMillis();
Expand All @@ -63,6 +66,7 @@ public HttpResponse convert(Object resObj) throws Exception {
public static class NaiveStringConverter implements ResponseConverter {
@Override
public HttpResponse convert(Object resObj) throws Exception {
assert RequestContext.current() != null;
final DefaultHttpResponse res = new DefaultHttpResponse();
final HttpData data = HttpData.ofUtf8(String.format("String: %s", resObj));
final long current = System.currentTimeMillis();
Expand All @@ -80,6 +84,7 @@ public HttpResponse convert(Object resObj) throws Exception {
public static class TypedNumberConverter implements ResponseConverter {
@Override
public HttpResponse convert(Object resObj) throws Exception {
assert RequestContext.current() != null;
final DefaultHttpResponse res = new DefaultHttpResponse();
final HttpData data = HttpData.ofUtf8(String.format("Number[%d]", resObj));
final long current = System.currentTimeMillis();
Expand All @@ -97,6 +102,7 @@ public HttpResponse convert(Object resObj) throws Exception {
public static class TypedStringConverter implements ResponseConverter {
@Override
public HttpResponse convert(Object resObj) throws Exception {
assert RequestContext.current() != null;
final DefaultHttpResponse res = new DefaultHttpResponse();
final HttpData data = HttpData.ofUtf8(String.format("String[%s]", resObj));
final long current = System.currentTimeMillis();
Expand All @@ -114,6 +120,7 @@ public HttpResponse convert(Object resObj) throws Exception {
public static class UnformattedStringConverter implements ResponseConverter {
@Override
public HttpResponse convert(Object resObj) throws Exception {
assert RequestContext.current() != null;
final DefaultHttpResponse res = new DefaultHttpResponse();
final HttpData data = HttpData.ofUtf8(resObj.toString());
final long current = System.currentTimeMillis();
Expand Down

0 comments on commit 553f6d2

Please sign in to comment.