-
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.
Merge pull request #1041 from golemfactory/bugfix/tcp-proxy-patch
Fix TcpProxy for non HTTP communication
- Loading branch information
Showing
16 changed files
with
486 additions
and
248 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
File renamed without changes.
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,95 @@ | ||
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, | ||
}); | ||
|
||
const abortController = new AbortController(); | ||
|
||
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, | ||
}, | ||
signalOrTimeout: abortController.signal, | ||
}); | ||
|
||
const PORT_ON_PROVIDER = 80; | ||
const PORT_ON_REQUESTOR = 8080; | ||
|
||
const exe = await rental.getExeUnit(abortController.signal); | ||
|
||
// 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`, { | ||
signalOrTimeout: abortController.signal, | ||
}); | ||
|
||
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. Process PID: %d", process.pid); | ||
return; | ||
} | ||
|
||
abortController.abort("SIGINT called"); | ||
|
||
isClosing = true; | ||
|
||
console.log("Shutting down gracefully"); | ||
await proxy.close(); | ||
logger.info("Shutdown routine completed"); | ||
}; | ||
|
||
process.on("SIGINT", () => { | ||
stopServer() | ||
.then(() => rental.stopAndFinalize()) | ||
.catch((err) => logger.error("Failed to shutdown cleanly", err)); | ||
}); | ||
|
||
await waitFor(() => server.isFinished()); | ||
console.log("Server process finished"); | ||
} 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
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export { ExeUnit, LifecycleFunction, ExeUnitOptions } from "./exe-unit"; | ||
export { Batch } from "./batch"; | ||
export { GolemWorkError, WorkErrorCode } from "./error"; | ||
export { TcpProxy } from "../../network/tcpProxy"; | ||
export { TcpProxy } from "../../network/tcp-proxy"; |
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
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
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.