@@ -13,6 +13,10 @@ export const wsClients = new Map<string, Lark.WSClient>();
1313export const httpServers = new Map < string , http . Server > ( ) ;
1414export const botOpenIds = new Map < string , string > ( ) ;
1515export const botNames = new Map < string , string > ( ) ;
16+ // HTTP close is awaited, so a replacement monitor can write identity before
17+ // registering its replacement server. Revisions keep stale close cleanup from
18+ // erasing that newer identity.
19+ const botIdentityRevisions = new Map < string , number > ( ) ;
1620
1721export const FEISHU_WEBHOOK_MAX_BODY_BYTES = 64 * 1024 ;
1822export const FEISHU_WEBHOOK_BODY_TIMEOUT_MS = 5_000 ;
@@ -30,6 +34,10 @@ type WebhookAnomalyDefaults = {
3034 logEvery : number ;
3135} ;
3236
37+ type BotIdentitySnapshot = {
38+ revision : number ;
39+ } ;
40+
3341const FEISHU_WEBHOOK_RATE_LIMIT_FALLBACK_DEFAULTS : WebhookRateLimitDefaults = {
3442 windowMs : 60_000 ,
3543 maxRequests : 120 ,
@@ -117,6 +125,57 @@ function closeWsClient(client: Lark.WSClient | undefined): void {
117125 }
118126}
119127
128+ function readBotIdentityRevision ( accountId : string ) : number {
129+ return botIdentityRevisions . get ( accountId ) ?? 0 ;
130+ }
131+
132+ function bumpBotIdentityRevision ( accountId : string ) : void {
133+ botIdentityRevisions . set ( accountId , readBotIdentityRevision ( accountId ) + 1 ) ;
134+ }
135+
136+ function captureBotIdentitySnapshot ( accountId : string ) : BotIdentitySnapshot {
137+ return { revision : readBotIdentityRevision ( accountId ) } ;
138+ }
139+
140+ function captureBotIdentitySnapshots ( ) : Array < [ accountId : string , snapshot : BotIdentitySnapshot ] > {
141+ const accountIds = new Set ( [ ...botOpenIds . keys ( ) , ...botNames . keys ( ) ] ) ;
142+ return Array . from ( accountIds , ( accountId ) : [ string , BotIdentitySnapshot ] => [
143+ accountId ,
144+ captureBotIdentitySnapshot ( accountId ) ,
145+ ] ) ;
146+ }
147+
148+ function clearFeishuBotIdentityStateIfUnchanged (
149+ accountId : string ,
150+ snapshot : BotIdentitySnapshot ,
151+ ) : void {
152+ if ( readBotIdentityRevision ( accountId ) !== snapshot . revision ) {
153+ return ;
154+ }
155+ botOpenIds . delete ( accountId ) ;
156+ botNames . delete ( accountId ) ;
157+ bumpBotIdentityRevision ( accountId ) ;
158+ }
159+
160+ export function setFeishuBotIdentityState (
161+ accountId : string ,
162+ identity : { botOpenId : string ; botName : string | undefined } ,
163+ ) : void {
164+ botOpenIds . set ( accountId , identity . botOpenId ) ;
165+ if ( identity . botName ) {
166+ botNames . set ( accountId , identity . botName ) ;
167+ } else {
168+ botNames . delete ( accountId ) ;
169+ }
170+ bumpBotIdentityRevision ( accountId ) ;
171+ }
172+
173+ export function clearFeishuBotIdentityState ( accountId : string ) : void {
174+ botOpenIds . delete ( accountId ) ;
175+ botNames . delete ( accountId ) ;
176+ bumpBotIdentityRevision ( accountId ) ;
177+ }
178+
120179function isServerNotRunningError ( error : Error ) : boolean {
121180 return ( error as NodeJS . ErrnoException ) . code === "ERR_SERVER_NOT_RUNNING" ;
122181}
@@ -155,23 +214,26 @@ export async function closeFeishuHttpServer(server: http.Server): Promise<void>
155214 } ) ;
156215}
157216
158- async function closeTrackedHttpServer ( accountId : string , server : http . Server ) : Promise < void > {
217+ export async function closeTrackedFeishuHttpServer (
218+ accountId : string ,
219+ server : http . Server ,
220+ ) : Promise < void > {
221+ const identitySnapshot = captureBotIdentitySnapshot ( accountId ) ;
159222 try {
160223 await closeFeishuHttpServer ( server ) ;
161224 } finally {
162225 if ( httpServers . get ( accountId ) === server ) {
163226 httpServers . delete ( accountId ) ;
227+ clearFeishuBotIdentityStateIfUnchanged ( accountId , identitySnapshot ) ;
164228 }
165- botOpenIds . delete ( accountId ) ;
166- botNames . delete ( accountId ) ;
167229 }
168230}
169231
170232async function closeTrackedHttpServers (
171233 entries : Array < [ accountId : string , server : http . Server ] > ,
172234) : Promise < void > {
173235 const results = await Promise . allSettled (
174- entries . map ( ( [ accountId , server ] ) => closeTrackedHttpServer ( accountId , server ) ) ,
236+ entries . map ( ( [ accountId , server ] ) => closeTrackedFeishuHttpServer ( accountId , server ) ) ,
175237 ) ;
176238 const rejected = results . find (
177239 ( result ) : result is PromiseRejectedResult => result . status === "rejected" ,
@@ -215,23 +277,25 @@ export async function stopFeishuMonitorState(accountId?: string): Promise<void>
215277 wsClients . delete ( accountId ) ;
216278 const server = httpServers . get ( accountId ) ;
217279 if ( server ) {
218- await closeTrackedHttpServer ( accountId , server ) ;
280+ await closeTrackedFeishuHttpServer ( accountId , server ) ;
219281 return ;
220282 }
221- botOpenIds . delete ( accountId ) ;
222- botNames . delete ( accountId ) ;
283+ clearFeishuBotIdentityState ( accountId ) ;
223284 return ;
224285 }
225286
226287 for ( const client of wsClients . values ( ) ) {
227288 closeWsClient ( client ) ;
228289 }
229290 wsClients . clear ( ) ;
291+ const identitySnapshots = captureBotIdentitySnapshots ( ) ;
230292 try {
231293 await closeTrackedHttpServers ( [ ...httpServers . entries ( ) ] ) ;
232294 } finally {
233- httpServers . clear ( ) ;
234- botOpenIds . clear ( ) ;
235- botNames . clear ( ) ;
295+ for ( const [ identityAccountId , snapshot ] of identitySnapshots ) {
296+ if ( ! httpServers . has ( identityAccountId ) ) {
297+ clearFeishuBotIdentityStateIfUnchanged ( identityAccountId , snapshot ) ;
298+ }
299+ }
236300 }
237301}
0 commit comments