-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbroker.go
216 lines (181 loc) · 4.38 KB
/
broker.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Package chanbroker a Broker for goroutine, is simliar to kafka
//
// chanbroker has three types of goroutine: Producer, Consumer(Subscriber), Broker
package chanbroker
import (
"container/list"
"errors"
"time"
)
// Content as interface
type Content interface{}
// Subscriber as channel for Content
type Subscriber chan Content
// chanbroker desc
type Broker struct {
regSub chan Subscriber
unRegSub chan Subscriber
contents chan Content
stop chan bool
subscribers map[Subscriber]*list.List
timeout time.Duration
cachenum uint
timerChan <-chan time.Time
}
// ErrBrokerExit represent broker goroutine exit
var ErrBrokerExit error = errors.New("chanbroker exit")
// ErrPublishTimeOut represent publish context timeout
var ErrPublishTimeOut error = errors.New("chanbroker Pulish Time out")
// ErrRegTimeOut represent Subscriber registration timeout
var ErrRegTimeOut error = errors.New("chanbroker Reg Time out")
// ErrStopBrokerTimeOut represent stop broker goroutine timeout
var ErrStopBrokerTimeOut error = errors.New("chanbroker Stop Broker Time out")
// NewBroker create a new broker
func NewBroker(timeout time.Duration) *Broker {
Broker := new(Broker)
Broker.regSub = make(chan Subscriber)
Broker.unRegSub = make(chan Subscriber)
Broker.contents = make(chan Content, 16)
Broker.stop = make(chan bool, 1)
Broker.subscribers = make(map[Subscriber]*list.List)
Broker.timeout = timeout
Broker.cachenum = 0
Broker.timerChan = nil
Broker.run()
return Broker
}
func (broker *Broker) onContentPush(content Content) {
for sub, clist := range broker.subscribers {
loop := true
for next := clist.Front(); next != nil && loop == true; {
cur := next
next = cur.Next()
select {
case sub <- cur.Value:
if broker.cachenum > 0 {
broker.cachenum--
}
clist.Remove(cur)
default:
loop = false
}
}
len := clist.Len()
if len == 0 {
select {
case sub <- content:
default:
clist.PushBack(content)
broker.cachenum++
}
} else {
clist.PushBack(content)
broker.cachenum++
}
}
if broker.cachenum > 0 && broker.timerChan == nil {
timer := time.NewTimer(broker.timeout)
broker.timerChan = timer.C
}
}
func (broker *Broker) onTimerPush() {
for sub, clist := range broker.subscribers {
loop := true
for next := clist.Front(); next != nil && loop == true; {
cur := next
next = cur.Next()
select {
case sub <- cur.Value:
if broker.cachenum > 0 {
broker.cachenum--
}
clist.Remove(cur)
default:
loop = false
}
}
}
if broker.cachenum > 0 {
timer := time.NewTimer(broker.timeout)
broker.timerChan = timer.C
} else {
broker.timerChan = nil
}
}
func (broker *Broker) run() {
go func() { // Broker Goroutine
for {
select {
case content := <-broker.contents:
broker.onContentPush(content)
case <-broker.timerChan:
broker.onTimerPush()
case sub := <-broker.regSub:
clist := list.New()
broker.subscribers[sub] = clist
case sub := <-broker.unRegSub:
_, ok := broker.subscribers[sub]
if ok {
delete(broker.subscribers, sub)
close(sub)
}
case _, ok := <-broker.stop:
if ok == true {
close(broker.stop)
} else {
if broker.cachenum == 0 {
for sub := range broker.subscribers {
delete(broker.subscribers, sub)
close(sub)
}
return
}
}
broker.onTimerPush()
for sub, clist := range broker.subscribers {
if clist.Len() == 0 {
delete(broker.subscribers, sub)
close(sub)
}
}
}
}
}()
}
// RegSubscriber register subscriber
func (broker *Broker) RegSubscriber(size uint) (Subscriber, error) {
sub := make(Subscriber, size)
select {
case <-time.After(broker.timeout):
return nil, ErrRegTimeOut
case broker.regSub <- sub:
return sub, nil
}
}
// UnRegSubscriber unregister subscriber
func (broker *Broker) UnRegSubscriber(sub Subscriber) {
select {
case <-time.After(broker.timeout):
return
case broker.unRegSub <- sub:
return
}
}
// StopBroker stop broker goroutine
func (broker *Broker) StopBroker() error {
select {
case broker.stop <- true:
return nil
case <-time.After(broker.timeout):
return ErrStopBrokerTimeOut
}
}
// PubContent publish content
func (broker *Broker) PubContent(c Content) error {
select {
case <-time.After(broker.timeout):
return ErrPublishTimeOut
case broker.contents <- c:
return nil
}
}