[12.x] keep single NotificationSender instance #58452
Merged
+64
−6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR #55507, authored by me, added logic to ensure the
Illuminate\Notifications\Events\NotificationFailedis dispatched whenever a notification failed and also guard against double sending the same event in case a 3rd-party channel was already manually dispatching that event.As noted by discussion #58434, the
Illuminate\Notifications\ChannelManageralwaysnewup a new instance of theIlluminate\Notifications\NotificationSenderclass, and as such, a new listener for theIlluminate\Notifications\Events\NotificationFailedis registered times for each instance.In a case where multiple notifications are sent, but then down the road one fails, those multiple listeners were invoked.
The current approach also prevents garbage collection of the
Illuminate\Notifications\NotificationSenderinstances, as the registered listener would prevent those instances from being collected/destructed.In a scenario where an artisan command is scheduled to send multiple notifications for several users, that could lead to a memory overflow.
This PR
Illuminate\Notifications\NotificationSenderon theIlluminate\Notifications\ChannelManager.Illuminate\Notifications\NotificationSenderinstance is just called once even after multiple notifications were sent.Notes:
Illuminate\Notifications\ChannelManageris already registered as a singleton, that will only instantiate a singleIlluminate\Notifications\NotificationSenderinstance per life cycle.Illuminate\Notifications\NotificationSenderinstance, only one listener is regThanks @macropay-solutions for the heads-up.