This repository has been archived by the owner on Dec 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathcoop.go
137 lines (124 loc) · 3.05 KB
/
coop.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// coop contains some of the most commonly used concurrent
// flows. This package is mostly around for reference. even though
// you can use it as a library, I'd suggest you to implement your own
// control mechanisms.
package coop
import (
"sync"
"time"
)
// Runs fn at the specified time.
func At(t time.Time, fn func()) (done <-chan bool) {
return After(t.Sub(time.Now()), fn)
}
// Runs until time in every dur.
func Until(t time.Time, dur time.Duration, fn func()) (done <-chan bool) {
ch := make(chan bool, 1)
untilRecv(ch, t, dur, fn)
return ch
}
func untilRecv(ch chan bool, t time.Time, dur time.Duration, fn func()) {
if t.Sub(time.Now()) > 0 {
time.AfterFunc(dur, func() {
fn()
untilRecv(ch, t, dur, fn)
})
return
}
doneSig(ch, true)
}
// Runs fn after duration. Similar to time.AfterFunc
func After(duration time.Duration, fn func()) (done <-chan bool) {
ch := make(chan bool, 1)
time.AfterFunc(duration, func() {
fn()
doneSig(ch, true)
})
return ch
}
// Runs fn in every specified duration.
func Every(dur time.Duration, fn func()) {
time.AfterFunc(dur, func() {
fn()
Every(dur, fn)
})
}
// Runs fn and times out if it runs longer than the provided
// duration. It will send false to the returning
// channel if timeout occurs.
// TODO: cancel if timeout occurs
func Timeout(duration time.Duration, fn func()) (done <-chan bool) {
ch := make(chan bool, 2)
go func() {
<-time.After(duration)
doneSig(ch, false)
}()
go func() {
fn()
doneSig(ch, true)
}()
return ch
}
// Starts to run the given list of fns concurrently.
func All(fns ...func()) (done <-chan bool) {
var wg sync.WaitGroup
wg.Add(len(fns))
ch := make(chan bool, 1)
for _, fn := range fns {
go func(f func()) {
f()
wg.Done()
}(fn)
}
go func() {
wg.Wait()
doneSig(ch, true)
}()
return ch
}
// Starts to run the given list of fns concurrently,
// at most n fns at a time.
func AllWithThrottle(throttle int, fns ...func()) (done <-chan bool) {
ch := make(chan bool, 1)
go func() {
for {
num := throttle
if throttle > len(fns) {
num = len(fns)
}
next := fns[:num]
fns = fns[num:]
<-All(next...)
if len(fns) == 0 {
doneSig(ch, true)
break
}
}
}()
return ch
}
// Run the same function with n copies.
func Replicate(n int, fn func()) (done <-chan bool) {
funcs := make([]func(), n)
for i := 0; i < n; i++ {
funcs[i] = fn
}
return All(funcs...)
}
func doneSig(ch chan bool, val bool) {
ch <- val
close(ch)
}