-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fixed TcpProxy implementation so that it works with non-http use…
…-cases as well
- Loading branch information
Showing
8 changed files
with
289 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* eslint-disable */ | ||
const http = require("http"); | ||
|
||
(async function main() { | ||
const PORT = parseInt(process.env["PORT"] ?? "80"); | ||
|
||
// Increase the value if you want to test long response/liveliness scenarios | ||
const SIMULATE_DELAY_SEC = parseInt(process.env["SIMULATE_DELAY_SEC"] ?? "0"); | ||
|
||
const respond = (res) => { | ||
res.writeHead(200); | ||
res.end("Hello Golem!"); | ||
}; | ||
|
||
const app = http.createServer((req, res) => { | ||
if (SIMULATE_DELAY_SEC > 0) { | ||
setTimeout(() => { | ||
respond(res); | ||
}, SIMULATE_DELAY_SEC * 1000); | ||
} else { | ||
respond(res); | ||
} | ||
}); | ||
|
||
const server = app.listen(PORT, () => console.log(`HTTP server started at "http://localhost:${PORT}"`)); | ||
|
||
const shutdown = () => { | ||
server.close((err) => { | ||
if (err) { | ||
console.error("Server close encountered an issue", err); | ||
} else { | ||
console.log("Server closed successfully"); | ||
} | ||
}); | ||
}; | ||
|
||
process.on("SIGINT", shutdown); | ||
process.on("SIGTERM", shutdown); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { GolemNetwork, waitFor } from "@golem-sdk/golem-js"; | ||
import { pinoPrettyLogger } from "@golem-sdk/pino-logger"; | ||
|
||
(async () => { | ||
const logger = pinoPrettyLogger({ | ||
level: "info", | ||
}); | ||
const glm = new GolemNetwork({ | ||
logger, | ||
}); | ||
|
||
try { | ||
await glm.connect(); | ||
|
||
const network = await glm.createNetwork({ | ||
ip: "10.0.0.0/24", | ||
}); | ||
|
||
const rental = await glm.oneOf({ | ||
order: { | ||
demand: { | ||
workload: { | ||
imageTag: "golem/node:20-alpine", | ||
capabilities: ["vpn"], | ||
}, | ||
}, | ||
market: { | ||
rentHours: 0.25, | ||
pricing: { | ||
model: "burn-rate", | ||
avgGlmPerHour: 1, | ||
}, | ||
}, | ||
network, | ||
}, | ||
}); | ||
|
||
const PORT_ON_PROVIDER = 80; | ||
const PORT_ON_REQUESTOR = 8080; | ||
|
||
const exe = await rental.getExeUnit(); | ||
|
||
// Install the server script | ||
await exe.uploadFile(`./rental-model/advanced/tcp-proxy/server.js`, "/golem/work/server.js"); | ||
|
||
// Start the server process on the provider | ||
const server = await exe.runAndStream(`PORT=${PORT_ON_PROVIDER} node /golem/work/server.js`); | ||
|
||
server.stdout.subscribe((data) => console.log("provider>", data)); | ||
server.stderr.subscribe((data) => console.error("provider>", data)); | ||
|
||
// Create a proxy instance | ||
const proxy = exe.createTcpProxy(PORT_ON_PROVIDER); | ||
proxy.events.on("error", (error) => console.error("TcpProxy reported an error:", error)); | ||
|
||
// Start listening and expose the port on your requestor machine | ||
await proxy.listen(PORT_ON_REQUESTOR); | ||
console.log(`Server Proxy listen at http://localhost:${PORT_ON_REQUESTOR}`); | ||
|
||
let isClosing = false; | ||
const stopServer = async () => { | ||
if (isClosing) { | ||
console.log("Already closing, ignoring subsequent shutdown request"); | ||
return; | ||
} | ||
|
||
isClosing = true; | ||
|
||
console.log("Shutting down gracefully"); | ||
await proxy.close(); | ||
}; | ||
|
||
process.on("SIGINT", () => { | ||
stopServer() | ||
.then(() => rental.stopAndFinalize()) | ||
.then(() => logger.info("Shutdown routine completed")) | ||
.catch((err) => logger.error("Failed to shutdown cleanly", err)); | ||
}); | ||
|
||
await waitFor(() => server.isFinished()); | ||
} catch (error) { | ||
logger.error("Failed to run the example", error); | ||
} finally { | ||
await glm.disconnect(); | ||
} | ||
})().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.