Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: GOMAXPROCS=1 #288

Merged
merged 2 commits into from
Sep 25, 2024
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
3 changes: 3 additions & 0 deletions lessons/210/deploy/go-app/2-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ spec:
containers:
- name: go-app
image: aputra/go-app-210:v3
env:
- name: GOMAXPROCS
value: 1
Comment on lines +40 to +42
Copy link

@DarVoid DarVoid Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you ever want to run it with the limits of the container here:

- name: GOMEMLIMIT
  valueFrom:
    resourceFieldRef:
      resource: limits.memory
- name: GOMAXPROCS
  valueFrom:
    resourceFieldRef:
      resource: limits.cpu

Copy link

@DarVoid DarVoid Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

ports:
- name: http
containerPort: 8080
Expand Down
96 changes: 50 additions & 46 deletions lessons/210/node-app/app.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,69 @@
import express from "express";
import save from "./devices.js";
import summary from "./metrics.js";
import save from "./devices.js";
import { randomUUID } from "crypto";
import { register } from "prom-client";
import config from "./config.js";

const app = express();
import * as http from "node:http";

app.use(express.json());
const server = http.createServer({ keepAliveTimeout: 60000 }, (req, res) => {
if (req.url === "/metrics") {
res.writeHead(200, { "Content-Type": register.contentType });
register.metrics().then((data) => res.end(data));
return;
}

// Expose Prometheus metrics.
app.get("/metrics", async (_req, res) => {
res.setHeader("Content-Type", register.contentType);
register.metrics().then((data) => res.status(200).send(data));
});
if (req.url === "/healthz") {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("OK");
return;
}

// Returns the status of the application.
app.get("/healthz", async (_req, res) => {
// Placeholder for the health check
res.send("OK");
});
if (req.method === "GET" && req.url === "/api/devices") {
const device = {
uuid: "9add349c-c35c-4d32-ab0f-53da1ba40a2a",
mac: "5F-33-CC-1F-43-82",
firmware: "2.1.6",
};

// Returns a list of connected devices.
app.get("/api/devices", async (_req, res) => {
const device = {
uuid: "9add349c-c35c-4d32-ab0f-53da1ba40a2a",
mac: "5F-33-CC-1F-43-82",
firmware: "2.1.6",
};
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(device));
return;
}

res.status(200).json(device);
});
if (req.method === "POST" && req.url === "/api/devices") {
let body = "";
req.on("data", (chunk) => {
body += chunk.toString();
});

// Registers the device.
app.post("/api/devices", async (req, res) => {
let device = req.body;
req.on("end", () => {
let device = JSON.parse(body);

// Generate a new UUID for the device.
device.uuid = randomUUID();
device.uuid = randomUUID();

// Get the current time to record the duration of the request.
const end = summary.startTimer();
const end = summary.startTimer();

// Save the device to the database.
save(device)
.then(() => {
// Record the duration of the insert query.
end({ op: "db" });
save(device)
.then(() => {
end({ op: "db" });

// Return Device back to the client.
res.status(201).json(device);
})
.catch((error) => {
// Log the error.
console.error(error);
res.writeHead(201, { "Content-Type": "application/json" });
res.end(JSON.stringify(device));
})
.catch((error) => {
console.error(error);

// Return a summary of the error to the client.
res.status(400).json({ message: error.message });
res.writeHead(400, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: error.message }));
});
});
});

app.listen(config.appPort, () => {
console.log(`Starting the web server on port ${config.appPort}`);
return;
}

res.writeHead(404, { "Content-Type": "text/plain" });
res.end("Not Found");
});

server.listen(config.appPort);
Loading