-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
48 lines (40 loc) · 1014 Bytes
/
group.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
package socketconsumer
type group struct {
// Unique id of the group
id string
// Registered consumers
consumers map[*Consumer]bool
}
// newGroup creates a new group
func newGroup(id string) *group {
return &group{
id: id,
consumers: make(map[*Consumer]bool),
}
}
// addConsumer adds a consumer to the group
func (g *group) addConsumer(c *Consumer) {
g.consumers[c] = true
}
// removeConsumer removes a consumer from the group
func (g *group) removeConsumer(c *Consumer) {
delete(g.consumers, c)
}
// Check group is empty
func (g *group) isEmpty() bool {
return len(g.consumers) == 0
}
// broadcast sends a message to all consumers in the group
func (g *group) broadcast(message *Message) {
for c := range g.consumers {
c.Send(message)
}
}
// broadcastExcept sends a message to all consumers in the group except the specified consumer
func (g *group) broadcastExcept(message *Message, except *Consumer) {
for c := range g.consumers {
if c != except {
c.Send(message)
}
}
}