-
Notifications
You must be signed in to change notification settings - Fork 107
/
subscription.go
152 lines (137 loc) · 3.29 KB
/
subscription.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
package stomp
import (
"fmt"
"log"
"sync"
"github.com/go-stomp/stomp/frame"
)
// The Subscription type represents a client subscription to
// a destination. The subscription is created by calling Conn.Subscribe.
//
// Once a client has subscribed, it can receive messages from the C channel.
type Subscription struct {
C chan *Message
id string
destination string
conn *Conn
ackMode AckMode
completed bool
completedMutex *sync.Mutex
}
// BUG(jpj): If the client does not read messages from the Subscription.C
// channel quickly enough, the client will stop reading messages from the
// server.
// Identification for this subscription. Unique among
// all subscriptions for the same Client.
func (s *Subscription) Id() string {
return s.id
}
// Destination for which the subscription applies.
func (s *Subscription) Destination() string {
return s.destination
}
// AckMode returns the Acknowledgement mode specified when the
// subscription was created.
func (s *Subscription) AckMode() AckMode {
return s.ackMode
}
// Active returns whether the subscription is still active.
// Returns false if the subscription has been unsubscribed.
func (s *Subscription) Active() bool {
return !s.completed
}
// Unsubscribes and closes the channel C.
func (s *Subscription) Unsubscribe(opts ...func(*frame.Frame) error) error {
if s.completed {
return ErrCompletedSubscription
}
f := frame.New(frame.UNSUBSCRIBE, frame.Id, s.id)
for _, opt := range opts {
if opt == nil {
return ErrNilOption
}
err := opt(f)
if err != nil {
return err
}
}
s.conn.sendFrame(f)
s.completedMutex.Lock()
if !s.completed {
s.completed = true
close(s.C)
}
s.completedMutex.Unlock()
return nil
}
// Read a message from the subscription. This is a convenience
// method: many callers will prefer to read from the channel C
// directly.
func (s *Subscription) Read() (*Message, error) {
if s.completed {
return nil, ErrCompletedSubscription
}
msg, ok := <-s.C
if !ok {
return nil, ErrCompletedSubscription
}
if msg.Err != nil {
return nil, msg.Err
}
return msg, nil
}
func (s *Subscription) readLoop(ch chan *frame.Frame) {
for {
if s.completed {
return
}
f, ok := <-ch
if !ok {
return
}
if f.Command == frame.MESSAGE {
destination := f.Header.Get(frame.Destination)
contentType := f.Header.Get(frame.ContentType)
msg := &Message{
Destination: destination,
ContentType: contentType,
Conn: s.conn,
Subscription: s,
Header: f.Header,
Body: f.Body,
}
s.completedMutex.Lock()
if !s.completed {
s.C <- msg
}
s.completedMutex.Unlock()
} else if f.Command == frame.ERROR {
message, _ := f.Header.Contains(frame.Message)
text := fmt.Sprintf("Subscription %s: %s: ERROR message:%s",
s.id,
s.destination,
message)
log.Println(text)
contentType := f.Header.Get(frame.ContentType)
msg := &Message{
Err: &Error{
Message: f.Header.Get(frame.Message),
Frame: f,
},
ContentType: contentType,
Conn: s.conn,
Subscription: s,
Header: f.Header,
Body: f.Body,
}
s.completedMutex.Lock()
if !s.completed {
s.completed = true
s.C <- msg
close(s.C)
}
s.completedMutex.Unlock()
return
}
}
}