-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
antonputra
merged 2 commits into
antonputra:main
from
vorandrew:feature/go-concurrency-nerf
Sep 25, 2024
Merged
ref: GOMAXPROCS=1 #288
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,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); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://blog.howardjohn.info/posts/gomaxprocs/
golang/go#33803
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!