feat: keep containers alive for 60s of inactivity instead of killing them on disconnect
This commit is contained in:
@ -34,6 +34,9 @@ import {
|
||||
saveFileRL,
|
||||
} from "./ratelimit";
|
||||
|
||||
// The amount of time in ms that a container will stay alive without a hearbeat.
|
||||
const CONTAINER_TIMEOUT = 60_000;
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const app: Express = express();
|
||||
@ -161,7 +164,7 @@ io.on("connection", async (socket) => {
|
||||
try {
|
||||
// Start a new container if the container doesn't exist or it timed out.
|
||||
if (!containers[data.sandboxId] || !(await containers[data.sandboxId].isRunning())) {
|
||||
containers[data.sandboxId] = await Sandbox.create({ timeoutMs: 1200_000 });
|
||||
containers[data.sandboxId] = await Sandbox.create({ timeoutMs: CONTAINER_TIMEOUT });
|
||||
console.log("Created container ", data.sandboxId);
|
||||
}
|
||||
} catch (e: any) {
|
||||
@ -198,6 +201,17 @@ io.on("connection", async (socket) => {
|
||||
|
||||
socket.emit("loaded", sandboxFiles.files);
|
||||
|
||||
socket.on("heartbeat", async () => {
|
||||
try {
|
||||
// This keeps the container alive for another CONTAINER_TIMEOUT seconds.
|
||||
// The E2B docs are unclear, but the timeout is relative to the time of this method call.
|
||||
await containers[data.sandboxId].setTimeout(CONTAINER_TIMEOUT);
|
||||
} catch (e: any) {
|
||||
console.error("Error setting timeout:", e);
|
||||
io.emit("error", `Error: set timeout. ${e.message ?? e}`);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("getFile", (fileId: string, callback) => {
|
||||
console.log(fileId);
|
||||
try {
|
||||
@ -656,25 +670,6 @@ io.on("connection", async (socket) => {
|
||||
}
|
||||
|
||||
if (data.isOwner && connections[data.sandboxId] <= 0) {
|
||||
await Promise.all(
|
||||
Object.entries(terminals).map(async ([key, terminal]) => {
|
||||
await terminal.close();
|
||||
delete terminals[key];
|
||||
})
|
||||
);
|
||||
|
||||
await lockManager.acquireLock(data.sandboxId, async () => {
|
||||
try {
|
||||
if (containers[data.sandboxId]) {
|
||||
await containers[data.sandboxId].kill();
|
||||
delete containers[data.sandboxId];
|
||||
console.log("Closed container", data.sandboxId);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error closing container ", data.sandboxId, error);
|
||||
}
|
||||
});
|
||||
|
||||
socket.broadcast.emit(
|
||||
"disableAccess",
|
||||
"The sandbox owner has disconnected."
|
||||
|
Reference in New Issue
Block a user