line / armeria

Your go-to microservice framework for any situation, from the creator of Netty et al. You can build any type of microservice leveraging your favorite technologies, including gRPC, Thrift, Kotlin, Retrofit, Reactive Streams, Spring Boot and Dropwizard.
https://armeria.dev
Apache License 2.0
4.73k stars 899 forks source link

Add `StreamMessage.timeout()` #5744

Open ikhoon opened 3 weeks ago

ikhoon commented 3 weeks ago

Currently, there is no timeout for StreamMessage's aggregate() or subscribe(). If we want to give a timeout for HttpRequest.aggregate(), a separate scheduler needs to be run to abort the request if it is not completed within the given time.

The timeout API can also be used to detect an idle stream by setting a timeout until the next message. https://github.com/line/armeria/pull/5713#issuecomment-2150498394

enum StreamTimeoutMode {
    UNTIL_FIRST,
    // Maybe the default one
    UNTIL_NEXT,
    UNTIL_EOS // or UNTIL_LAST
}

StreamMessage<Object> stream = ...;
// Sets a timeout for each message to 5 seconds.
stream.timeout(5_seconds).subscribe(....);

// UNTIL_EOS will be useful for unary requests.
HttpRequest req = ...;
req.timeout(StreamTimeoutMode.UNTIL_EOS, 10_seconds).aggregate();
req.aggregate(10_seconds); // Shortcut

// UNTIL_NEXT will be useful for stream requests such as gRPC streams or WebSocket.
WebSocket input = ...;
input.timeout(UNTIL_NEXT, 3_seconds).subscribe(...);
sjy982 commented 3 weeks ago

I'm in charge of this issue.

ikhoon commented 3 weeks ago

Yes, please. 🙇