-
Notifications
You must be signed in to change notification settings - Fork 19
/
session.ts
436 lines (394 loc) · 16.9 KB
/
session.ts
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the Apache License. See License in the project root for license information.
import * as log from "./log";
import { Connection } from "./connection";
import { Receiver, ReceiverOptions } from "./receiver";
import { Sender, SenderOptions } from "./sender";
import {
SenderEvents, ReceiverEvents, SessionEvents, AmqpError, Session as RheaSession,
EventContext as RheaEventContext
} from "rhea";
import { Func, EmitParameters, emitEvent } from "./util/utils";
import { OnAmqpEvent } from "./eventContext";
import { Entity } from "./entity";
import { OperationTimeoutError } from "./operationTimeoutError";
/**
* Describes the event listeners that can be added to the Session.
* @interface Session
*/
export declare interface Session {
on(event: SessionEvents, listener: OnAmqpEvent): this;
}
/**
* Describes the session that wraps the rhea session.
* @class Session
*/
export class Session extends Entity {
private _session: RheaSession;
private _connection: Connection;
constructor(connection: Connection, session: RheaSession) {
super();
this._connection = connection;
this._session = session;
this._initializeEventListeners();
}
/**
* @property {Connection} connection The underlying AMQP connection.
* @readonly
*/
get connection(): Connection {
return this._connection;
}
get outgoing(): any {
return (this._session as any).outgoing;
}
get error(): AmqpError | Error | undefined {
return this._session.error;
}
/**
* Determines whether the session and the underlying connection is open.
* @returns {boolean} result `true` - is open; `false` otherwise.
*/
isOpen(): boolean {
let result = false;
if (this._connection.isOpen() && this._session.is_open()) {
result = true;
}
return result;
}
/**
* Determines whether the close from the peer is a response to a locally initiated close request.
* @returns {boolean} `true` if close was locally initiated, `false` otherwise.
*/
isClosed(): boolean {
return this._session.is_closed();
}
/**
* Determines whether both local and remote endpoint for just the session itself are closed.
* Within the "session_close" event handler, if this method returns `false` it means that
* the local end is still open. It can be useful to determine whether the close
* was initiated locally under such circumstances.
*
* @returns {boolean} `true` - closed, `false` otherwise.
*/
isItselfClosed(): boolean {
return this._session.is_itself_closed();
}
/**
* Removes the underlying amqp session from the internal map in rhea.
* Also removes all the event handlers added in the rhea-promise library on the session.
*/
remove(): void {
if (this._session) {
// Remove our listeners and listeners from rhea's 'session' object.
this.removeAllListeners();
this._session.removeAllListeners();
this._session.remove();
}
}
begin(): void {
if (this._session) {
this._session.begin();
}
}
/**
* Closes the underlying amqp session in rhea if open. Also removes all the event
* handlers added in the rhea-promise library on the session
* @return {Promise<void>} Promise<void>
* - **Resolves** the promise when rhea emits the "session_close" event.
* - **Rejects** the promise with an AmqpError when rhea emits the "session_error" event while trying
* to close an amqp session.
*/
close(): Promise<void> {
this.removeAllListeners();
return new Promise<void>((resolve, reject) => {
log.error("[%s] The session is open ? -> %s", this.connection.id, this.isOpen());
if (this.isOpen()) {
let onError: Func<RheaEventContext, void>;
let onClose: Func<RheaEventContext, void>;
let waitTimer: any;
const removeListeners = () => {
clearTimeout(waitTimer);
this.actionInitiated--;
this._session.removeListener(SessionEvents.sessionError, onError);
this._session.removeListener(SessionEvents.sessionClose, onClose);
};
onClose = (context: RheaEventContext) => {
removeListeners();
log.session("[%s] Resolving the promise as the amqp session has been closed.",
this.connection.id);
return resolve();
};
onError = (context: RheaEventContext) => {
removeListeners();
log.error("[%s] Error occurred while closing amqp session.",
this.connection.id, context.session!.error);
reject(context.session!.error);
};
const actionAfterTimeout = () => {
removeListeners();
const msg: string = `Unable to close the amqp session due to operation timeout.`;
log.error("[%s] %s", this.connection.id, msg);
reject(new OperationTimeoutError(msg));
};
// listeners that we add for completing the operation are added directly to rhea's objects.
this._session.once(SessionEvents.sessionClose, onClose);
this._session.once(SessionEvents.sessionError, onError);
log.session("[%s] Calling session.close()", this.connection.id);
waitTimer = setTimeout(actionAfterTimeout, this.connection.options!.operationTimeoutInSeconds! * 1000);
this._session.close();
this.actionInitiated++;
} else {
return resolve();
}
});
}
/**
* Creates an amqp receiver on this session.
* @param {Session} session The amqp session object on which the receiver link needs to be established.
* @param {ReceiverOptions} [options] Options that can be provided while creating an amqp receiver.
* @return {Promise<Receiver>} Promise<Receiver>
* - **Resolves** the promise with the Receiver object when rhea emits the "receiver_open" event.
* - **Rejects** the promise with an AmqpError when rhea emits the "receiver_close" event while trying
* to create an amqp receiver or the operation timeout occurs.
*/
createReceiver(options?: ReceiverOptions): Promise<Receiver> {
return new Promise((resolve, reject) => {
if (options &&
((options.onMessage && !options.onError) || (options.onError && !options.onMessage))) {
if (options.credit_window !== 0) {
// - If the 'onMessage' handler is not provided and the credit_window is not set to 0,
// then messages may be lost between the receiver link getting created and the message
// handler being attached.
// - It can be possible for a service to initially accept the link attach, which would
// cause the promise to resolve. However, moments later the service may send a detach
// due to some internal or configuration issue. If no error handler is attached, then
// the error may fall through.
// - Hence it is advised to either provide both 'onMessage' and 'onError' handlers, or
// please set the credit_window to `0`, if you want to provide only the 'onError' handler.
return reject(new Error("Either provide both 'onMessage' and 'onError' handlers, or pl" +
"ease set the credit_window to 0, if you want to provide only the 'onError' " +
"handler. This ensures no messages are lost between the receiver getting created " +
" and the 'onMessage' handler being added."));
}
}
// Register session handlers for session_error and session_close if provided.
// listeners provided by the user in the options object should be added
// to our (rhea-promise) object.
if (options && options.onSessionError) {
this.on(SessionEvents.sessionError, options.onSessionError);
log.session("[%s] Added event handler for event '%s' on rhea-promise 'session', " +
"while creating the 'receiver'.", this.connection.id, SessionEvents.sessionError);
}
if (options && options.onSessionClose) {
this.on(SessionEvents.sessionClose, options.onSessionClose);
log.session("[%s] Added event handler for event '%s' on rhea-promise 'session', " +
" while creating the 'receiver'.", this.connection.id, SessionEvents.sessionClose);
}
const rheaReceiver = this._session.attach_receiver(options);
const receiver = new Receiver(this, rheaReceiver, options);
receiver.actionInitiated++;
let onOpen: Func<RheaEventContext, void>;
let onClose: Func<RheaEventContext, void>;
let waitTimer: any;
if (options && options.onMessage) {
receiver.on(ReceiverEvents.message, options.onMessage);
log.receiver("[%s] Added event handler for event '%s' on rhea-promise 'receiver'.",
this.connection.id, ReceiverEvents.message);
}
if (options && options.onError) {
receiver.on(ReceiverEvents.receiverError, options.onError);
log.receiver("[%s] Added event handler for event '%s' on rhea-promise 'receiver'.",
this.connection.id, ReceiverEvents.receiverError);
}
if (options && options.onClose) {
receiver.on(ReceiverEvents.receiverClose, options.onClose);
log.receiver("[%s] Added event handler for event '%s' on rhea-promise 'receiver'.",
this.connection.id, ReceiverEvents.receiverClose);
}
if (options && options.onSettled) {
receiver.on(ReceiverEvents.settled, options.onSettled);
log.receiver("[%s] Added event handler for event '%s' on rhea-promise 'receiver'.",
this.connection.id, ReceiverEvents.settled);
}
const removeListeners = () => {
clearTimeout(waitTimer);
receiver.actionInitiated--;
rheaReceiver.removeListener(ReceiverEvents.receiverOpen, onOpen);
rheaReceiver.removeListener(ReceiverEvents.receiverClose, onClose);
};
onOpen = (context: RheaEventContext) => {
removeListeners();
log.receiver("[%s] Resolving the promise with amqp receiver '%s'.",
this.connection.id, receiver.name);
return resolve(receiver);
};
onClose = (context: RheaEventContext) => {
removeListeners();
log.error("[%s] Error occurred while creating a receiver over amqp connection: %O.",
this.connection.id, context.receiver!.error);
return reject(context.receiver!.error);
};
const actionAfterTimeout = () => {
removeListeners();
const msg: string = `Unable to create the amqp receiver ${receiver.name} due to ` +
`operation timeout.`;
log.error("[%s] %s", this.connection.id, msg);
return reject(new OperationTimeoutError(msg));
};
// listeners that we add for completing the operation are added directly to rhea's objects.
rheaReceiver.once(ReceiverEvents.receiverOpen, onOpen);
rheaReceiver.once(ReceiverEvents.receiverClose, onClose);
waitTimer = setTimeout(actionAfterTimeout, this.connection.options!.operationTimeoutInSeconds! * 1000);
});
}
/**
* Creates an amqp sender on this session.
* @param {SenderOptions} [options] Options that can be provided while creating an amqp sender.
* @return {Promise<Sender>} Promise<Sender>
* - **Resolves** the promise with the Sender object when rhea emits the "sender_open" event.
* - **Rejects** the promise with an AmqpError when rhea emits the "sender_close" event while trying
* to create an amqp sender or the operation timeout occurs.
*/
createSender(options?: SenderOptions): Promise<Sender> {
return new Promise((resolve, reject) => {
// Register session handlers for session_error and session_close if provided.
if (options && options.onSessionError) {
this.on(SessionEvents.sessionError, options.onSessionError);
log.session("[%s] Added event handler for event '%s' on rhea-promise 'session', " +
"while creating the sender.", this.connection.id, SessionEvents.sessionError);
}
if (options && options.onSessionClose) {
this.on(SessionEvents.sessionClose, options.onSessionClose);
log.session("[%s] Added event handler for event '%s' on rhea-promise 'session', " +
"while creating the sender.", this.connection.id, SessionEvents.sessionClose);
}
const rheaSender = this._session.attach_sender(options);
const sender = new Sender(this, rheaSender, options);
sender.actionInitiated++;
let onSendable: Func<RheaEventContext, void>;
let onClose: Func<RheaEventContext, void>;
let waitTimer: any;
// listeners provided by the user in the options object should be added
// to our (rhea-promise) object.
if (options) {
if (options.onError) {
sender.on(SenderEvents.senderError, options.onError);
}
if (options.onClose) {
sender.on(SenderEvents.senderClose, options.onClose);
}
if (options.onAccepted) {
sender.on(SenderEvents.accepted, options.onAccepted);
}
if (options.onRejected) {
sender.on(SenderEvents.rejected, options.onRejected);
}
if (options.onReleased) {
sender.on(SenderEvents.released, options.onReleased);
}
if (options.onModified) {
sender.on(SenderEvents.modified, options.onModified);
}
}
const removeListeners = () => {
clearTimeout(waitTimer);
sender.actionInitiated--;
rheaSender.removeListener(SenderEvents.senderOpen, onSendable);
rheaSender.removeListener(SenderEvents.senderClose, onClose);
};
onSendable = (context: RheaEventContext) => {
removeListeners();
log.sender("[%s] Resolving the promise with amqp sender '%s'.",
this.connection.id, sender.name);
return resolve(sender);
};
onClose = (context: RheaEventContext) => {
removeListeners();
log.error("[%s] Error occurred while creating a sender over amqp connection: %O.",
this.connection.id, context.sender!.error);
return reject(context.sender!.error);
};
const actionAfterTimeout = () => {
removeListeners();
const msg: string = `Unable to create the amqp sender ${sender.name} due to ` +
`operation timeout.`;
log.error("[%s] %s", this.connection.id, msg);
return reject(new OperationTimeoutError(msg));
};
// listeners that we add for completing the operation are added directly to rhea's objects.
rheaSender.once(SenderEvents.sendable, onSendable);
rheaSender.once(SenderEvents.senderClose, onClose);
waitTimer = setTimeout(actionAfterTimeout, this.connection.options!.operationTimeoutInSeconds! * 1000);
});
}
/**
* Adds event listeners for the possible events that can occur on the session object and
* re-emits the same event back with the received arguments from rhea's event emitter.
* @private
* @returns {void} void
*/
private _initializeEventListeners(): void {
for (const eventName in SessionEvents) {
this._session.on(SessionEvents[eventName],
(context) => {
const params: EmitParameters = {
rheaContext: context,
emitter: this,
eventName: SessionEvents[eventName],
emitterType: "session",
connectionId: this.connection.id
};
emitEvent(params);
});
}
// Add event handlers for *_error and *_close events that can be propogated to the session
// object, if they are not handled at their level. * denotes - Sender and Receiver.
// Sender
this._session.on(SenderEvents.senderError, (context) => {
const params: EmitParameters = {
rheaContext: context,
emitter: this,
eventName: SenderEvents.senderError,
emitterType: "session",
connectionId: this.connection.id
};
emitEvent(params);
});
this._session.on(SenderEvents.senderClose, (context) => {
const params: EmitParameters = {
rheaContext: context,
emitter: this,
eventName: SenderEvents.senderClose,
emitterType: "session",
connectionId: this.connection.id
};
emitEvent(params);
});
// Receiver
this._session.on(ReceiverEvents.receiverError, (context) => {
const params: EmitParameters = {
rheaContext: context,
emitter: this,
eventName: ReceiverEvents.receiverError,
emitterType: "session",
connectionId: this.connection.id
};
emitEvent(params);
});
this._session.on(ReceiverEvents.receiverClose, (context) => {
const params: EmitParameters = {
rheaContext: context,
emitter: this,
eventName: ReceiverEvents.receiverClose,
emitterType: "session",
connectionId: this.connection.id
};
emitEvent(params);
});
if (typeof this._session.eventNames === "function") {
log.eventHandler("[%s] rhea-promise 'session' object is listening for events: %o " +
"emitted by rhea's 'session' object.", this.connection.id, this._session.eventNames());
}
}
}