-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathatomicjob.go
67 lines (53 loc) · 1.38 KB
/
atomicjob.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package elemental
import (
"context"
"sync"
)
// AtomicJob takes a func() error and returns a func(context.Context) error.
// The returned function can be called as many time as you like, but only
// one instance of the given job can be run at the same time.
//
// The returned function will either execute job if it
// it not already running or wait for the currently running job to finish.
// In both cases, the returned error from the job will be forwareded and returned
// to every caller.
//
// You must pass a context.Context to the returned function so you can
// control how much time you are willing to wait for the job to complete.
//
// If you wish to change some external state from within the job function,
// it is your responsibility to ensure everything is thread safe.
func AtomicJob(job func() error) func(context.Context) error {
var l sync.RWMutex
var errorChs []chan error
sem := make(chan struct{}, 1)
return func(ctx context.Context) error {
errCh := make(chan error)
l.Lock()
errorChs = append(errorChs, errCh)
l.Unlock()
select {
case sem <- struct{}{}:
go func() {
err := job()
l.Lock()
for _, ch := range errorChs {
select {
case ch <- err:
default:
}
}
errorChs = nil
l.Unlock()
<-sem
}()
default:
}
select {
case err := <-errCh:
return err
case <-ctx.Done():
return ctx.Err()
}
}
}