@@ -8,11 +8,11 @@ import (
8
8
9
9
type PubSub struct {
10
10
sync.RWMutex
11
- subscriptions map [string ]* Set // topic -> subscriptions
11
+ subscriptions map [uint64 ]* Set // topic -> subscriptions
12
12
}
13
13
14
14
type subscription struct {
15
- top string
15
+ top uint64
16
16
ttl time.Duration
17
17
c chan interface {}
18
18
}
@@ -33,24 +33,24 @@ func (s *subscription) Listen() (interface{}, error) {
33
33
34
34
func NewPubSub () * PubSub {
35
35
return & PubSub {
36
- subscriptions : make (map [string ]* Set ),
36
+ subscriptions : make (map [uint64 ]* Set ),
37
37
}
38
38
}
39
39
40
40
const (
41
- subscriptionBuffSize = 50
41
+ subscriptionBuffSize = 500
42
42
)
43
43
44
44
// Publish 发布topic对应的item,监听topic的所有订阅者都将收到item
45
- func (ps * PubSub ) Publish (topic string , item interface {}) error {
45
+ func (ps * PubSub ) Publish (topic uint64 , item interface {}) error {
46
46
ps .RLock ()
47
47
defer ps .RUnlock ()
48
48
s , subscribed := ps .subscriptions [topic ]
49
49
if ! subscribed {
50
50
return errors .New ("no subscribers" )
51
51
}
52
52
for _ , sub := range s .ToArray () {
53
- c := sub .( * subscription ). c
53
+ c := sub .c
54
54
// Not enough room in buffer, continue in order to not block publisher
55
55
if len (c ) == subscriptionBuffSize {
56
56
continue
@@ -61,7 +61,7 @@ func (ps *PubSub) Publish(topic string, item interface{}) error {
61
61
}
62
62
63
63
// Subscribe 订阅事件,返回一个Listen接口
64
- func (ps * PubSub ) Subscribe (topic string , ttl time.Duration ) Subscription {
64
+ func (ps * PubSub ) Subscribe (topic uint64 , ttl time.Duration ) Subscription {
65
65
sub := & subscription {
66
66
top : topic ,
67
67
ttl : ttl ,
@@ -101,21 +101,21 @@ func (ps *PubSub) unSubscribe(sub *subscription) {
101
101
}
102
102
103
103
type Set struct {
104
- items map [interface {} ]struct {}
104
+ items map [* subscription ]struct {}
105
105
lock * sync.RWMutex
106
106
}
107
107
108
108
func NewSet () * Set {
109
- return & Set {lock : & sync.RWMutex {}, items : make (map [interface {} ]struct {})}
109
+ return & Set {lock : & sync.RWMutex {}, items : make (map [* subscription ]struct {})}
110
110
}
111
111
112
- func (s * Set ) Add (item interface {} ) {
112
+ func (s * Set ) Add (item * subscription ) {
113
113
s .lock .Lock ()
114
114
defer s .lock .Unlock ()
115
115
s .items [item ] = struct {}{}
116
116
}
117
117
118
- func (s * Set ) Exists (item interface {} ) bool {
118
+ func (s * Set ) Exists (item * subscription ) bool {
119
119
s .lock .RLock ()
120
120
defer s .lock .RUnlock ()
121
121
_ , exists := s .items [item ]
@@ -128,10 +128,10 @@ func (s *Set) Size() int {
128
128
return len (s .items )
129
129
}
130
130
131
- func (s * Set ) ToArray () []interface {} {
131
+ func (s * Set ) ToArray () []* subscription {
132
132
s .lock .RLock ()
133
133
defer s .lock .RUnlock ()
134
- a := make ([]interface {} , len (s .items ))
134
+ a := make ([]* subscription , len (s .items ))
135
135
i := 0
136
136
for item := range s .items {
137
137
a [i ] = item
@@ -143,10 +143,10 @@ func (s *Set) ToArray() []interface{} {
143
143
func (s * Set ) Clear () {
144
144
s .lock .Lock ()
145
145
defer s .lock .Unlock ()
146
- s .items = make (map [interface {} ]struct {})
146
+ s .items = make (map [* subscription ]struct {})
147
147
}
148
148
149
- func (s * Set ) Remove (item interface {} ) {
149
+ func (s * Set ) Remove (item * subscription ) {
150
150
s .lock .Lock ()
151
151
defer s .lock .Unlock ()
152
152
delete (s .items , item )
0 commit comments