-
Couldn't load subscription status.
- Fork 314
Test connection pool concurrency #2605
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
Open
antoniosarosi
wants to merge
5
commits into
canary
Choose a base branch
from
antonio/concurrency
base: canary
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 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 hidden or 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 hidden or 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 hidden or 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,166 @@ | ||
| // Used to test connection pool concurrency | ||
|
|
||
| const http = require("http"); | ||
| const { URL } = require("url"); | ||
|
|
||
| // Get host and port. | ||
| const HOST = getArg("--host") || process.env.HOST || "127.0.0.1"; | ||
| const PORT = Number(getArg("--port") || process.env.PORT || 8001); | ||
|
|
||
| // Latency in milliseconds. | ||
| const LATENCY = Number(getArg("--latency") || process.env.LATENCY || 50); | ||
|
|
||
| // Get CLI args. | ||
| function getArg(flag) { | ||
| const i = process.argv.indexOf(flag); | ||
| return i !== -1 ? process.argv[i + 1] : undefined; | ||
| } | ||
|
|
||
| // Sleep millis. | ||
| function sleep(ms) { | ||
| return new Promise((res) => setTimeout(res, ms)); | ||
| } | ||
|
|
||
| // Respond with JSON. | ||
| function json(res, status, bodyObj) { | ||
| const body = JSON.stringify(bodyObj); | ||
| res.writeHead(status, { | ||
| "Content-Type": "application/json", | ||
| "Content-Length": Buffer.byteLength(body), | ||
| "Cache-Control": "no-store", | ||
| "Connection": "keep-alive", | ||
| // CORS (harmless if you curl) | ||
| "Access-Control-Allow-Origin": "*", | ||
| "Access-Control-Allow-Headers": "Content-Type, Authorization", | ||
| }); | ||
| res.end(body); | ||
| } | ||
|
|
||
| async function handleRequest(req, res) { | ||
| const url = new URL(req.url, `http://${req.headers.host}`); | ||
|
|
||
| // Health | ||
| if (req.method === "GET" && url.pathname === "/health") { | ||
| return json(res, 200, { ok: true }); | ||
| } | ||
|
|
||
| // OpenAI generic. | ||
| if (req.method === "POST" && url.pathname === "/v1/chat/completions") { | ||
| let body = ""; | ||
|
|
||
| req.on("data", chunk => body += chunk); | ||
|
|
||
| req.on("end", async () => { | ||
| // We don't actually need the request payload for this test. | ||
| // But parse if present to avoid client errors. | ||
| try { | ||
| if (body && body.length) { | ||
| JSON.parse(body); | ||
| } | ||
| } catch { | ||
| return json(res, 400, { error: { message: "Invalid JSON" } }); | ||
| } | ||
|
|
||
| // Simulate latency for concurrency testing | ||
| await sleep(LATENCY); | ||
|
|
||
| const now = Math.floor(Date.now() / 1000); | ||
|
|
||
| return json(res, 200, { | ||
| id: `cmpl-${now}-${Math.random().toString(36).slice(2, 8)}`, | ||
| object: "chat.completion", | ||
| created: now, | ||
| model: "concurrency-test", | ||
| choices: [ | ||
| { | ||
| index: 0, | ||
| message: { role: "assistant", content: "OpenAI" }, | ||
| finish_reason: "stop", | ||
| }, | ||
| ], | ||
| usage: { prompt_tokens: 0, completion_tokens: 1, total_tokens: 1 }, | ||
| }); | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| // Anthropic. | ||
| if (req.method === "POST" && url.pathname === "/v1/messages") { | ||
| let body = ""; | ||
|
|
||
| req.on("data", chunk => body += chunk); | ||
|
|
||
| req.on("end", async () => { | ||
| // We don't actually need the request payload for this test. | ||
| // But parse if present to avoid client errors. | ||
| try { | ||
| if (body && body.length) { | ||
| JSON.parse(body); | ||
| } | ||
| } catch { | ||
| return json(res, 400, { error: { message: "Invalid JSON" } }); | ||
| } | ||
|
|
||
| // Simulate latency for concurrency testing | ||
| await sleep(LATENCY); | ||
|
|
||
| const now = Math.floor(Date.now() / 1000); | ||
|
|
||
| return json(res, 200, { | ||
| id: `msg_${Math.random().toString(36).slice(2, 10)}`, | ||
| type: "message", | ||
| role: "assistant", | ||
| model: "concurrency-test", | ||
| content: [ | ||
| { type: "text", text: "Anthropic" } | ||
| ], | ||
| stop_reason: "end_turn", | ||
| stop_sequence: null, | ||
| usage: { input_tokens: 0, output_tokens: 1 }, | ||
| created_at: now, | ||
| }); | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| // Not found | ||
| json(res, 404, { error: { message: "Not found" } }); | ||
| } | ||
|
|
||
| const server = http.createServer(async (req, res) => { | ||
| console.log(`${req.method} ${req.url}`); | ||
|
|
||
| try { | ||
| await handleRequest(req, res); | ||
| } catch (e) { | ||
| json(res, 500, { error: { message: e?.message || "Internal error" } }); | ||
| } | ||
| }); | ||
|
|
||
| server.listen({ host: HOST, port: PORT, reuseAddress: true }, () => { | ||
| process.stdout.write(`Concurrency test server listening on http://${HOST}:${PORT}\n`); | ||
| }); | ||
|
|
||
| const sockets = new Set(); | ||
|
|
||
| server.on("connection", (socket) => { | ||
| sockets.add(socket); | ||
| socket.on("close", () => sockets.delete(socket)); | ||
| }); | ||
|
|
||
|
|
||
| function shutdown() { | ||
| server.close(() => process.exit(0)); | ||
| for (const s of sockets) { | ||
| try { | ||
| s.destroy(); | ||
| } catch { | ||
| // Ignore errors | ||
| } | ||
| } | ||
| } | ||
|
|
||
| process.on("SIGINT", shutdown); | ||
| process.on("SIGTERM", shutdown); | ||
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
Check warning
Code scanning / CodeQL
Log injection Medium
Copilot Autofix
AI 14 days ago
To prevent log injection, any user-controlled values included in the log string (such as
req.urland potentiallyreq.method) should have line breaks (\r,\n) stripped or replaced. The best and simplest mitigation is to process each such value withString.prototype.replace(/\r|\n/g, "")before logging.req.methodandreq.urlthrough a sanitizing function that removes/replaces newlines and carriage returns.sanitizeForLog(str)) that takes a string and strips newlines, then use it for bothreq.methodandreq.urlin your log statement.Apply these changes only to this file/snippet.