-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageBrokerChannel.ts
30 lines (26 loc) · 1.01 KB
/
MessageBrokerChannel.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
import "https://deno.land/x/[email protected]/load.ts";
import { AmqpChannel, AmqpConnect } from "./deps.ts";
let channel: AmqpChannel | undefined = undefined;
let pendingPromise: Promise<AmqpChannel> | undefined = undefined;
export function getChannel(): Promise<AmqpChannel> {
// `fix: use a fresh amqp channel for every client`
// This is worse for performance, but solves a weird bug where queues are not correctly declared if a channel has been used before.
//if (channel) return new Promise((resolve) => resolve(channel!));
//if (pendingPromise) return pendingPromise;
pendingPromise = openChannel();
return pendingPromise;
}
function openChannel(): Promise<AmqpChannel> {
// deno-lint-ignore no-async-promise-executor
return new Promise<AmqpChannel>(
async (resolve, reject) => {
try {
const connection = await AmqpConnect(Deno.env.get("AMQP_URL")!);
channel = await connection.openChannel();
resolve(channel);
} catch (error) {
reject(error);
}
},
);
}