Skip to content
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

Server-side timeout mechanism #10361

Open
sorra opened this issue Jul 9, 2023 · 0 comments
Open

Server-side timeout mechanism #10361

sorra opened this issue Jul 9, 2023 · 0 comments
Assignees
Milestone

Comments

@sorra
Copy link

sorra commented Jul 9, 2023

Is your feature request related to a problem?

Regarding #9684 , there have been multiple asks in the community for the server-side timeout. Servlets and database connections support timeout, so why gRPC does not provide it?
Our application is having such a problem. The grpc-java server usually runs workers in a ThreadPoolExecutor with a maximum size (infinite size is not better with the problem). If some server call instances run infinitely (e.g. having an infinite loop or in a waiting state infinitely), they will occupy some threads. And if this situation persists, eventually all threads in the pool will be occupied and no work can be done anymore, which results in a service downtime.
The client-side timeout only helps the client stop infinite waiting, it does not help the server stop infinite processing.

Describe the solution you'd like

Per the comment by @ejona86 #9684 (comment) , application developers can do it via a server interceptor. But it is not simple work for application developers, so why not provide an optional server interceptor in the grpc-java framework?

I have developed two classes for doing so: TimeoutServerInterceptor (the server interceptor) and ServerTimeoutManager (the global manager of the timeout queue). The timeout value is configurable and is only applied to unary calls because we do not expect a general timeout limit for streaming calls. I want to merge them into grpc-java codebase.
I have created a pull request #10360

My solution is as follows:

  • Intercept each RPC method invocation:
    • before the invocation, wrap the current thread as a TimeoutTask and schedule the task after a delay that is equal to the timeout value;
    • after the invocation, invalidate the TimeoutTask because the invocation has been completed in time, so the thread will not be mis-interrupted when it is reused for another server call.
  • If the invocation does not complete in time, the related TimeoutTask will be executed by the scheduler, and it interrupts the wrapped thread.
    Please let me know if my solution is suitable for merging.

Describe alternatives you've considered

Application developers can also perform AOP on their RPC method implementation to control how long a method executes. But this way: (1) is less easy to use, (2) requires an extra AOP framework, and (3) requires application developers themselves to guarantee every RPC method is AOP-weaved. It is also difficult to distinguish unary calls from streaming calls. So I prefer the gRPC native server interceptor solution.

Additional context

@sanjaypujare sanjaypujare changed the title Server-side timeout mechansim Server-side timeout mechanism Jul 24, 2023
@larry-safran larry-safran added this to the Unscheduled milestone Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants