-
Notifications
You must be signed in to change notification settings - Fork 60
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
Enhance Error Execution Utilities: Add Retry, Timeout, Cancellation, and Advanced Error Handling #153
Enhance Error Execution Utilities: Add Retry, Timeout, Cancellation, and Advanced Error Handling #153
Conversation
…and advanced error handling This commit introduces several improvements to the error execution utilities to make them more robust and production-ready. The following changes have been implemented: 1. **Timeout and Cancellation Support**: - Updated `ErrExec` and `ErrExecWithTimeout` to support context-based cancellation and timeout. This ensures that functions can be terminated early if they take too long or if the operation is canceled. 2. **Retry Logic**: - Added a `RetryExec` function to retry failed functions a configurable number of times with delays in between retries. - Added `ErrExecWithRetry` to execute functions concurrently while automatically retrying any failed ones. 3. **Advanced Error Handling**: - Enhanced `ErrExecSequential` to accumulate errors and stop execution based on custom error types (e.g., stop on `CustomError` with a critical severity). - Introduced `CustomError` with logic to control whether execution should stop upon encountering a specific error. 4. **Logging and Monitoring**: - Added `LogExecution` to log the execution time of functions. This helps in monitoring performance and debugging execution times. These improvements make the utilities more flexible, resilient, and suitable for handling complex scenarios in production systems, such as dealing with timeouts, retries, and advanced error handling. Signed-off-by: UTSAV SINGHAL <[email protected]>
…ogic, and logging - Added timeout and cancellation support to ErrExec and ErrExecWithTimeout functions. - Introduced retry logic in RetryExec to handle temporary failures with a configurable retry count and delay. - Updated ErrExecSequential to support more flexible error accumulation and early termination on critical errors using CustomError. - Added LogExecution to track function execution time, integrated with ErrExecWithLogging for concurrent function execution. - Improved error handling for functions, allowing for retries and graceful error reporting. These changes improve the robustness and flexibility of error handling in concurrent and sequential function executions. Signed-off-by: UTSAV SINGHAL <[email protected]>
Hi @shubham19may @zriyanshdz ,please review this PR and update if any necessary changes required. |
cc - @shubham19may @hash-data |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems, these changes are not necessary. Can you take some important issues that are required.
Thanks
func ErrExec(functions ...func() error) error { | ||
group, _ := errgroup.WithContext(context.Background()) | ||
group, ctx := errgroup.WithContext(context.Background()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this ctx is not passed to any function so no cancellation will happen.
group.Go(func() error { | ||
select { | ||
case <-ctx.Done(): // Check if the context is canceled or times out | ||
return ctx.Err() | ||
default: | ||
return one() | ||
} | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not required
func RetryExec(function func() error, retries int, delay time.Duration) error { | ||
var err error | ||
for i := 0; i <= retries; i++ { | ||
err = function() | ||
if err == nil { | ||
return nil // Function succeeded | ||
} | ||
log.Printf("Attempt %d failed: %v", i+1, err) | ||
time.Sleep(delay) // Wait before retrying | ||
} | ||
|
||
return fmt.Errorf("failed after %d retries: %w", retries, err) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
already available check backoff
// CustomError represents a custom error type with an additional field to indicate if execution should stop. | ||
type CustomError struct { | ||
Message string | ||
Severity string | ||
} | ||
|
||
func (e *CustomError) Error() string { | ||
return e.Message | ||
} | ||
|
||
// ShouldStop checks if the custom error indicates that execution should be halted. | ||
func (e *CustomError) ShouldStop() bool { | ||
return e.Severity == "critical" // Example logic: Stop on "critical" errors | ||
} | ||
|
||
// LogExecution logs the execution of a function (can be customized as needed). | ||
func LogExecution(functionName string, startTime time.Time) { | ||
duration := time.Since(startTime) | ||
log.Printf("Function '%s' executed in %v", functionName, duration) | ||
} | ||
|
||
// ErrExecWithLogging executes a list of functions concurrently and logs execution time for each. | ||
func ErrExecWithLogging(functions ...func() error) error { | ||
group, ctx := errgroup.WithContext(context.Background()) | ||
|
||
for _, one := range functions { | ||
group.Go(func() error { | ||
startTime := time.Now() | ||
defer LogExecution("Function", startTime) // Log execution after function completes | ||
return one() | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dont think so it is required right now.
@UTSAVS26 closing this as currently its not needed. most of the changes done are not needed or already implemented as a part of other function. |
This PR enhances the existing error execution utilities with new features such as timeout, cancellation, retry logic, and execution logging. Specifically:
ErrExecSequential
to accumulate errors and stop execution on critical errors using a custom error type.These improvements make the error handling system more robust, especially for real-world applications where failures can occur intermittently and tasks need to be managed carefully.
Fixes #130
Type of change
How Has This Been Tested?