firebase-admin-go
firebase-admin-go copied to clipboard
fix(fcm): Optimize `SendEachInBatch` with worker pool
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.Responsescorrectly matches the order of the input messages, even with concurrent processing. - Updated unit tests in
messaging_batch_test.goto 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.