-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtimed.go
More file actions
40 lines (34 loc) · 836 Bytes
/
timed.go
File metadata and controls
40 lines (34 loc) · 836 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package goneli
import (
"errors"
"time"
"github.com/obsidiandynamics/libstdgo/concurrent"
"github.com/obsidiandynamics/libstdgo/scribe"
)
var errPerformWithNoError = errors.New("")
func void(f func()) func() error {
return func() error {
f()
return nil
}
}
func performTimed(logger scribe.Logger, opName string, op func() error, timeout time.Duration) (bool, error) {
errorRef := concurrent.NewAtomicReference()
go func() {
err := op()
if err != nil {
errorRef.Set(err)
} else {
errorRef.Set(errPerformWithNoError)
}
}()
res := errorRef.Await(concurrent.RefNot(concurrent.RefNil()), timeout)
if res == nil {
logger("Operation '%s' failed to complete within %v", opName, timeout)
return false, nil
}
if err := res.(error); err != errPerformWithNoError {
return true, err
}
return true, nil
}