Skip to content

Commit b1323ee

Browse files
committed
Speed-up initial webhook response times by going lazy init
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.
1 parent a79ffd3 commit b1323ee

15 files changed

+184
-16
lines changed

.netconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,8 @@
168168
sha = cf76df0d6a218c26ebe117339fe3445050b0532a
169169
etag = aed711a45e051edfddfcb76d9f8021d30f9817c342cfe8d1cc38f2af37b47aa8
170170
weak
171+
[file "src/WhatsApp/Extensions/FunctionContextAccessor.cs"]
172+
url = https://github.com/devlooped/catbag/blob/main/Microsoft/Azure/Functions/Worker/FunctionContextAccessor.cs
173+
sha = 91ea14062eb6ac6b9dc85d41b90fdbb56c46efbf
174+
etag = 3d9f39632c0a896dea7aa0cebcef77a7b668fdedfd8d2c8bf79826fa3a85121f
175+
weak

readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ To pay the Maintenance Fee, [become a Sponsor](https://github.com/sponsors/devlo
3030
var builder = FunctionsApplication.CreateBuilder(args);
3131
builder.ConfigureFunctionsWebApplication();
3232

33+
builder.UseWhatsApp(); // 👈 setup middleware
34+
35+
// add your messages handler here 👇
3336
builder.Services.AddWhatsApp<MyWhatsAppHandler>();
3437

3538
builder.Build().Run();

src/SampleApp/Sample/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
storage :
4949
CloudStorageAccount.Parse(builder.Configuration["AzureWebJobsStorage"]));
5050

51+
builder.UseWhatsApp();
52+
5153
var whatsapp = builder.Services
5254
.AddWhatsApp<ProcessHandler>(configure: options =>
5355
{

src/Tests/IntegrationTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using Microsoft.Extensions.Configuration;
1+
using Microsoft.Azure.Functions.Worker;
2+
using Microsoft.Extensions.Configuration;
23
using Microsoft.Extensions.DependencyInjection;
4+
using Moq;
35

46
namespace Devlooped.WhatsApp;
57

@@ -35,6 +37,7 @@ public async Task RunConversationAsync()
3537
.Build();
3638

3739
var services = new ServiceCollection()
40+
.AddSingleton<IFunctionContextAccessor>(Mock.Of<IFunctionContextAccessor>())
3841
.AddSingleton<IConfiguration>(configuration)
3942
.AddSingleton<IConversationStorage>(new TestConversationStorage(CloudStorageAccount.DevelopmentStorageAccount));
4043

src/Tests/PipelineTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Runtime.CompilerServices;
2+
using Microsoft.Azure.Functions.Worker;
23
using Microsoft.Extensions.Configuration;
34
using Microsoft.Extensions.DependencyInjection;
45
using Moq;
@@ -122,6 +123,7 @@ public async Task ConversationCalledAfterCustom()
122123
.Callback(() => order.Add("storage:save"));
123124

124125
var services = new ServiceCollection()
126+
.AddSingleton<IFunctionContextAccessor>(Mock.Of<IFunctionContextAccessor>())
125127
.AddSingleton<IConfiguration>(configuration)
126128
.AddSingleton(conversation.Object);
127129

@@ -183,6 +185,7 @@ public async Task ConversationRestored()
183185
});
184186

185187
var services = new ServiceCollection()
188+
.AddSingleton<IFunctionContextAccessor>(Mock.Of<IFunctionContextAccessor>())
186189
.AddSingleton<IConfiguration>(configuration)
187190
.AddSingleton<IConversationStorage>(storage);
188191

@@ -237,6 +240,7 @@ public async Task CanSendMessagesThroughPipeline()
237240
});
238241

239242
var services = new ServiceCollection()
243+
.AddSingleton<IFunctionContextAccessor>(Mock.Of<IFunctionContextAccessor>())
240244
.AddSingleton<IConfiguration>(configuration);
241245

