forked from denodrivers/redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutor.ts
51 lines (45 loc) · 1.39 KB
/
executor.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
import type { Connection, SendCommandOptions } from "../connection.ts";
import type { Pool } from "./pool.ts";
import type { CommandExecutor } from "../executor.ts";
import { DefaultExecutor } from "../executor.ts";
import type { RedisReply, RedisValue } from "../protocol/shared/types.ts";
export function createPooledExecutor(pool: Pool<Connection>): CommandExecutor {
return new PooledExecutor(pool);
}
class PooledExecutor implements CommandExecutor {
readonly #pool: Pool<Connection>;
constructor(pool: Pool<Connection>) {
this.#pool = pool;
}
get connection(): Connection {
throw new Error("PooledExecutor.connection is not supported");
}
async exec(
command: string,
...args: RedisValue[]
): Promise<RedisReply> {
const connection = await this.#pool.acquire();
try {
const executor = new DefaultExecutor(connection);
return await executor.exec(command, ...args);
} finally {
this.#pool.release(connection);
}
}
async sendCommand(
command: string,
args?: RedisValue[],
options?: SendCommandOptions,
): Promise<RedisReply> {
const connection = await this.#pool.acquire();
try {
const executor = new DefaultExecutor(connection);
return await executor.sendCommand(command, args, options);
} finally {
this.#pool.release(connection);
}
}
close(): void {
return this.#pool.close();
}
}