Cloud Function调用Gcloud Pub/Sub发布消息超时问题求助
Cloud Function 调用Pub/Sub发布消息超时问题
我使用Node.js的Pub/Sub客户端包,从Cloud Function向GCP Pub/Sub主题发布消息时,遇到以下超时错误:
Total timeout of API google.pubsub.v1.The publisher exceeded 60000 milliseconds before any response was received.
已尝试的解决方案
- 将Pub/Sub客户端的超时时间从60秒增加到600秒
- 提升Cloud Function的CPU和内存配置
- 使用批量消息发送以提升效率
- 创建单个Topic实例并复用它处理所有消息
- 尝试避免冷启动问题
- 使用管理员权限进行操作
- 尝试不使用JSON消息,改为发布缓冲区
发布消息的代码
export class FeedService { private static instance: FeedService; private pubsubClient: PubSub; private feedTopic: Topic; private maxMessages = 50; private maxMilliseconds = 10000; private messagePromises: Promise<string>[] = []; constructor() { this.pubsubClient = new PubSub({ projectId: process.env.PROJECT_ID, credentials: { client_email: process.env.GOOGLE_CLOUD_CLIENT_EMAIL, private_key: process.env.GOOGLE_CLOUD_CLIENT_PRIVATE_KEY, }, }); } public static getInstance() { if (!FeedService.instance) { FeedService.instance = new FeedService(); } return FeedService.instance; } // provide global topic instance private getFeedTopic() { if (!this.feedTopic) { this.feedTopic = this.pubsubClient.topic(process.env.FEED_TOPIC_ID, { batching: { maxMessages: this.maxMessages, maxMilliseconds: this.maxMilliseconds, }, }); } return this.feedTopic; } // Calling this function to publish messages async pushNotificationForAggregation(payload: FeedTypes.FeedQueuePayload) { try { const messageOptions: MessageOptions = { json: payload, publishTime: { seconds: Date.now() / 1000, }, }; // store promises this.messagePromises.push( this.getFeedTopic().publishMessage(messageOptions) ); return { success: true }; } catch (error: any) { logger.error( `[Failed to push feed to queue] [${payload.userId}]: ${error?.message}` ); return { success: false }; } } // await all promises before sending HTTP response async processRemainingMessages() { try { const messages = await Promise.all(this.messagePromises); return { success: true, messages }; } catch (error: any) { logger.error( `[Failed to process remaining messages] : ${error?.message}` ); return { success: false }; } } }
更新
将相同代码部署到Cloud Run上运行,一切正常,因此可以确认代码本身没有问题。
内容的提问来源于stack exchange,提问作者Alish Satani
相关产品推荐
相关产品推荐

