A simple watchdog context written in Go
Unlike the context.WithTimeout
function in Go, which automatically cancels after a fixed time, Watchgod's timer, created using WithWatchdog
, can be reset. This feature grants users the ability to extend the context and prevent cancellation as needed, offering more fine-grained control over the context's lifecycle.
Run this command to try it out!
go get github.com/stefanbildl/[email protected]
The WithWatchdog function returns a context that is derived from the provided parent context, along with a ResetFn function to reset the watchdog timer.
The function creates a child context from the provided parent context. A separate goroutine manages a timer that triggers a cancellation of the context after the specified timeout duration. The ResetFn function allows resetting the timer to prevent the context from being cancelled. If the timeout elapses without the watchdog being reset, the context is cancelled, and any operations depending on it should handle the cancellation appropriately.
package main
import (
"context"
"fmt"
"time"
"github.com/stefanbildl/watchgod"
)
func main() {
parentContext := context.Background()
timeoutDuration := 5 * time.Second
ctx, reset := watchgod.WithWatchdog(parentContext, timeoutDuration)
// Use the context and reset function as needed
// ...
// Reset the watchdog timer
reset()
select {
case <-ctx.Done():
fmt.Println("Context cancelled due to timeout.")
// Handle cancellation logic
// ...
}
}
I welcome contributions from the community to improve this project! Please follow these guidelines to contribute effectively:
- Fork the repository to your GitHub account.
- Clone the forked repository to your local machine:
git clone https://github.com/your-username/repository.git
- Create a new branch for your changes:
git checkout -b feature/YourFeature
- Make your changes, following the project's coding style and guidelines.
- Test your changes thoroughly.
- Commit your changes:
git commit -am 'feat: Description of your changes'
- Push to your forked repository:
git push origin feature/YourFeature
- Open a Pull Request (PR) from your forked repository to the main repository's
main
branch. - Provide a detailed description of your changes in the PR, including the motivation and context for the changes.
- Follow the existing code style and conventions.
- Write descriptive commit messages in the present tense.
- Ensure any new code includes relevant comments and documentation.
- Test your changes thoroughly and ensure there are no linting errors or warnings.
If you encounter bugs, have feature requests, or want to propose enhancements, please open an issue in the repository. Ensure to include relevant details and steps to reproduce the issue.
For questions or support related to the project, please open an issue in the repository or reach out to me.
I appreciate and welcome your contributions to this project!