Skip to content

Commit b62d0f8

Browse files
committed
Provide expiration timeout for the serialized descriptors
1 parent f8805b0 commit b62d0f8

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/Components/Server/src/Circuits/CircuitPersistenceManager.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
using Microsoft.AspNetCore.Components.Web;
99
using Microsoft.AspNetCore.DataProtection;
1010
using Microsoft.Extensions.DependencyInjection;
11+
using Microsoft.Extensions.Options;
1112

1213
namespace Microsoft.AspNetCore.Components.Server.Circuits;
1314

1415
internal partial class CircuitPersistenceManager(
16+
IOptions<CircuitOptions> circuitOptions,
1517
ServerComponentSerializer serverComponentSerializer,
1618
ICircuitPersistenceProvider circuitPersistenceProvider,
1719
IDataProtectionProvider dataProtectionProvider)
@@ -40,8 +42,12 @@ private Task PersistRootComponents(RemoteRenderer renderer, PersistentComponentS
4042
var invocation = new ServerComponentInvocationSequence();
4143
foreach (var (id, componentKey, (componentType, parameters)) in components)
4244
{
45+
var distributedRetention = circuitOptions.Value.PersistedCircuitDistributedRetentionPeriod;
46+
var localRetention = circuitOptions.Value.PersistedCircuitInMemoryRetentionPeriod;
47+
var maxRetention = distributedRetention > localRetention ? distributedRetention : localRetention;
48+
4349
var marker = ComponentMarker.Create(ComponentMarker.ServerMarkerType, false, componentKey);
44-
serverComponentSerializer.SerializeInvocation(ref marker, invocation, componentType, parameters);
50+
serverComponentSerializer.SerializeInvocation(ref marker, invocation, componentType, parameters, maxRetention);
4551
persistedComponents.Add(id, marker);
4652
}
4753

src/Shared/Components/ServerComponentSerializer.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,24 @@ public ServerComponentSerializer(IDataProtectionProvider dataProtectionProvider)
1616
.CreateProtector(ServerComponentSerializationSettings.DataProtectionProviderPurpose)
1717
.ToTimeLimitedDataProtector();
1818

19-
public void SerializeInvocation(ref ComponentMarker marker, ServerComponentInvocationSequence invocationId, Type type, ParameterView parameters)
19+
public void SerializeInvocation(
20+
ref ComponentMarker marker,
21+
ServerComponentInvocationSequence invocationId,
22+
Type type,
23+
ParameterView parameters,
24+
TimeSpan? dataExpiration = default)
2025
{
21-
var (sequence, serverComponent) = CreateSerializedServerComponent(invocationId, type, parameters, marker.Key);
26+
var expiration = dataExpiration ?? ServerComponentSerializationSettings.DataExpiration;
27+
var (sequence, serverComponent) = CreateSerializedServerComponent(invocationId, type, parameters, marker.Key, expiration);
2228
marker.WriteServerData(sequence, serverComponent);
2329
}
2430

2531
private (int sequence, string payload) CreateSerializedServerComponent(
2632
ServerComponentInvocationSequence invocationId,
2733
Type rootComponent,
2834
ParameterView parameters,
29-
ComponentMarkerKey? key)
35+
ComponentMarkerKey? key,
36+
TimeSpan dataExpiration)
3037
{
3138
var sequence = invocationId.Next();
3239

@@ -42,7 +49,7 @@ public void SerializeInvocation(ref ComponentMarker marker, ServerComponentInvoc
4249
invocationId.Value);
4350

4451
var serializedServerComponentBytes = JsonSerializer.SerializeToUtf8Bytes(serverComponent, ServerComponentSerializationSettings.JsonSerializationOptions);
45-
var protectedBytes = _dataProtector.Protect(serializedServerComponentBytes, ServerComponentSerializationSettings.DataExpiration);
52+
var protectedBytes = _dataProtector.Protect(serializedServerComponentBytes, dataExpiration);
4653
return (serverComponent.Sequence, Convert.ToBase64String(protectedBytes));
4754
}
4855
}

0 commit comments

Comments
 (0)