@@ -4,6 +4,8 @@ import { createChildLogger, getTracedAWSV3Client } from '@aws-github-runner/aws-
44
55const logger = createChildLogger ( 'sqs' ) ;
66
7+ const sqsClientsByRegion = new Map < string , SQS > ( ) ;
8+
79export interface ActionRequestMessage {
810 id : number ;
911 eventType : string ;
@@ -32,7 +34,8 @@ export interface GithubWorkflowEvent {
3234}
3335
3436export const sendActionRequest = async ( message : ActionRequestMessage ) : Promise < void > => {
35- const sqs = getTracedAWSV3Client ( new SQS ( { region : process . env . AWS_REGION } ) ) ;
37+ const region = getRegionFromQueueUrl ( message . queueId ) ?? process . env . AWS_REGION ;
38+ const sqs = getSqsClient ( region ) ;
3639
3740 const sqsMessage : SendMessageCommandInput = {
3841 QueueUrl : message . queueId ,
@@ -43,3 +46,30 @@ export const sendActionRequest = async (message: ActionRequestMessage): Promise<
4346
4447 await sqs . sendMessage ( sqsMessage ) ;
4548} ;
49+
50+ function getSqsClient ( region : string | undefined ) : SQS {
51+ if ( ! region ) {
52+ return getTracedAWSV3Client ( new SQS ( { } ) ) ;
53+ }
54+
55+ const cached = sqsClientsByRegion . get ( region ) ;
56+ if ( cached ) {
57+ return cached ;
58+ }
59+
60+ const client = getTracedAWSV3Client ( new SQS ( { region } ) ) ;
61+ sqsClientsByRegion . set ( region , client ) ;
62+ return client ;
63+ }
64+
65+ function getRegionFromQueueUrl ( queueUrl : string ) : string | undefined {
66+ try {
67+ const url = new URL ( queueUrl ) ;
68+ const parts = url . hostname . split ( '.' ) ;
69+ if ( parts . length >= 3 && parts [ 0 ] === 'sqs' ) {
70+ return parts [ 1 ] ;
71+ }
72+ } catch { }
73+
74+ return undefined ;
75+ }
0 commit comments