https://github.com/spatialcurrent/go-deadline
Library to create deadlines for goroutines and programs
https://github.com/spatialcurrent/go-deadline
Last synced: 21 days ago
JSON representation
Library to create deadlines for goroutines and programs
- Host: GitHub
- URL: https://github.com/spatialcurrent/go-deadline
- Owner: spatialcurrent
- License: mit
- Created: 2020-02-05T14:00:11.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-02-05T15:02:55.000Z (almost 6 years ago)
- Last Synced: 2024-06-20T15:49:08.329Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://circleci.com/gh/spatialcurrent/go-deadline/tree/master) [](https://goreportcard.com/report/spatialcurrent/go-deadline) [](https://godoc.org/github.com/spatialcurrent/go-deadline) [](https://github.com/spatialcurrent/go-deadline/blob/master/LICENSE)
# go-deadline
## Description
**go-deadline** is a library to create deadlines for goroutines and programs. This package is used as a safeguard to prevent a goroutine, test, or program from exhausting resources or otherwise running beyond the expected duration of time. You can also use this library for automatically introducing some chaos into your container environment to test failover and network resilience.
# Usage
**Go**
You can import **go-deadline** as a library with:
```go
import (
"time"
"github.com/spatialcurrent/go-deadline/pkg/deadline"
)
```
You can create a deadline near the start of your program.
```go
func main() {
...
d, err := deadline.New(5*time.Second, deadline.ExitError)
if err != nil {
return fmt.Errorf("error creating deadline: %w", err)
}
err := d.Start()
if err != nil {
return fmt.Errorf("error starting deadline: %w", err)
}
// deadline is no running in a separate goroutine
...
}
```
If you do not care to handle errors yourself, you can use the `deadline.MustStart` function with the default `deadline.ExitError` function.
```go
func main() {
...
deadline.MustStart(context.Background(), 5*time.Second, deadline.ExitError)
...
}
```
Alternatively, if you wish to post a custom error to `stderr`, you can provide a custom function as below.
```go
func main() {
...
deadline.MustStart(context.Background(), 5*time.Second, func(ctx context.Context) {
fmt.Fprintln(os.Stderr, "deadline reached")
os.Exit(1)
})
...
}
```
See [deadline](https://godoc.org/github.com/spatialcurrent/go-deadline/pkg/deadline) in GoDoc for further API documentation.
# Testing
To run Go tests use `make test_go` (or `bash scripts/test.sh`), which runs unit tests, `go vet`, `go vet with shadow`, [errcheck](https://github.com/kisielk/errcheck), [ineffassign](https://github.com/gordonklaus/ineffassign), [staticcheck](https://staticcheck.io/), and [misspell](https://github.com/client9/misspell).
# Contributing
[Spatial Current, Inc.](https://spatialcurrent.io) is currently accepting pull requests for this repository. We'd love to have your contributions! Please see [Contributing.md](https://github.com/spatialcurrent/go-deadline/blob/master/CONTRIBUTING.md) for how to get started.
# License
This work is distributed under the **MIT License**. See **LICENSE** file.