~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/gopkg.in/goose.v1/sync/timeout.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package sync
 
2
 
 
3
import (
 
4
        "time"
 
5
)
 
6
 
 
7
// RunWithTimeout runs the specified function and returns true if it completes before timeout, else false.
 
8
func RunWithTimeout(timeout time.Duration, f func()) bool {
 
9
        ch := make(chan struct{})
 
10
        go func() {
 
11
                f()
 
12
                close(ch)
 
13
        }()
 
14
        select {
 
15
        case <-ch:
 
16
                return true
 
17
        case <-time.After(timeout):
 
18
        }
 
19
        return false
 
20
}