fix: 终审识别文件扫——JWKS 失败→503、getNats 在途去重、service-token clientId 取自 config

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
lovefyf
2026-08-13 14:16:32 +08:00
co-authored by Claude Opus 4.8
parent 203815d6a2
commit e13d86fe4f
4 changed files with 71 additions and 4 deletions
+20 -2
View File
@@ -3,11 +3,29 @@ import type { NatsConnection } from 'nats';
import { config } from '../config';
let nc: NatsConnection | null = null;
/**
* 在途去重:nc 只在 connect() **resolve 之后**才被赋值,所以并发的首批调用者读到的
* 都还是 null,于是各自拨一条连接;最后一个赋值进 nc,前面那些谁也够不着——drain 不掉、
* 也不计入 closeNats,就那么挂在服务端的连接表上直到进程死。记住这次在途的 promise,
* 让并发调用者共享同一次拨号;成功与失败都清空,失败后下一次调用可以重新拨。
*/
let connecting: Promise<NatsConnection> | null = null;
export async function getNats(): Promise<NatsConnection> {
if (nc && !nc.isClosed()) return nc;
nc = await connect({ servers: config.natsUrl, name: config.serviceName, timeout: 5000 });
return nc;
if (connecting) return await connecting;
const dial = connect({ servers: config.natsUrl, name: config.serviceName, timeout: 5000 })
.then((c) => {
nc = c;
connecting = null;
return c;
})
.catch((err: unknown) => {
connecting = null;
throw err;
});
connecting = dial;
return await dial;
}
export async function checkNats(): Promise<'ok' | 'fail'> {