-
Notifications
You must be signed in to change notification settings - Fork 8
/
delay_element.go
123 lines (106 loc) · 2.77 KB
/
delay_element.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
package mono
import (
"context"
"sync/atomic"
"time"
"github.com/jjeffcaii/reactor-go"
"github.com/jjeffcaii/reactor-go/hooks"
"github.com/jjeffcaii/reactor-go/internal"
"github.com/jjeffcaii/reactor-go/scheduler"
)
var (
_ reactor.Subscription = (*delayElementSubscriber)(nil)
_ reactor.Subscriber = (*delayElementSubscriber)(nil)
)
const (
_ = iota
_delayNextStatRunning
_delayNextStatConfirmed
_delayNextStatFinish
)
type delayElementSubscriber struct {
delay time.Duration
sc scheduler.Scheduler
ctx context.Context
s reactor.Subscription
actual reactor.Subscriber
done int32
nextStat int32
cancelled chan struct{}
}
func (d *delayElementSubscriber) Request(n int) {
d.s.Request(n)
}
func (d *delayElementSubscriber) Cancel() {
d.s.Cancel()
internal.SafeCloseDone(d.cancelled)
}
func (d *delayElementSubscriber) OnSubscribe(ctx context.Context, s reactor.Subscription) {
d.s = s
d.ctx = ctx
d.actual.OnSubscribe(ctx, d)
}
func (d *delayElementSubscriber) OnError(err error) {
if atomic.CompareAndSwapInt32(&d.done, 0, 1) {
d.actual.OnError(err)
return
}
hooks.Global().OnErrorDrop(err)
}
func (d *delayElementSubscriber) OnNext(v Any) {
if atomic.LoadInt32(&d.done) != 0 {
hooks.Global().OnNextDrop(v)
return
}
atomic.StoreInt32(&d.nextStat, _delayNextStatRunning)
if err := d.sc.Worker().Do(func() {
select {
case <-d.ctx.Done():
d.OnError(reactor.ErrSubscribeCancelled)
case <-time.After(d.delay):
d.actual.OnNext(v)
if atomic.CompareAndSwapInt32(&d.nextStat, _delayNextStatConfirmed, _delayNextStatFinish) {
d.actual.OnComplete()
}
case <-d.cancelled:
d.OnError(reactor.ErrSubscribeCancelled)
}
}); err != nil {
d.OnError(err)
}
}
func (d *delayElementSubscriber) OnComplete() {
if !atomic.CompareAndSwapInt32(&d.done, 0, 1) {
return
}
if !atomic.CompareAndSwapInt32(&d.nextStat, _delayNextStatRunning, _delayNextStatConfirmed) {
d.actual.OnComplete()
}
}
func newDelayElementSubscriber(actual reactor.Subscriber, delay time.Duration, sc scheduler.Scheduler) reactor.Subscriber {
return &delayElementSubscriber{
delay: delay,
actual: actual,
sc: sc,
cancelled: make(chan struct{}),
}
}
type monoDelayElement struct {
source reactor.RawPublisher
delay time.Duration
sc scheduler.Scheduler
}
func (p *monoDelayElement) Parent() reactor.RawPublisher {
return p.source
}
func (p *monoDelayElement) SubscribeWith(ctx context.Context, actual reactor.Subscriber) {
actual = newDelayElementSubscriber(actual, p.delay, p.sc)
p.source.SubscribeWith(ctx, actual)
}
func newMonoDelayElement(source reactor.RawPublisher, delay time.Duration, sc scheduler.Scheduler) *monoDelayElement {
return &monoDelayElement{
source: source,
delay: delay,
sc: sc,
}
}