Configuring server timeouts

There are several ways to override the default timeouts at server-side.

Using ServerBuilder

import com.linecorp.armeria.server.Server;
import com.linecorp.armeria.server.ServerBuilder;

import java.time.Duration;

ServerBuilder sb = Server.builder();
Server server = sb.http(port)
                  .annotatedService(new HelloService())
                  .requestTimeout(Duration.ofSeconds(5))
                  .build();

Using VirtualHostBuilder

You can set the timeouts for each virtual host.

import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.server.Server;
import com.linecorp.armeria.server.ServerBuilder;

import java.time.Duration;

import static com.linecorp.armeria.common.HttpStatus.OK;

ServerBuilder sb = Server.builder();
Server server = sb.http(port)
                  .defaultVirtualHost()
                  .service("/hello1", (ctx, req) -> HttpResponse.of(OK))
                  .requestTimeout(Duration.ofSeconds(5))
                  .and()
                  .virtualHost("*.foo.com")
                  .service("/hello2", (ctx, req) -> HttpResponse.of(OK))
                  .requestTimeout(Duration.ofSeconds(10))
                  .and()
                  .withVirtualHost(builder -> {
                      builder.hostnamePattern("*.bar.com")
                             .service("/hello3", (ctx, req) -> HttpResponse.of(OK))
                             .requestTimeout(Duration.ofSeconds(15));
                  })
                  .build();

Using ServiceBindingBuilder

You can set the timeouts for each route.

import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.server.Server;
import com.linecorp.armeria.server.ServerBuilder;

import java.time.Duration;

import static com.linecorp.armeria.common.HttpStatus.OK;

ServerBuilder sb = Server.builder();
Server server = sb.http(port)
                  .route()
                  .get("/hello1")
                  .requestTimeout(Duration.ofSeconds(5))
                  .build((ctx, req) -> HttpResponse.of(OK))
                  .withRoute(builder -> {
                      builder.get("/hello2")
                             .requestTimeout(Duration.ofSeconds(10))
                             .build((ctx, req) -> HttpResponse.of(OK));
                  })
                  .build();

Using ServiceRequestContext

You can use the context to set the timeouts for a request.

import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.server.Server;
import com.linecorp.armeria.server.ServerBuilder;

import java.time.Duration;

import static com.linecorp.armeria.common.HttpStatus.OK;

ServerBuilder sb = Server.builder();
Server server = sb.http(port)
                  .service("/hello", (ctx, req) -> {
                      ctx.setRequestTimeout(Duration.ofSeconds(5));
                      return HttpResponse.of(OK);
                  })
                  .build();

Using @RequestTimeout Annotation

You can use the @RequestTimeout annotation for annotated services and gRPC services.

HelloService.java
import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.server.ServiceRequestContext;
import com.linecorp.armeria.server.annotation.Get;
import com.linecorp.armeria.server.annotation.decorator.RequestTimeout;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

import static com.linecorp.armeria.common.HttpStatus.OK;

@RequestTimeout(value = 10, unit = TimeUnit.SECONDS)
public class HelloService {

    @Get("/hello")
    @RequestTimeout(5000)
    public HttpResponse getHello() {
        return HttpResponse.of(OK);
    }

}

Global Configuration

You may globally configure the default timeout with system properties or a custom FlagsProvider.

Using FlagsProvider

You can override the request timeout specified by JVM system properties by implementing your own FlagsProvider and loading it via Java SPI.

package com.example.providers;

// Add the following text file to your classpath or JAR file:
//
// $ cat META-INF/services/com.linecorp.armeria.common.FlagsProvider
// com.example.providers.MyFlagsProvider
//
public class MyFlagsProvider implements FlagsProvider {
  @Override
  public int priority() {
    // The provider with higher value will be evaluated first.
    return 100;
  }

  @Override
  public Long defaultRequestTimeoutMillis() {
      return 1000L;
  }
}

Using JVM system properties

You can override the default server timeouts by specifying the following JVM system properties if you do not prefer setting it programmatically.

-Dcom.linecorp.armeria.defaultRequestTimeoutMillis=<long>

Represents the amount of time from receiving a request to sending the corresponding response completely. Default: 10000.