Skip to content

Commit 797ec6c

Browse files
authored
Merge pull request #168 from SentryMan/timeout
Add Request Timeout Annotation
2 parents f3bf0bc + df18f10 commit 797ec6c

File tree

6 files changed

+58
-22
lines changed

6 files changed

+58
-22
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.avaje.http.api;
2+
3+
import static java.lang.annotation.ElementType.METHOD;
4+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
5+
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.Target;
8+
import java.time.temporal.ChronoUnit;
9+
10+
/**
11+
* Overrides global request timeout for this endpoint.
12+
*
13+
* <pre>{@code
14+
* @Client
15+
* interface CustomerApi {
16+
* @Get("/{id}")
17+
* @RequestTimeout(value = 1, ChronoUnit.SECONDS)
18+
* Customer getById(long id);
19+
* }
20+
*
21+
* }</pre>
22+
*/
23+
@Target(METHOD)
24+
@Retention(RUNTIME)
25+
public @interface RequestTimeout {
26+
long value();
27+
28+
ChronoUnit chronoUnit() default ChronoUnit.MILLIS;
29+
}

http-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
<dependency>
8484
<groupId>io.avaje</groupId>
8585
<artifactId>avaje-http-api</artifactId>
86-
<version>1.20</version>
86+
<version>${project.version}</version>
8787
<scope>test</scope>
8888
</dependency>
8989

http-generator-client/src/main/java/io/avaje/http/generator/client/ClientMethodWriter.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.avaje.http.generator.core.*;
44

55
import javax.lang.model.element.TypeElement;
6+
import java.util.Optional;
67
import java.util.Set;
78
import java.util.stream.Collectors;
89

@@ -24,6 +25,7 @@ class ClientMethodWriter {
2425
private MethodParam bodyHandlerParam;
2526
private String methodGenericParams = "";
2627
private final boolean useJsonb;
28+
private final Optional<RequestTimeoutPrism> timeout;
2729

2830
ClientMethodWriter(MethodReader method, Append writer, ProcessingContext ctx, boolean useJsonb) {
2931
this.method = method;
@@ -32,6 +34,7 @@ class ClientMethodWriter {
3234
this.ctx = ctx;
3335
this.returnType = Util.parseType(method.returnType());
3436
this.useJsonb = useJsonb;
37+
this.timeout = method.timeout();
3538
}
3639

3740
void addImportTypes(ControllerReader reader) {
@@ -86,12 +89,18 @@ void write() {
8689
writeQueryParams(pathSegments);
8790
writeBeanParams(pathSegments);
8891
writeFormParams(pathSegments);
92+
timeout.ifPresent(this::writeTimeout);
8993
writeBody();
9094
writeEnd();
9195
}
9296

93-
private void writeEnd() {
94-
WebMethod webMethod = method.webMethod();
97+
private void writeTimeout(RequestTimeoutPrism p) {
98+
99+
writer.append(" .requestTimeout(of(%s, %s))", p.value(), p.chronoUnit()).eol();
100+
}
101+
102+
private void writeEnd() {
103+
final var webMethod = method.webMethod();
95104
writer.append(" .%s()", webMethod.name()).eol();
96105
if (returnType == UType.VOID) {
97106
writer.append(" .asVoid();").eol();

http-generator-client/src/main/java/io/avaje/http/generator/client/ClientProcessor.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
import java.io.IOException;
44
import java.io.Writer;
55
import java.util.LinkedHashSet;
6-
import java.util.List;
6+
import java.util.Objects;
77
import java.util.Set;
88

99
import javax.annotation.processing.AbstractProcessor;
1010
import javax.annotation.processing.ProcessingEnvironment;
1111
import javax.annotation.processing.RoundEnvironment;
1212
import javax.annotation.processing.SupportedAnnotationTypes;
1313
import javax.lang.model.SourceVersion;
14-
import javax.lang.model.element.AnnotationMirror;
15-
import javax.lang.model.element.AnnotationValue;
1614
import javax.lang.model.element.Element;
1715
import javax.lang.model.element.TypeElement;
1816
import javax.tools.FileObject;
@@ -84,22 +82,11 @@ private void writeServicesFile() {
8482
}
8583

8684
private void writeForImported(Element importedElement) {
87-
for (AnnotationMirror annotationMirror : importedElement.getAnnotationMirrors()) {
88-
for (AnnotationValue value : annotationMirror.getElementValues().values()) {
89-
for (Object apiClassDef : (List<?>) value.getValue()) {
90-
writeImported(apiClassDef.toString());
91-
}
92-
}
93-
}
94-
}
9585

96-
private void writeImported(String fullName) {
97-
// trim .class suffix
98-
String apiClassName = fullName.substring(0, fullName.length() - 6);
99-
TypeElement typeElement = ctx.typeElement(apiClassName);
100-
if (typeElement != null) {
101-
writeClient(typeElement);
102-
}
86+
ImportPrism.getInstanceOn(importedElement).types().stream()
87+
.map(ctx::asElement)
88+
.filter(Objects::nonNull)
89+
.forEach(this::writeClient);
10390
}
10491

10592
private void writeClient(Element controller) {

http-generator-core/src/main/java/io/avaje/http/generator/core/MethodReader.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class MethodReader {
3939
private final PathSegments pathSegments;
4040
private final boolean hasValid;
4141
private final List<ExecutableElement> superMethods;
42+
private final Optional<RequestTimeoutPrism> timeout;
4243

4344
private WebMethod webMethod;
4445
private String webMethodPath;
@@ -62,7 +63,12 @@ public class MethodReader {
6263
this.securityRequirements = readSecurityRequirements();
6364
this.apiResponses = buildApiResponses();
6465
this.javadoc = buildJavadoc(element, ctx);
65-
66+
this.timeout = RequestTimeoutPrism.getOptionalOn(element);
67+
timeout.ifPresent(
68+
p -> {
69+
bean.addStaticImportType("java.time.temporal.ChronoUnit." + p.chronoUnit());
70+
bean.addStaticImportType("java.time.Duration.of");
71+
});
6672
if (isWebMethod()) {
6773
this.hasValid = initValid();
6874
this.pathSegments = PathSegments.parse(Util.combinePath(bean.path(), webMethodPath));
@@ -366,4 +372,8 @@ public String bodyName() {
366372
}
367373
return "body";
368374
}
375+
376+
public Optional<RequestTimeoutPrism> timeout() {
377+
return timeout;
378+
}
369379
}

http-generator-core/src/main/java/io/avaje/http/generator/core/package-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
@GeneratePrism(value = org.jetbrains.annotations.NotNull.class, publicAccess = true)
3232
@GeneratePrism(value = io.avaje.http.api.Client.class, publicAccess = true)
3333
@GeneratePrism(value = io.avaje.http.api.Client.Import.class, publicAccess = true)
34+
@GeneratePrism(value = io.avaje.http.api.RequestTimeout.class, publicAccess = true)
3435
package io.avaje.http.generator.core;
3536

3637
import io.avaje.prism.GeneratePrism;

0 commit comments

Comments
 (0)