-
Notifications
You must be signed in to change notification settings - Fork 12
/
amqp_connection.ts
201 lines (172 loc) · 5.14 KB
/
amqp_connection.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
import { AmqpChannel } from "./amqp_channel.ts";
import type { AmqpSocket } from "./amqp_socket.ts";
import type {
ConnectionClose,
ConnectionStart,
ConnectionTune,
} from "./amqp_types.ts";
import {
CHANNEL,
CHANNEL_OPEN,
CHANNEL_OPEN_OK,
CONNECTION,
CONNECTION_CLOSE,
CONNECTION_CLOSE_OK,
CONNECTION_OPEN,
CONNECTION_OPEN_OK,
CONNECTION_START,
CONNECTION_START_OK,
CONNECTION_TUNE,
CONNECTION_TUNE_OK,
HARD_ERROR_CONNECTION_FORCED,
} from "./amqp_constants.ts";
import { AmqpMultiplexer, createAmqpMux } from "./amqp_multiplexer.ts";
import { serializeConnectionError } from "./error_handling.ts";
import { createResolvable, ResolvablePromise } from "./resolvable.ts";
export interface AmqpConnectionOptions {
username: string;
password: string;
vhost?: string;
heartbeatInterval?: number;
frameMax?: number;
loglevel: "debug" | "none";
}
const NULL_CHAR = String.fromCharCode(0);
function credentials(username: string, password: string) {
return `${NULL_CHAR}${username}${NULL_CHAR}${password}`;
}
const clientProperties = Object.freeze({
product: "deno-amqp",
platform: `Deno ${Deno.version.deno} https://deno.land`,
version: "0",
information: "https://deno.land/x/amqp/",
});
function tune(ours: number | undefined, theirs: number) {
if (ours === undefined) {
return theirs;
}
if (ours === 0) {
return Math.max(ours, theirs);
}
return Math.min(ours, theirs);
}
export class AmqpConnection implements AmqpConnection {
#channelMax = -1;
#isOpen = false;
#channelNumbers: number[] = [];
#username: string;
#password: string;
#vhost: string;
#closedPromise: ResolvablePromise<void>;
#options: AmqpConnectionOptions;
#socket: AmqpSocket;
#mux: AmqpMultiplexer;
constructor(socket: AmqpSocket, options: AmqpConnectionOptions) {
this.#options = options;
this.#socket = socket;
this.#mux = createAmqpMux(this.#socket);
this.#username = options.username;
this.#password = options.password;
this.#vhost = options.vhost || "/";
this.#closedPromise = createResolvable<void>();
this.#mux.receive(0, CONNECTION, CONNECTION_CLOSE)
.then(this.#handleClose)
.catch(this.#closedPromise.reject)
.finally(() => {
this.#isOpen = false;
});
}
#handleClose = async (args: ConnectionClose) => {
this.#isOpen = false;
await this.#mux.send(0, CONNECTION, CONNECTION_CLOSE_OK, args);
this.#closedPromise.reject(new Error(serializeConnectionError(args)));
this.#socket.close();
};
#handleStart = async (_args: ConnectionStart) => {
await this.#mux.send(0, CONNECTION, CONNECTION_START_OK, {
clientProperties,
response: credentials(this.#username, this.#password),
});
};
#handleTune = async (args: ConnectionTune) => {
const heartbeatInterval = tune(
this.#options.heartbeatInterval,
args.heartbeat,
);
this.#channelMax = tune(undefined, args.channelMax);
const frameMax = tune(this.#options.frameMax, args.frameMax);
await this.#mux.send(0, CONNECTION, CONNECTION_TUNE_OK, {
heartbeat: heartbeatInterval,
channelMax: this.#channelMax,
frameMax,
});
this.#socket.tune({
frameMax,
sendTimeout: heartbeatInterval * 1000,
readTimeout: heartbeatInterval * 1000 * 2,
});
await this.#mux.send(0, CONNECTION, CONNECTION_OPEN, {
virtualHost: this.#vhost,
});
await this.#mux.receive(0, CONNECTION, CONNECTION_OPEN_OK);
};
/**
* Open this connection.
*/
async open() {
await this.#socket.start();
await this.#mux.receive(0, CONNECTION, CONNECTION_START).then(
this.#handleStart,
);
await this.#mux.receive(0, CONNECTION, CONNECTION_TUNE).then(
this.#handleTune,
);
this.#isOpen = true;
}
/**
* Creates and opens a new channel
*/
async openChannel(): Promise<AmqpChannel> {
for (
let channelNumber = 1;
channelNumber < this.#channelMax;
++channelNumber
) {
if (!this.#channelNumbers.find((num) => num === channelNumber)) {
this.#channelNumbers.push(channelNumber);
await this.#mux.send(channelNumber, CHANNEL, CHANNEL_OPEN, {});
await this.#mux.receive(channelNumber, CHANNEL, CHANNEL_OPEN_OK);
const channel = new AmqpChannel(channelNumber, this.#mux);
return channel;
}
}
throw new Error(`Maximum channels ${this.#channelMax} reached`);
}
/**
* Gracefully close this connection.
*/
async close() {
if (this.#isOpen) {
await this.#mux.send(0, CONNECTION, CONNECTION_CLOSE, {
classId: 0,
methodId: 0,
replyCode: HARD_ERROR_CONNECTION_FORCED,
});
await this.#mux.receive(0, CONNECTION, CONNECTION_CLOSE_OK);
this.#socket.close();
}
this.#isOpen = false;
this.#closedPromise.resolve();
}
/**
* Returns a promise that is settled when this connection is closed.
*
* If the connection is gracefully closed, the promise will _resolve_.
*
* If the connection is unexpectedly closed by the server or from an error, the promise
* will _reject_ with the reason.
*/
async closed() {
return await this.#closedPromise;
}
}