Giter Site home page Giter Site logo

watchgod's Introduction

watchgod

A simple watchdog context written in Go

What does it do?

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.

Go Get watchgod

Run this command to try it out!

 go get github.com/stefanbildl/[email protected]

WithWatchdog

The WithWatchdog function returns a context that is derived from the provided parent context, along with a ResetFn function to reset the watchdog timer.

Behavior

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.

Example

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
		// ...
	}
}

Contributing

I welcome contributions from the community to improve this project! Please follow these guidelines to contribute effectively:

How to Contribute

  1. Fork the repository to your GitHub account.
  2. Clone the forked repository to your local machine:
    git clone https://github.com/your-username/repository.git
  3. Create a new branch for your changes:
    git checkout -b feature/YourFeature
  4. Make your changes, following the project's coding style and guidelines.
  5. Test your changes thoroughly.
  6. Commit your changes:
    git commit -am 'feat: Description of your changes'
  7. Push to your forked repository:
    git push origin feature/YourFeature
  8. Open a Pull Request (PR) from your forked repository to the main repository's main branch.
  9. Provide a detailed description of your changes in the PR, including the motivation and context for the changes.

Code Style and Guidelines

  • 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.

Issue Reporting

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.

Questions and Support

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!

watchgod's People

Contributors

stefanbildl avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.