firebase-admin-go icon indicating copy to clipboard operation
firebase-admin-go copied to clipboard

fix(fcm): Optimize `SendEachInBatch` with worker pool

Open lahirumaramba opened this issue 6 months ago • 0 comments
trafficstars

The sendEachInBatch function, used for sending FCM messages individually within a batch, previously created a new goroutine for each message. This could lead to high CPU usage for large batches.

This commit refactors sendEachInBatch to use a fixed-size pool of concurrent operations (10 operations) to manage message sending. This limits the number of active goroutines, reducing CPU overhead and improving resource utilization.

Key changes:

  • Implemented early validation: All messages are now validated upfront. If any message is invalid, the function returns an error immediately without attempting to send any messages.
  • Introduced a mechanism for managing concurrent operations: Manages a fixed number of goroutines to process message sending tasks. Messages are distributed via channels.
  • Ensured result ordering: The order of responses in BatchResponse.Responses correctly matches the order of the input messages, even with concurrent processing.
  • Updated unit tests in messaging_batch_test.go to comprehensively cover the new implementation, including scenarios for varying batch sizes, partial failures, early validation, and response ordering.
  • Confirmed that the existing HTTP client continues to leverage HTTP/2.

lahirumaramba avatar May 13 '25 22:05 lahirumaramba