-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ddsched plugin gRPC calls have timeout by default
- Loading branch information
Michal Tichák
committed
Oct 22, 2024
1 parent
153a87c
commit 6455cbe
Showing
3 changed files
with
77 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* === This file is part of ALICE O² === | ||
* | ||
* Copyright 2021 CERN and copyright holders of ALICE O². | ||
* Author: Teo Mrnjavac <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* In applying this license CERN does not waive the privileges and | ||
* immunities granted to it by virtue of its status as an | ||
* Intergovernmental Organization or submit itself to any jurisdiction. | ||
*/ | ||
|
||
package integration | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func UnaryTimeoutInterceptor(timeout time.Duration, cause string) grpc.UnaryClientInterceptor { | ||
return func( | ||
ctx context.Context, | ||
method string, | ||
req, reply interface{}, | ||
connection *grpc.ClientConn, | ||
invoker grpc.UnaryInvoker, | ||
opts ...grpc.CallOption, | ||
) error { | ||
// Create a context with a timeout | ||
ctx, cancel := context.WithTimeoutCause(ctx, timeout, errors.New(cause)) | ||
defer cancel() // Ensure the context is canceled after the call | ||
|
||
// Invoke the RPC call with the new context | ||
err := invoker(ctx, method, req, reply, connection, opts...) | ||
if err != nil { | ||
st, _ := status.FromError(err) | ||
// Handle error, maybe logging or processing the error status | ||
return st.Err() | ||
} | ||
return nil | ||
} | ||
} |