/
base.go
119 lines (103 loc) · 3.08 KB
/
base.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package subscribers
import (
"context"
"time"
"code.vegaprotocol.io/vega/core/events"
)
type Base struct {
ctx context.Context
cfunc context.CancelFunc
sCh chan struct{}
ch chan []events.Event
ack bool
running bool
id int
}
func NewBase(ctx context.Context, buf int, ack bool) *Base {
ctx, cfunc := context.WithCancel(ctx)
b := &Base{
ctx: ctx,
cfunc: cfunc,
sCh: make(chan struct{}),
ch: make(chan []events.Event, buf),
ack: ack,
running: !ack, // assume the implementation will start a routine asap
}
if b.ack {
go b.cleanup()
}
return b
}
func (b *Base) cleanup() {
<-b.ctx.Done()
b.Halt()
}
// Ack returns whether or not this is a synchronous/async subscriber.
func (b *Base) Ack() bool {
return b.ack
}
// Pause the current subscriber will not receive events from the channel.
func (b *Base) Pause() {
if b.running {
b.running = false
close(b.sCh)
}
}
// Resume unpauzes the subscriber.
func (b *Base) Resume() {
if !b.running {
b.sCh = make(chan struct{})
b.running = true
}
}
func (b Base) isRunning() bool {
return b.running
}
// C returns the event channel for optional subscribers.
func (b *Base) C() chan<- []events.Event {
return b.ch
}
// Closed indicates to the broker that the subscriber is closed for business.
func (b *Base) Closed() <-chan struct{} {
return b.ctx.Done()
}
// Skip lets the broker know that the subscriber is not receiving events.
func (b *Base) Skip() <-chan struct{} {
return b.sCh
}
// Halt is called by the broker on shutdown, this closes the open channels.
func (b *Base) Halt() {
// This is a hacky fix, but the fact remains: closing this channel occasionally causes a data race
// we're using select, hoist the channel assignment, but the fact is: select is not atomic
// allow attempted writes during shutdown, unless this is an acking sub, with a potential blocking channel
defer func() {
if !b.ack {
time.Sleep(20 * time.Millisecond) // add sleep to avoid race (send on closed channel), 20ms should be plenty
}
close(b.ch) // close the event channel after pause (skip) and cfunc (closed) are toggled
}()
b.cfunc() // cancels the subscriber context, which breaks the loop
b.Pause() // close the skip channel
}
// SetID set the ID (exposed only to broker).
func (b *Base) SetID(id int) {
b.id = id
}
// ID returns the subscriber ID.
func (b *Base) ID() int {
return b.id
}