Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
73 changes: 73 additions & 0 deletions src/exec/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { runtimeStore } from "../runtime/store.js";
import { cleanupVm } from "../runtime/cleanup.js";
import { sessionLogger } from "../utils/logger.js";

export interface Session {
sessionId: string;
createdAt: number;
lastActivityAt: number;
state: "creating" | "active" | "destroying";
}

const sessions = new Map<string, Session>();

export function getSession(sessionId: string): Session | undefined {
return sessions.get(sessionId);
}

export function createSession(sessionId: string): Session {
const session: Session = {
sessionId,
createdAt: Date.now(),
lastActivityAt: Date.now(),
state: "creating",
};
sessions.set(sessionId, session);
return session;
}

export function touchSession(sessionId: string): void {
const session = sessions.get(sessionId);
if (session) session.lastActivityAt = Date.now();
}

export async function destroySession(sessionId: string): Promise<boolean> {
const session = sessions.get(sessionId);
if (!session) return false;

session.state = "destroying";

const fn = runtimeStore.functions.get(sessionId);
if (fn) {
for (const vm of [...fn.vms]) {
await cleanupVm(fn, vm);
}
runtimeStore.functions.delete(sessionId);
}

sessions.delete(sessionId);
sessionLogger.info({ sessionId }, "session destroyed");
return true;
}

export function getActiveSessions(): number {
return sessions.size;
}

export function getAllSessions(): Session[] {
return [...sessions.values()];
}

export function startSessionReaper(ttlMs: number = 30 * 60 * 1000): void {
setInterval(() => {
const now = Date.now();
for (const [id, session] of sessions) {
if (now - session.lastActivityAt > ttlMs && session.state === "active") {
sessionLogger.info({ sessionId: id, idleMs: now - session.lastActivityAt }, "reaping idle session");
destroySession(id).catch(err => {
sessionLogger.error({ sessionId: id, err }, "session reap failed");
});
}
}
}, 60_000);
}
14 changes: 14 additions & 0 deletions src/runtime/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function buildPayload(req: any, subPath: string): string {
export function readVsockResponse(
socket: Socket,
timeout: number,
onStreamChunk?: (chunk: any) => void
): Promise<{ type: string; data: any; error?: string }> {
return new Promise((resolve, reject) => {
let buffer = "";
Expand All @@ -41,6 +42,14 @@ export function readVsockResponse(
onData = (chunk: Buffer) => {
buffer += chunk.toString();

if (buffer.length > 10 * 1024 * 1024) {
clearTimeout(timer);
cleanup();
socket.destroy();
reject(new Error("Response too large"));
return;
}

let index;

while ((index = buffer.indexOf("\n")) >= 0) {
Expand All @@ -53,6 +62,11 @@ export function readVsockResponse(
try {
const msg = JSON.parse(line);

if (msg.type === "stream") {
onStreamChunk?.(msg);
continue;
}

if (msg.type === "response" || msg.type === "error") {
clearTimeout(timer);
cleanup();
Expand Down
17 changes: 17 additions & 0 deletions src/runtime/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,20 @@ export async function sendRequest(subPath: string, req: any, res: any, vm: Vm) {
res.status(statusCode).json(msg.data ?? { error: msg.error });
}
}

export async function sendMessage(
vm: Vm,
message: Record<string, any>,
onStreamChunk?: (chunk: any) => void,
timeout: number = 60000,
): Promise<any> {
const socket = await getVmSocket(vm);
socket.write(JSON.stringify(message) + "\n");

transportLogger.debug(
{ vmId: vm.id, messageType: message.type, messageId: message.id },
"message sent to VM"
);

return readVsockResponse(socket, timeout, onStreamChunk);
}
56 changes: 56 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,59 @@ export interface RuntimeFunction {
vms: Vm[];
readyVms: Set<Vm>;
}

export interface ExecuteMessage {
type: "execute";
id: string;
command: string;
args: string[];
cwd?: string;
env?: Record<string, string>;
timeout?: number;
}

export interface WriteFileMessage {
type: "write_file";
id: string;
path: string;
content: string;
mode?: number;
}

export interface ReadFileMessage {
type: "read_file";
id: string;
path: string;
}

export interface ListFilesMessage {
type: "list_files";
id: string;
path?: string;
recursive?: boolean;
}

export interface CancelMessage {
type: "cancel";
id: string;
}

export interface StreamMessage {
type: "stream";
id: string;
stream: "stdout" | "stderr";
data: string;
}

export interface ResponseMessage {
type: "response";
id: string;
data: any;
}

export interface ErrorMessage {
type: "error";
id: string;
error: string;
code?: number;
}
1 change: 1 addition & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const vmManagerLogger = logger.child({ module: "vm-manager" });
export const transportLogger = logger.child({ module: "transport" });
export const protocolLogger = logger.child({ module: "protocol" });
export const cleanupLogger = logger.child({ module: "cleanup" });
export const sessionLogger = logger.child({module: "session"})

export const httpLoggerOptions: Options = {
logger: logger.child({ module: "http" }),
Expand Down
Loading