Skip to content

Add Request Timeout Annotation #168

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

Merged
merged 2 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions http-api/src/main/java/io/avaje/http/api/RequestTimeout.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.avaje.http.api;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.time.temporal.ChronoUnit;

/**
* Overrides global request timeout for this endpoint.
*
* <pre>{@code
* @Client
* interface CustomerApi {
* @Get("/{id}")
* @RequestTimeout(value = 1, ChronoUnit.SECONDS)
* Customer getById(long id);
* }
*
* }</pre>
*/
@Target(METHOD)
@Retention(RUNTIME)
public @interface RequestTimeout {
long value();

ChronoUnit chronoUnit() default ChronoUnit.MILLIS;
}
2 changes: 1 addition & 1 deletion http-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-http-api</artifactId>
<version>1.20</version>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.avaje.http.generator.core.*;

import javax.lang.model.element.TypeElement;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -24,6 +25,7 @@ class ClientMethodWriter {
private MethodParam bodyHandlerParam;
private String methodGenericParams = "";
private final boolean useJsonb;
private final Optional<RequestTimeoutPrism> timeout;

ClientMethodWriter(MethodReader method, Append writer, ProcessingContext ctx, boolean useJsonb) {
this.method = method;
Expand All @@ -32,6 +34,7 @@ class ClientMethodWriter {
this.ctx = ctx;
this.returnType = Util.parseType(method.returnType());
this.useJsonb = useJsonb;
this.timeout = method.timeout();
}

void addImportTypes(ControllerReader reader) {
Expand Down Expand Up @@ -86,12 +89,18 @@ void write() {
writeQueryParams(pathSegments);
writeBeanParams(pathSegments);
writeFormParams(pathSegments);
timeout.ifPresent(this::writeTimeout);
writeBody();
writeEnd();
}

private void writeEnd() {
WebMethod webMethod = method.webMethod();
private void writeTimeout(RequestTimeoutPrism p) {

writer.append(" .requestTimeout(of(%s, %s))", p.value(), p.chronoUnit()).eol();
}

private void writeEnd() {
final var webMethod = method.webMethod();
writer.append(" .%s()", webMethod.name()).eol();
if (returnType == UType.VOID) {
writer.append(" .asVoid();").eol();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
import java.io.IOException;
import java.io.Writer;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.FileObject;
Expand Down Expand Up @@ -84,22 +82,11 @@ private void writeServicesFile() {
}

private void writeForImported(Element importedElement) {
for (AnnotationMirror annotationMirror : importedElement.getAnnotationMirrors()) {
for (AnnotationValue value : annotationMirror.getElementValues().values()) {
for (Object apiClassDef : (List<?>) value.getValue()) {
writeImported(apiClassDef.toString());
}
}
}
}

private void writeImported(String fullName) {
// trim .class suffix
String apiClassName = fullName.substring(0, fullName.length() - 6);
TypeElement typeElement = ctx.typeElement(apiClassName);
if (typeElement != null) {
writeClient(typeElement);
}
ImportPrism.getInstanceOn(importedElement).types().stream()
.map(ctx::asElement)
.filter(Objects::nonNull)
.forEach(this::writeClient);
}

private void writeClient(Element controller) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class MethodReader {
private final PathSegments pathSegments;
private final boolean hasValid;
private final List<ExecutableElement> superMethods;
private final Optional<RequestTimeoutPrism> timeout;

private WebMethod webMethod;
private String webMethodPath;
Expand All @@ -59,7 +60,12 @@ public class MethodReader {

this.apiResponses = buildApiResponses();
this.javadoc = buildJavadoc(element, ctx);

this.timeout = RequestTimeoutPrism.getOptionalOn(element);
timeout.ifPresent(
p -> {
bean.addStaticImportType("java.time.temporal.ChronoUnit." + p.chronoUnit());
bean.addStaticImportType("java.time.Duration.of");
});
if (isWebMethod()) {
this.hasValid = initValid();
this.pathSegments = PathSegments.parse(Util.combinePath(bean.path(), webMethodPath));
Expand Down Expand Up @@ -325,4 +331,8 @@ public String bodyName() {
}
return "body";
}

public Optional<RequestTimeoutPrism> timeout() {
return timeout;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
@GeneratePrism(value = org.jetbrains.annotations.NotNull.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.Client.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.Client.Import.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.RequestTimeout.class, publicAccess = true)
package io.avaje.http.generator.core;

import io.avaje.prism.GeneratePrism;