Add Next.js instrumentation hook and update HTTP agent options to disable keep-alive for Google OAuth
Build and Push Reader Image / docker (push) Successful in 2m21s

This commit is contained in:
2026-04-24 01:30:05 +07:00
parent d9a6629d31
commit 690a2fbd51
2 changed files with 35 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
/**
* Next.js Instrumentation Hook — chạy một lần khi server khởi động.
*
* Mục đích:
* 1. Ép IPv4-first để tránh Happy Eyeballs timeout khi gọi Google OAuth
* (openid-client bên trong NextAuth gọi oauth2.googleapis.com).
* 2. Tắt keep-alive trên global https agent để tránh stale connection:
* Sau lần đăng nhập đầu, connection pool giữ lại TCP socket tới Google.
* NAT/firewall của Docker drop socket này sau vài phút (silently).
* Khi đăng nhập lần 2, openid-client cố reuse socket đã chết → treo 3500ms.
* keepAlive: false buộc mở connection mới mỗi request, không reuse pool cũ.
*/
export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
const { setDefaultResultOrder } = await import("dns")
setDefaultResultOrder("ipv4first")
const https = await import("https")
https.globalAgent = new https.Agent({
keepAlive: false,
})
const http = await import("http")
http.globalAgent = new http.Agent({
keepAlive: false,
})
}
}
+7
View File
@@ -3,6 +3,13 @@ const readerApiOrigin = (process.env.READER_API_ORIGIN || "http://localhost:8000
const nextConfig = {
output: "standalone",
// Tắt HTTP keep-alive để tránh stale connection tới Google OAuth.
// Sau lần đăng nhập đầu, Node.js giữ TCP socket tới oauth2.googleapis.com trong pool.
// NAT/firewall của Docker drop socket sau vài phút (silently). Khi login lần 2,
// openid-client (bên trong NextAuth) reuse socket đã chết → request treo → OAUTH_CALLBACK_ERROR 3500ms.
httpAgentOptions: {
keepAlive: false,
},
typescript: {
ignoreBuildErrors: true,
},