Speed-up initial webhook response times by going lazy init#306
Merged
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR implements lazy initialization for WhatsApp webhook handlers to improve initial response times. The key change shifts from eager dependency resolution to lazy factory-based resolution, allowing webhooks to respond immediately while deferring handler pipeline initialization until needed.
- Introduces mandatory
UseWhatsApp()call on the functions application builder (breaking change) - Converts handler and pipeline runner dependencies to
Func<T>factory delegates for lazy initialization - Adds function context accessor middleware to enable proper dependency resolution in isolated worker model
Reviewed Changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| WhatsAppServiceCollectionExtensions.cs | Adds validation for required middleware and registers lazy factory delegates |
| WhatsAppApplicationBuilderExtensions.cs | New extension method to configure required WhatsApp middleware |
| FunctionContextAccessor.cs | New middleware implementation for accessing function context in isolated worker |
| TaskSchedulerProcessor.cs | Updates to use lazy factory for pipeline runner |
| PipelineRunner.cs | Updates to use lazy factory for handler resolution |
| AzureFunctionsWebhook.cs | Updates to use lazy factory for handler resolution |
| AzureFunctionsProcessors.cs | Updates to use lazy factory for pipeline runner |
| AzureFunctionsConsole.cs | Updates to use lazy factory for handler resolution |
| Program.cs | Sample showing required UseWhatsApp() call |
| readme.md | Documentation update for new required setup step |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Member
Author
🧪 Details on Ubuntu 24.04.3 LTSfrom dotnet-retest v0.7.2 on .NET 9.0.9 with 💜 by @devlooped |
Breaking change: we now *require* invoking UseWhatsApp on the functions app builder. We throw an exception if we detect it's misisng: ``` var builder = FunctionsApplication.CreateBuilder(args); builder.ConfigureFunctionsWebApplication(); builder.UseWhatsApp(); ``` When the user's handler pipeline was non-trivial to spin-up, webhook responses were delayed until the full DI graph for it was built. We now instead make that dependency fully lazy so that the webhook can respond immediately and send the typing/read indicators as needed right-away. Both pipeline runner and handler are changed to Func<T> accordingly, and we had to bring the function context accessor to properly retrieve the instances from the right container at run-time in the isolated worker model.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Breaking change: we now require invoking UseWhatsApp on the functions app builder. We throw an exception if we detect it's misisng:
When the user's handler pipeline was non-trivial to spin-up, webhook responses were delayed until the full DI graph for it was built.
We now instead make that dependency fully lazy so that the webhook can respond immediately and send the typing/read indicators as needed right-away.
Both pipeline runner and handler are changed to Func accordingly, and we had to bring the function context accessor to properly retrieve the instances from the right container at run-time in the isolated worker model.