-
Notifications
You must be signed in to change notification settings - Fork 554
[Feature] Add timeout for apiserver grpc server #3427
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
kevin85421
merged 17 commits into
ray-project:master
from
machichima:3344-apiserver-timeout-grpc-server
May 6, 2025
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c677b80
feat: add grpc server timeout
machichima 9cbf138
Merge branch 'master' of github.com:ray-project/kuberay into 3344-api…
machichima 33cf5da
test: unit test for timeout interceptor
machichima d34686f
fix: update timeout to 60 seconds
machichima ebb5ba4
feat: enable setting grpc timeout by env variable
machichima cade329
refactor: default timeout to constant
machichima a8fee22
fix: update time out error message
machichima d4a554a
fix: add default timeout value in warning message
machichima f1b5baf
feat: set gRPC timeout in flag instead of env var
machichima f8ad90f
Merge branch 'master' of github.com:ray-project/kuberay into 3344-api…
machichima 4c897aa
test: update mock handler that mimic grpc package handler
machichima aa7fcb5
feat: simplify timeout interceptor
machichima 6425633
fix: variable type to log
machichima 79a6349
docs: add docstring for mock Handle
machichima 54c8d57
fix: remove suffix in timeout constant
machichima 897ad01
Merge branch 'master' of github.com:ray-project/kuberay into 3344-api…
machichima 47f872a
style: add comment for constant args
machichima File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ package interceptor | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"google.golang.org/grpc" | ||
klog "k8s.io/klog/v2" | ||
|
@@ -19,3 +21,40 @@ func APIServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary | |
klog.Infof("%v handler finished", info.FullMethod) | ||
return | ||
} | ||
|
||
func TimeoutInterceptor(timeout time.Duration) grpc.UnaryServerInterceptor { | ||
return func( | ||
ctx context.Context, | ||
req interface{}, | ||
_ *grpc.UnaryServerInfo, | ||
handler grpc.UnaryHandler, | ||
) (interface{}, error) { | ||
// Create a context with timeout | ||
ctx, cancel := context.WithTimeout(ctx, timeout) | ||
dentiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
defer cancel() | ||
dentiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Channel to capture execution result | ||
done := make(chan struct{}) | ||
var ( | ||
resp interface{} | ||
err error | ||
) | ||
|
||
go func() { | ||
resp, err = handler(ctx, req) | ||
close(done) | ||
}() | ||
|
||
select { | ||
case <-ctx.Done(): | ||
// Raise error if time out | ||
if ctx.Err() == context.DeadlineExceeded { | ||
return nil, fmt.Errorf("grpc server timed out") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we name the grpc server with KubeRay API server ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure! Just changed |
||
} | ||
return nil, ctx.Err() | ||
case <-done: | ||
// Handler finished | ||
return resp, err | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.