-
Notifications
You must be signed in to change notification settings - Fork 0
/
future.go
73 lines (55 loc) · 1 KB
/
future.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
package future
import (
"github.com/panjf2000/ants/v2"
)
type _FutureI interface {
Err() error
OK() bool
mustBeFutureType()
}
type Future[T any] struct {
ch chan struct{}
value T
err error
}
func NewFuture[T any]() *Future[T] {
return &Future[T]{
ch: make(chan struct{}),
}
}
func (future *Future[T]) Await() (T, error) {
<-future.ch
return future.value, future.err
}
func (future *Future[T]) Value() T {
<-future.ch
return future.value
}
func (future *Future[T]) Err() error {
<-future.ch
return future.err
}
func (future *Future[T]) OK() bool {
<-future.ch
return future.err == nil
}
func (future *Future[T]) Inner() <-chan struct{} {
return future.ch
}
func (future *Future[T]) mustBeFutureType() {
}
type Pool struct {
inner *ants.Pool
}
func NewRuntime(cap int, opts ...ants.Option) *Pool {
pool, err := ants.NewPool(cap, opts...)
if err != nil {
panic(err)
}
return &Pool{
inner: pool,
}
}
func (r *Pool) Submit(method func()) error {
return r.inner.Submit(method)
}