-
Notifications
You must be signed in to change notification settings - Fork 0
/
go-eventpublisherfunk.go
187 lines (163 loc) · 4.38 KB
/
go-eventpublisherfunk.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package eventpublisherfunk
import (
"context"
"fmt"
"sync"
"go.uber.org/atomic"
)
const (
// DefaultBusCapacity = 128
// DefaultDataChannelCapacity describe the cap of data channel
DefaultDataChannelCapacity = 65535
// DefaultErrorChannelCapacity describe the cap of error channel
DefaultErrorChannelCapacity = 1024
)
// EventData describe the standard event data struct
type EventData struct {
ID string
Data interface{}
Topic string
}
//HandleFunc describe the func that used for handle EventData with special Topic
type HandleFunc func(EventData) (eventId string, err error)
// ErrorhandlFunc describe the error handler if error occurred,
type ErrorHandleFunc func(error)
type DataChannel chan EventData
type ErrorChannel chan error
type Publisher interface {
PublishEvent(EventData) (string, error)
PublishEventAsync(EventData) (string, error)
RegisterTopicHandleFunc(string, HandleFunc) error
RegisterErrorHandleFunc(ErrorHandleFunc) error
CloseAsyncPublisher() error
DumpBus() *Bus
}
type Bus struct {
handles *sync.Map
errhandles ErrorHandleFunc
}
type publisher struct {
Bus *Bus
DataCh DataChannel
ErrorCh ErrorChannel
async *atomic.Bool
ctx context.Context
cancel context.CancelFunc
workerWG *sync.WaitGroup
bufferWG *sync.WaitGroup
}
// NewPublisher creates a new Publisher instance,where could be used through Publish.PublishEvent()
// No need to Close()
func NewPublisher() Publisher {
p := new(publisher)
p.Bus = &Bus{
handles: new(sync.Map),
errhandles: nil,
}
return p
}
// NewAsyncPublish creates a new Publisher instance with publishasyncWorker, which has many goroutines to process EventData.
// Need close the instance via CloseAsyncPublisher(), see examples in examples directory
func NewAsyncPublisher() Publisher {
ctx, cancel := context.WithCancel(context.Background())
p := &publisher{
Bus: &Bus{
handles: new(sync.Map),
},
DataCh: make(DataChannel, DefaultDataChannelCapacity),
ErrorCh: make(ErrorChannel, DefaultErrorChannelCapacity),
async: atomic.NewBool(true),
ctx: ctx,
cancel: cancel,
workerWG: &sync.WaitGroup{},
bufferWG: &sync.WaitGroup{},
}
p.publisherasyncWorker()
return p
}
// CloseAsyncPublisher() close the instance of Publisher
func (p *publisher) CloseAsyncPublisher() error {
select {
case <-p.ctx.Done():
default:
p.async.Swap(false)
p.cancel()
p.workerWG.Wait()
}
return nil
}
// DumpBus dumps the current Bus struct
func (p *publisher) DumpBus() *Bus {
return p.Bus
}
// RegisterTopicHandleFunc register consumer of topic, which may do many things sync or async.
// users should privide (f HandleFunc) first
func (p *publisher) RegisterTopicHandleFunc(topic string, f HandleFunc) error {
p.Bus.handles.Store(topic, f)
return nil
}
// RegisterErrorHandleFunc register consumer of errors which may renturn by HandleFunc
func (p *publisher) RegisterErrorHandleFunc(f ErrorHandleFunc) error {
p.Bus.errhandles = f
return nil
}
// PublishEvent publish a DataEvent into Publish instance, the DataEvent data may be process by HandleFunc
func (p *publisher) PublishEvent(d EventData) (string, error) {
return p.publish(d)
}
// PublishEventAsync publish a DataEvent into Publish instance, the Publisher instance must be initialized through NewAsyncPublisher()
func (p *publisher) PublishEventAsync(d EventData) (string, error) {
if !p.async.Load() {
return "", fmt.Errorf("DefaultError,need async")
}
select { //
case <-p.ctx.Done():
return "", fmt.Errorf("DefaultError")
default:
}
p.bufferWG.Add(1)
defer p.bufferWG.Done()
p.DataCh <- d
return d.ID, nil
}
func (p *publisher) publish(d EventData) (string, error) {
t := d.Topic
if t != "" {
if f, ok := p.Bus.handles.Load(t); ok {
return f.(HandleFunc)(d)
}
}
return "", fmt.Errorf("DefaultError,need redefine")
}
func (p *publisher) publisherasyncWorker() error {
p.workerWG.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
for d := range p.DataCh {
_, err := p.publish(d)
if err != nil {
p.ErrorCh <- err
}
}
}(p.workerWG)
p.workerWG.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
for err := range p.ErrorCh {
if p.Bus.errhandles != nil {
p.Bus.errhandles(err)
}
}
}(p.workerWG)
p.workerWG.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
<-p.ctx.Done()
{
p.bufferWG.Wait()
close(p.DataCh)
close(p.ErrorCh)
}
}(p.workerWG)
return nil
}