forked from hprose/hprose-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
topic.go
76 lines (67 loc) · 1.95 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
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* rpc/topic.go *
* *
* hprose push topic for Go. *
* *
* LastModified: Sep 21, 2016 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/
package rpc
import (
"sync"
"time"
)
type topic struct {
sync.RWMutex
messages map[string]chan interface{}
heartbeat time.Duration
}
func newTopic(heartbeat time.Duration) *topic {
t := new(topic)
t.messages = make(map[string]chan interface{})
t.heartbeat = heartbeat
return t
}
func (t *topic) get(id string) (message chan interface{}) {
t.RLock()
message = t.messages[id]
t.RUnlock()
return
}
func (t *topic) put(id string, message chan interface{}) {
t.Lock()
t.messages[id] = message
t.Unlock()
}
func (t *topic) remove(id string) {
t.Lock()
delete(t.messages, id)
t.Unlock()
}
func (t *topic) idlist() (result []string) {
t.RLock()
result = make([]string, len(t.messages))
i := 0
for id := range t.messages {
result[i] = id
i++
}
t.RUnlock()
return
}
func (t *topic) exist(id string) (exist bool) {
t.RLock()
_, exist = t.messages[id]
t.RUnlock()
return
}