-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopic.go
151 lines (125 loc) · 3.11 KB
/
topic.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
// This file is part of the Smart Home
// Program complex distribution https://github.com/e154/smart-home
// Copyright (C) 2024, Filippov Alex
//
// This library is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// This library 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
// Library General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library. If not, see
// <https://www.gnu.org/licenses/>.
package bus
import (
"fmt"
"reflect"
"sync"
"time"
)
const queueSize = 1024
type Topic struct {
name string
*Statistic
sync.RWMutex
handlers []*handler
lastMsg []reflect.Value
}
func NewTopic(name string) *Topic {
return &Topic{
name: name,
handlers: make([]*handler, 0),
Statistic: NewStatistic(),
}
}
func (t *Topic) Publish(args ...interface{}) {
t.RLock()
defer t.RUnlock()
if len(t.handlers) == 0 {
return
}
rArgs := buildHandlerArgs(append([]interface{}{t.name}, args...))
t.lastMsg = rArgs
for _, h := range t.handlers {
t.rps.Inc()
h.queue <- rArgs
}
}
func (t *Topic) Subscribe(fn interface{}, options ...interface{}) error {
if reflect.TypeOf(fn).Kind() != reflect.Func {
return fmt.Errorf("%s is not a reflect.Func", reflect.TypeOf(fn))
}
h := &handler{
callback: reflect.ValueOf(fn),
queue: make(chan []reflect.Value, queueSize),
}
t.Lock()
t.handlers = append(t.handlers, h)
t.Unlock()
go func(h *handler) {
var startTime time.Time
for args := range h.queue {
go func(args []reflect.Value) {
startTime = time.Now()
h.callback.Call(args)
t.setTime(time.Since(startTime))
}(args)
}
}(h)
if len(options) > 0 {
if retain, ok := options[0].(bool); ok && !retain {
return nil
}
}
t.RLock()
// sand last message value
if t.lastMsg != nil {
go h.callback.Call(t.lastMsg)
}
t.RUnlock()
return nil
}
func (t *Topic) Unsubscribe(fn interface{}) (empty bool, err error) {
t.Lock()
defer t.Unlock()
rv := reflect.ValueOf(fn)
var indexesToDelete []int
for i, h := range t.handlers {
if h.callback == rv || h.callback.Pointer() == rv.Pointer() {
indexesToDelete = append(indexesToDelete, i)
}
}
for i := len(indexesToDelete) - 1; i >= 0; i-- {
index := indexesToDelete[i]
close(t.handlers[index].queue)
t.handlers = append(t.handlers[:index], t.handlers[index+1:]...)
}
empty = len(t.handlers) == 0
return
}
func (t *Topic) Close() {
t.Lock()
defer t.Unlock()
for _, h := range t.handlers {
close(h.queue)
}
t.handlers = make([]*handler, 0)
t.rps.Stop()
}
func (t *Topic) Stat() *StatItem {
t.RLock()
defer t.RUnlock()
return &StatItem{
Topic: t.name,
Subscribers: len(t.handlers),
Min: t.min.Load(),
Max: t.max.Load(),
Avg: t.avg.Load(),
Rps: t.rps.Value(),
}
}