Skip to content

Commit

Permalink
(enh): simple ttl caching ssh sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
matyson committed Oct 1, 2024
1 parent c5aaa84 commit c8db021
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
25 changes: 16 additions & 9 deletions apps/deepsirius-ui/src/server/api/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { env } from '~/env.mjs';
import { getServerAuthSession } from '~/server/auth';
import { prisma } from '~/server/db';
import { ssh } from '~/server/ssh';
import { cache as sshCache } from '../ssh-cache';

type CreateContextOptions = {
session: Session | null;
Expand Down Expand Up @@ -154,13 +155,21 @@ const ensureSSHConnection = t.middleware(async ({ ctx, next }) => {
throw new TRPCError({ code: 'UNAUTHORIZED' });
}

const connection = await ctx.ssh.connect({
username: ctx.session.user.name,
privateKey: ctx.privateKey,
host: env.SSH_HOST,
port: 22,
passphrase: env.PRIVATE_KEY_PASSPHRASE,
});
let connection = sshCache.get(ctx.session.user.name);
if (!connection) {
connection = await ctx.ssh.connect({
username: ctx.session.user.name,
privateKey: ctx.privateKey,
host: env.SSH_HOST,
port: 22,
passphrase: env.PRIVATE_KEY_PASSPHRASE,
});
if (!connection) {
throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR' });
}

sshCache.set(ctx.session.user.name, connection, 1000 * 60 * 6);
}

const res = await next({
ctx: {
Expand All @@ -170,8 +179,6 @@ const ensureSSHConnection = t.middleware(async ({ ctx, next }) => {
},
});

connection.dispose();

return res;
});

Expand Down
49 changes: 49 additions & 0 deletions apps/deepsirius-ui/src/server/ssh-cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/** The ISC License
Copyright (c) 2010-2023 Isaac Z. Schlueter and Contributors
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */

import type { NodeSSH } from 'node-ssh';

export const cache = {
data: new Map<PropertyKey, NodeSSH>(),
timers: new Map<PropertyKey, ReturnType<typeof setTimeout>>(),
set: (k: PropertyKey, v: NodeSSH, ttl: number) => {
if (cache.timers.has(k)) {
clearTimeout(cache.timers.get(k));
}
cache.timers.set(
k,
setTimeout(() => cache.delete(k), ttl),
);
cache.data.set(k, v);
},
get: (k: PropertyKey) => cache.data.get(k),
has: (k: PropertyKey) => cache.data.has(k),
delete: (k: PropertyKey) => {
if (cache.timers.has(k)) {
clearTimeout(cache.timers.get(k));
}
cache.timers.delete(k);
cache.data.get(k)?.dispose();
return cache.data.delete(k);
},
clear: () => {
cache.data.clear();
for (const v of cache.timers.values()) {
clearTimeout(v);
}
cache.timers.clear();
},
};

0 comments on commit c8db021

Please sign in to comment.