Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth - recreate client on getconection if disconnected #107

Merged
merged 15 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 49 additions & 30 deletions app/api/auth/[...nextauth]/options.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,55 @@
import { RedisClientType, createClient } from "falkordb";
import CredentialsProvider from "next-auth/providers/credentials"
import { AuthOptions } from "next-auth"
import { AuthOptions, User } from "next-auth"


export const connections = new Map<number, RedisClientType>();
const connections = new Map<number, RedisClientType>();

async function newClient(credentials: {host: string, port: string, password: string, username: string}, id: number) {
const client = await createClient({
socket: {
host: credentials.host ?? "localhost",
port: credentials.port ? parseInt(credentials.port) : 6379,
reconnectStrategy: false
},
password: credentials.password ?? undefined,
username: credentials.username ?? undefined
})

// Save connection in connections map for later use
connections.set(id, client as RedisClientType)

await client.on('error', err => {
// Close coonection on error and remove from connections map
console.error('FalkorDB Client Error', err)
const connection = connections.get(id)
if (connection) {
connections.delete(id)
connection.disconnect()
.catch((e) => {
console.warn('FalkorDB Client Disconnect Error', e)
})
}
}).connect();

// Verify connection
await client.ping()
return client
}

export async function getConnection(user: User) {
let conn = connections.get(user.id)
if (!conn) {
await newClient({
host: user.host,
port: user.port.toString() ?? "6379",
username: user.username,
password: user.password,
}, user.id)
}
return conn
}

let userId = 1;

const authOptions: AuthOptions = {
Expand All @@ -26,34 +72,7 @@ const authOptions: AuthOptions = {
const id = userId;
userId += 1;

const client = await createClient({
socket: {
host: credentials.host ?? "localhost",
port: credentials.port ? parseInt(credentials.port, 10) : 6379,
reconnectStrategy: false
},
password: credentials.password ?? undefined,
username: credentials.username ?? undefined
})

// Save connection in connections map for later use
connections.set(id, client as RedisClientType)

await client.on('error', err => {
// Close coonection on error and remove from connections map
console.error('FalkorDB Client Error', err)
const connection = connections.get(id)
if (connection) {
connections.delete(id)
connection.disconnect()
.catch((e) => {
console.warn('FalkorDB Client Disconnect Error', e)
})
}
}).connect();

// Verify connection
await client.ping()
await newClient(credentials, id)

const res = {
id,
Expand Down
4 changes: 2 additions & 2 deletions app/api/graph/[graph]/[node]/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextRequest, NextResponse } from "next/server";
import { Graph } from 'falkordb';
import { getServerSession } from "next-auth/next";
import authOptions, { connections } from "../../../auth/[...nextauth]/options";
import authOptions, { getConnection } from "../../../auth/[...nextauth]/options";

// eslint-disable-next-line import/prefer-default-export
export async function GET(request: NextRequest, { params }: { params: { graph: string, node: string } }) {
Expand All @@ -12,7 +12,7 @@ export async function GET(request: NextRequest, { params }: { params: { graph: s
return NextResponse.json({ message: "Not authenticated" }, { status: 401 })
}

const client = connections.get(id)
const client = await getConnection(session.user)
if (!client) {
return NextResponse.json({ message: "Not authenticated" }, { status: 401 })
}
Expand Down
4 changes: 2 additions & 2 deletions app/api/graph/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextRequest, NextResponse } from "next/server";
import { Graph } from 'falkordb';
import { getServerSession } from "next-auth/next";
import authOptions, { connections } from "../auth/[...nextauth]/options";
import authOptions, { getConnection } from "../auth/[...nextauth]/options";

// eslint-disable-next-line import/prefer-default-export
export async function GET(request: NextRequest) {
Expand All @@ -12,7 +12,7 @@ export async function GET(request: NextRequest) {
return NextResponse.json({ message: "Not authenticated" }, { status: 401 })
}

const client = connections.get(id)
const client = await getConnection(session.user)
if(!client) {
return NextResponse.json({ message: "Not authenticated" }, { status: 401 })
}
Expand Down
Loading