242246
var sent = 0;

src/WhatsApp/AzureFunctionsConsole.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Devlooped.WhatsApp;
1414
/// </summary>
1515
class AzureFunctionsConsole(
1616
IWhatsAppClient client,
17-
IWhatsAppHandler handler,
17+
Func<IWhatsAppHandler> handler,
1818
ILogger<AzureFunctionsWebhook> logger,
1919
IHostEnvironment environment)
2020
{
@@ -70,7 +70,7 @@ public async Task<IActionResult> MessageConsole([HttpTrigger(AuthorizationLevel.
7070

7171
// Await all responses
7272
// No action needed, just make sure all items are processed
73-
_ = Task.Run(() => handler.HandleAsync([message]).ToArrayAsync().AsTask()).Ignore();
73+
_ = Task.Run(() => handler().HandleAsync([message]).ToArrayAsync().AsTask()).Ignore();
7474
}
7575
else
7676
{

src/WhatsApp/AzureFunctionsProcessors.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
namespace Devlooped.WhatsApp;
1010

11-
class AzureFunctionsProcessors(PipelineRunner runner, IOptions<WhatsAppOptions> options)
11+
class AzureFunctionsProcessors(Func<PipelineRunner> runner, IOptions<WhatsAppOptions> options)
1212
{
1313
readonly WhatsAppOptions options = options.Value;
1414

1515
[Function("whatsapp_dequeue")]
1616
public Task DequeueAsync([QueueTrigger("whatsappwebhook", Connection = "AzureWebJobsStorage")] string json)
17-
=> runner.ProcessAsync(json);
17+
=> runner().ProcessAsync(json);
1818

1919
[Function("whatsapp_eventgrid")]
2020
public async Task<IActionResult> HandleEventGrid(
@@ -25,7 +25,7 @@ public async Task<IActionResult> HandleEventGrid(
2525
[Microsoft.Azure.Functions.Worker.Http.FromBody] EventGridEvent e)
2626
#endif
2727
{
28-
await runner.ProcessAsync(Regex.Unescape(e.Data.ToString()).Trim('"'));
28+
await runner().ProcessAsync(Regex.Unescape(e.Data.ToString()).Trim('"'));
2929
return new OkResult();
3030
}
3131

@@ -41,7 +41,7 @@ public async Task<IActionResult> ProcessAsync(
4141
using var reader = new StreamReader(req.Body, Encoding.UTF8);
4242
var json = await reader.ReadToEndAsync();
4343

44-
await runner.ProcessAsync(json);
44+
await runner().ProcessAsync(json);
4545
return new OkResult();
4646
}
4747
}

src/WhatsApp/AzureFunctionsWebhook.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace Devlooped.WhatsApp;
2424
class AzureFunctionsWebhook(
2525
IMessageProcessor messageProcessor,
2626
IWhatsAppClient whatsapp,
27-
IWhatsAppHandler handler,
27+
Func<IWhatsAppHandler> handler,
2828
IOptions<MetaOptions> metaOptions,
2929
IOptions<WhatsAppOptions> functionOptions,
3030
IHostEnvironment hosting,
@@ -116,7 +116,7 @@ async Task<IActionResult> ProcessFlowDataAsync(string json, EncryptedFlowData en
116116

117117
FlowDataResponse? flowResponse = default;
118118

119-
await foreach (var response in handler.HandleAsync([flow]))
119+
await foreach (var response in handler().HandleAsync([flow]))
120120
{
121121
if (response is FlowDataResponse fdr)
122122
{
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// <auto-generated />
2+
#region License
3+
// MIT License
4+
//
5+
// Copyright (c) Daniel Cazzulino
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
// SOFTWARE.
24+
#endregion
25+
26+
#nullable enable
27+
28+
using System.Diagnostics;
29+
using System.Threading.Tasks;
30+
using System.Threading;
31+
using Microsoft.Azure.Functions.Worker;
32+
using Microsoft.Azure.Functions.Worker.Middleware;
33+
using Microsoft.Extensions.DependencyInjection;
34+
35+
// Follows implementation of HttpContextAccessor at https://github.com/dotnet/aspnetcore/blob/main/src/Http/Http/src/HttpContextAccessor.cs
36+
37+
namespace Microsoft.Azure.Functions.Worker
38+
{
39+
/// <summary>
40+
/// Provides access to the current <see cref="FunctionContext"/>, if one is available.
41+
/// </summary>
42+
public interface IFunctionContextAccessor
43+
{
44+
/// <summary>
45+
/// Gets or sets the current <see cref="FunctionContext"/>.
46+
/// Returns <see langword="null" /> if there is no active <see cref="FunctionContext" />.
47+
/// </summary>
48+
FunctionContext? FunctionContext { get; set; }
49+
}
50+
}
51+
52+
namespace Microsoft.Extensions.Hosting
53+
{
54+
/// <summary>
55+
/// Extension method to allow access to the current <see cref="FunctionContext"/>
56+
/// from dependency injection.
57+
/// </summary>
58+
public static class FunctionContextAccessorExtensions
59+
{
60+
/// <summary>
61+
/// Adds a default implementation for the <see cref="IFunctionContextAccessor"/> service.
62+
/// </summary>
63+
public static IFunctionsWorkerApplicationBuilder UseFunctionContextAccessor(this IFunctionsWorkerApplicationBuilder builder)
64+
{
65+
builder.UseMiddleware<FunctionContextAccessorMiddleware>();
66+
builder.Services.AddSingleton<IFunctionContextAccessor, FunctionContextAccessor>();
67+
return builder;
68+
}
69+
}
70+
71+
[DebuggerDisplay("FunctionContext = {FunctionContext}")]
72+
class FunctionContextAccessor : IFunctionContextAccessor
73+
{
74+
static readonly AsyncLocal<FunctionContextHolder> current = new AsyncLocal<FunctionContextHolder>();
75+
76+
public virtual FunctionContext? FunctionContext
77+
{
78+
get => current.Value?.Context;
79+
set
80+
{
81+
var holder = current.Value;
82+
if (holder != null)
83+
{
84+
// Clear current context trapped in the AsyncLocals, as its done.
85+
holder.Context = default;
86+
}
87+
88+
if (value != null)
89+
{
90+
// Use an object indirection to hold the context in the AsyncLocal,
91+
// so it can be cleared in all ExecutionContexts when its cleared.
92+
current.Value = new FunctionContextHolder { Context = value };
93+
}
94+
}
95+
}
96+
97+
class FunctionContextHolder
98+
{
99+
public FunctionContext? Context;
100+
}
101+
}
102+
103+
class FunctionContextAccessorMiddleware(IFunctionContextAccessor accessor) : IFunctionsWorkerMiddleware
104+
{
105+
public Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
106+
{
107+
accessor.FunctionContext = context;
108+
return next(context);
109+
}
110+
}
111+
}

src/WhatsApp/PipelineRunner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Devlooped.WhatsApp;
77
class PipelineRunner(
88
Idempotency idempotency,
99
IWhatsAppClient whatsapp,
10-
IWhatsAppHandler handler,
10+
Func<IWhatsAppHandler> handler,
1111
IOptions<WhatsAppOptions> functionOptions,
1212
ILogger<PipelineRunner> logger)
1313
{
@@ -45,7 +45,7 @@ public async Task ProcessAsync(string json)
4545
{
4646
// Await all responses
4747
// No action needed, just make sure all items are processed
48-
await handler.HandleAsync([message]).ToArrayAsync();
48+
await handler().HandleAsync([message]).ToArrayAsync();
4949
logger.LogInformation($"Completed work item: {message.Id}");
5050
}
5151
catch (Exception e)

0 commit comments

Comments
 (0)