Skip to content

Commit

Permalink
Merge pull request #43 from eapache/cleanup-deadline
Browse files Browse the repository at this point in the history
Simplify deadline a bit
  • Loading branch information
eapache authored Jan 13, 2024
2 parents f26ac90 + 5a1d983 commit b938ac8
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions deadline/deadline.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,15 @@ func New(timeout time.Duration) *Deadline {
// Run runs the given function, passing it a stopper channel. If the deadline passes before
// the function finishes executing, Run returns ErrTimeOut to the caller and closes the stopper
// channel so that the work function can attempt to exit gracefully. It does not (and cannot)
// simply kill the running function, so if it doesn't respect the stopper channel then it may
// keep running after the deadline passes. If the function finishes before the deadline, then
// the return value of the function is returned from Run.
// kill the running function's goroutine, so if the function doesn't respect the stopper channel,
// then it may keep running after the deadline passes. If the function finishes before the
// deadline, then the return value of the function is returned from Run.
func (d *Deadline) Run(work func(<-chan struct{}) error) error {
result := make(chan error)
result := make(chan error, 1)
stopper := make(chan struct{})

go func() {
value := work(stopper)
select {
case result <- value:
case <-stopper:
}
result <- work(stopper)
}()

timer := time.NewTimer(d.timeout)
Expand Down

0 comments on commit b938ac8

Please sign in to comment.