-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
37 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Timeout Pattern in Go | ||
|
||
The Timeout pattern is used to limit the execution time of a function or operation. It ensures that the operation completes within a specified time duration, and if it exceeds that duration, it either returns a default value or invokes a fallback function. | ||
|
||
## Usage | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/kinfinity/distributed-resilience/timeout" | ||
) | ||
|
||
func main() { | ||
// Create a Timeout instance with a fallback function | ||
timeout := timeout.NewTimeOutWithFallback(5 * time.Second, func() error { | ||
// Custom fallback logic here | ||
return nil | ||
}) | ||
|
||
// Watch for timeout | ||
result := timeout.Watch(executionCompletionChan) | ||
if result.result { | ||
// Operation completed within the timeout duration | ||
} else { | ||
// Operation timed out | ||
} | ||
} | ||
``` | ||
|
||
# **References** | ||
|
||
- [codecentric resilience-design-patterns](https://www.codecentric.de/wissens-hub/blog/resilience-design-patterns-retry-fallback-timeout-circuit-breaker) |
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"context" | ||
"fmt" | ||
"time" | ||
|
||
) | ||
|
||
// Timeout | ||
|