Skip to content

Instantly share code, notes, and snippets.

@kylelemons
Created June 28, 2011 04:24
Show Gist options
  • Save kylelemons/1050485 to your computer and use it in GitHub Desktop.
Save kylelemons/1050485 to your computer and use it in GitHub Desktop.
Demonstrate timeouts
$ for WORKTIME in {1..5}0; do echo -n "$WORKTIME: "; ./timeout --timeout 25 --worktime $WORKTIME; done
10: Worker finished
20: Worker finished
30: Timeout
40: Timeout
50: Timeout
package main
import (
"fmt"
"time"
"flag"
)
var (
workerTime = flag.Int64("worktime", 50, "Time for worker to run (ms)")
timeout = flag.Int64("timeout", 25, "Timeout (ms)")
)
func worker(done chan bool) {
// simulate long-running computation
<-time.After(*workerTime * 1e6)
done <- true
}
func main() {
flag.Parse()
done := make(chan bool, 1)
go worker(done)
select {
case <-done:
fmt.Println("Worker finished")
case <-time.After(*timeout * 1e6):
fmt.Println("Timeout")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment