forked from Azure/azure-service-bus-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_session.go
208 lines (177 loc) · 5.7 KB
/
message_session.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package servicebus
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/Azure/azure-amqp-common-go/v3/rpc"
"github.com/devigned/tab"
"github.com/Azure/go-amqp"
)
// MessageSession represents and allows for interaction with a Service Bus Session.
type MessageSession struct {
*Receiver
entity EntityManagementAddresser
mu sync.RWMutex
sessionID *string
lockExpiration time.Time
done chan struct {
}
cancel sync.Once
}
func newMessageSession(r *Receiver, entity EntityManagementAddresser, sessionID *string) (retval *MessageSession, _ error) {
retval = &MessageSession{
Receiver: r,
entity: entity,
sessionID: sessionID,
lockExpiration: time.Now(),
done: make(chan struct{}),
}
return
}
// Close communicates that Handler receiving messages should no longer continue to be executed. This can happen when:
// - A Handler recognizes that no further messages will come to this session.
// - A Handler has given up on receiving more messages before a session. Future messages should be delegated to the next
// available session client.
func (ms *MessageSession) Close() {
ms.cancel.Do(func() {
close(ms.done)
})
}
// LockedUntil fetches the moment in time when the Session lock held by this Receiver will expire.
func (ms *MessageSession) LockedUntil() time.Time {
ms.mu.RLock()
defer ms.mu.RUnlock()
return ms.lockExpiration
}
// RenewLock requests that the Service Bus Server renews this client's lock on an existing Session.
func (ms *MessageSession) RenewLock(ctx context.Context) error {
ms.mu.Lock()
defer ms.mu.Unlock()
link, err := rpc.NewLinkWithSession(ms.Receiver.session.Session, ms.entity.ManagementPath())
if err != nil {
tab.For(ctx).Error(err)
return err
}
msg := &amqp.Message{
ApplicationProperties: map[string]interface{}{
"operation": "com.microsoft:renew-session-lock",
},
Value: map[string]interface{}{
"session-id": ms.SessionID(),
},
}
if deadline, ok := ctx.Deadline(); ok {
msg.ApplicationProperties["com.microsoft:server-timeout"] = uint(time.Until(deadline) / time.Millisecond)
}
resp, err := link.RetryableRPC(ctx, 5, 5*time.Second, msg)
if err != nil {
tab.For(ctx).Error(err)
return err
}
if rawMessageValue, ok := resp.Message.Value.(map[string]interface{}); ok {
if rawExpiration, ok := rawMessageValue["expiration"]; ok {
if ms.lockExpiration, ok = rawExpiration.(time.Time); ok {
return nil
}
return errors.New("\"expiration\" not of expected type time.Time")
}
return errors.New("missing expected property \"expiration\" in \"Value\"")
}
return errors.New("value not of expected type map[string]interface{}")
}
// ListSessions will list all of the sessions available
func (ms *MessageSession) ListSessions(ctx context.Context) ([]byte, error) {
link, err := rpc.NewLink(ms.Receiver.client, ms.entity.ManagementPath())
if err != nil {
tab.For(ctx).Error(err)
return nil, err
}
msg := &amqp.Message{
ApplicationProperties: map[string]interface{}{
"operation": "com.microsoft:get-message-sessions",
},
Value: map[string]interface{}{
"last-updated-time": time.Now().UTC().Add(-30 * time.Minute),
"skip": int32(0),
"top": int32(100),
},
}
rsp, err := link.RetryableRPC(ctx, 5, 5*time.Second, msg)
if err != nil {
tab.For(ctx).Error(err)
return nil, err
}
if rsp.Code != 200 {
err := fmt.Errorf("amqp error (%d): %q", rsp.Code, rsp.Description)
tab.For(ctx).Error(err)
return nil, err
}
return rsp.Message.Data[0], nil
}
// SetState updates the current State associated with this Session.
func (ms *MessageSession) SetState(ctx context.Context, state []byte) error {
link, err := rpc.NewLinkWithSession(ms.Receiver.session.Session, ms.entity.ManagementPath())
if err != nil {
return err
}
msg := &amqp.Message{
ApplicationProperties: map[string]interface{}{
"operation": "com.microsoft:set-session-state",
},
Properties: &amqp.MessageProperties{
GroupID: *ms.SessionID(),
},
Value: map[string]interface{}{
"session-id": ms.SessionID(),
"session-state": state,
},
}
rsp, err := link.RetryableRPC(ctx, 5, 5*time.Second, msg)
if err != nil {
return err
}
if rsp.Code != 200 {
return fmt.Errorf("amqp error (%d): %q", rsp.Code, rsp.Description)
}
return nil
}
// State retrieves the current State associated with this Session.
// https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-amqp-request-response#get-session-state
func (ms *MessageSession) State(ctx context.Context) ([]byte, error) {
const sessionStateField = "session-state"
link, err := rpc.NewLinkWithSession(ms.Receiver.session.Session, ms.entity.ManagementPath())
if err != nil {
return []byte{}, err
}
msg := &amqp.Message{
ApplicationProperties: map[string]interface{}{
"operation": "com.microsoft:get-session-state",
},
Value: map[string]interface{}{
"session-id": ms.SessionID(),
},
}
rsp, err := link.RetryableRPC(ctx, 5, 5*time.Second, msg)
if err != nil {
return []byte{}, err
}
if rsp.Code != 200 {
return []byte{}, fmt.Errorf("amqp error (%d): %q", rsp.Code, rsp.Description)
}
if val, ok := rsp.Message.Value.(map[string]interface{}); ok {
if rawState, ok := val[sessionStateField]; ok {
if state, ok := rawState.([]byte); ok || rawState == nil {
return state, nil
}
return nil, newErrIncorrectType(sessionStateField, []byte{}, rawState)
}
return nil, ErrMissingField(sessionStateField)
}
return nil, newErrIncorrectType("value", map[string]interface{}{}, rsp.Message.Value)
}
// SessionID gets the unique identifier of the session being interacted with by this MessageSession.
func (ms *MessageSession) SessionID() *string {
return ms.sessionID
}