Skip to content

Commit 70240f9

Browse files
authored
Adds a connection pool to RedisChatHistory (#5963)
1 parent f482048 commit 70240f9

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

libs/langchain-redis/src/chat_histories.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
createClient,
32
RedisClientOptions,
43
RedisClientType,
54
RedisModules,
@@ -12,6 +11,7 @@ import {
1211
mapChatMessagesToStoredMessages,
1312
mapStoredMessagesToChatMessages,
1413
} from "@langchain/core/messages";
14+
import { pool } from "./connections.js";
1515

1616
/**
1717
* Type for the input to the `RedisChatMessageHistory` constructor.
@@ -68,7 +68,7 @@ export class RedisChatMessageHistory extends BaseListChatMessageHistory {
6868
super(fields);
6969

7070
const { sessionId, sessionTTL, config, client } = fields;
71-
this.client = (client ?? createClient(config ?? {})) as RedisClientType<
71+
this.client = (client ?? pool.getClient(config)) as RedisClientType<
7272
RedisModules,
7373
RedisFunctions,
7474
RedisScripts
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { createClient, RedisClientOptions } from "redis";
2+
3+
// A minimalistic connection pool to avoid creating multiple connections
4+
class RedisConnectionPool {
5+
clients = new Map();
6+
7+
getClient(config: RedisClientOptions = {}) {
8+
if (!this.clients.has(config))
9+
this.clients.set(config, createClient(config));
10+
return this.clients.get(config);
11+
}
12+
}
13+
14+
export const pool = new RedisConnectionPool();

0 commit comments

Comments
 (0)