Skip to content

Commit af0fdf4

Browse files
committed
Some cleanup
1 parent 594fd90 commit af0fdf4

File tree

2 files changed

+101
-105
lines changed

2 files changed

+101
-105
lines changed

src/Components/Server/src/Circuits/CircuitHost.cs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -782,26 +782,22 @@ internal Task UpdateRootComponents(
782782
switch (operation.Type)
783783
{
784784
case RootComponentOperationType.Add:
785+
var task = webRootComponentManager.AddRootComponentAsync(
786+
operation.SsrComponentId,
787+
operation.Descriptor.ComponentType,
788+
operation.Descriptor.Key,
789+
operation.Descriptor.Parameters);
790+
if (pendingTasks != null)
785791
{
786-
var task = webRootComponentManager.AddRootComponentAsync(
787-
operation.SsrComponentId,
788-
operation.Descriptor.ComponentType,
789-
operation.Descriptor.Key,
790-
operation.Descriptor.Parameters);
791-
if (pendingTasks != null)
792-
{
793-
pendingTasks[i] = task;
794-
}
792+
pendingTasks[i] = task;
795793
}
796794
break;
797795
case RootComponentOperationType.Update:
798-
{
799-
// We don't need to await component updates as any unhandled exception will be reported and terminate the circuit.
800-
_ = webRootComponentManager.UpdateRootComponentAsync(
801-
operation.SsrComponentId,
802-
operation.Descriptor.Key,
803-
operation.Descriptor.Parameters);
804-
}
796+
// We don't need to await component updates as any unhandled exception will be reported and terminate the circuit.
797+
_ = webRootComponentManager.UpdateRootComponentAsync(
798+
operation.SsrComponentId,
799+
operation.Descriptor.Key,
800+
operation.Descriptor.Parameters);
805801
break;
806802
case RootComponentOperationType.Remove:
807803
webRootComponentManager.RemoveRootComponent(operation.SsrComponentId);

src/Components/Server/src/Circuits/ServerComponentDeserializer.cs

Lines changed: 89 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -142,72 +142,6 @@ public bool TryDeserializeComponentDescriptorCollection(string serializedCompone
142142
return true;
143143
}
144144

145-
public bool TryDeserializeRootComponentOperations(string serializedComponentOperations, [NotNullWhen(true)] out CircuitRootComponentOperation[]? operations)
146-
{
147-
int[]? seenComponentIdsStorage = null;
148-
try
149-
{
150-
var result = JsonSerializer.Deserialize<RootComponentOperation[]>(
151-
serializedComponentOperations,
152-
ServerComponentSerializationSettings.JsonSerializationOptions);
153-
154-
operations = new CircuitRootComponentOperation[result.Length];
155-
156-
Span<int> seenSsrComponentIds = result.Length <= 128
157-
? stackalloc int[result.Length]
158-
: (seenComponentIdsStorage = ArrayPool<int>.Shared.Rent(result.Length)).AsSpan(0, result.Length);
159-
var currentSsrComponentIdIndex = 0;
160-
for (var i = 0; i < result.Length; i++)
161-
{
162-
var operation = result[i];
163-
if (seenSsrComponentIds[0..currentSsrComponentIdIndex].Contains(operation.SsrComponentId))
164-
{
165-
Log.InvalidRootComponentOperation(_logger, operation.Type, message: "Duplicate component ID.");
166-
operations = null;
167-
return false;
168-
}
169-
170-
seenSsrComponentIds[currentSsrComponentIdIndex++] = operation.SsrComponentId;
171-
172-
if (operation.Type == RootComponentOperationType.Remove)
173-
{
174-
operations[i] = new(operation, null);
175-
continue;
176-
}
177-
178-
if (operation.Marker == null)
179-
{
180-
Log.InvalidRootComponentOperation(_logger, operation.Type, message: "Missing marker.");
181-
operations = null;
182-
return false;
183-
}
184-
185-
if (!TryDeserializeWebRootComponentDescriptor(operation.Marker.Value, out var descriptor))
186-
{
187-
operations = null;
188-
return false;
189-
}
190-
191-
operations[i] = new(operation, descriptor);
192-
}
193-
194-
return true;
195-
}
196-
catch (Exception ex)
197-
{
198-
Log.FailedToProcessRootComponentOperations(_logger, ex);
199-
operations = null;
200-
return false;
201-
}
202-
finally
203-
{
204-
if (seenComponentIdsStorage != null)
205-
{
206-
ArrayPool<int>.Shared.Return(seenComponentIdsStorage);
207-
}
208-
}
209-
}
210-
211145
public bool TryDeserializeWebRootComponentDescriptor(ComponentMarker record, [NotNullWhen(true)] out WebRootComponentDescriptor? result)
212146
{
213147
result = default;
@@ -263,26 +197,25 @@ public bool TryDeserializeWebRootComponentDescriptor(ComponentMarker record, [No
263197
return true;
264198
}
265199

266-
private (ComponentDescriptor, ServerComponent) DeserializeComponentDescriptor(ComponentMarker record)
200+
private bool TryDeserializeComponentTypeAndParameters(ServerComponent serverComponent, [NotNullWhen(true)] out Type? componentType, out ParameterView parameters)
267201
{
268-
if (!TryDeserializeServerComponent(record, out var serverComponent))
202+
parameters = default;
203+
componentType = _rootComponentTypeCache
204+
.GetRootComponent(serverComponent.AssemblyName, serverComponent.TypeName);
205+
206+
if (componentType == null)
269207
{
270-
return default;
208+
Log.FailedToFindComponent(_logger, serverComponent.TypeName, serverComponent.AssemblyName);
209+
return false;
271210
}
272211

273-
if (!TryDeserializeComponentTypeAndParameters(serverComponent, out var componentType, out var parameters))
212+
if (!_parametersDeserializer.TryDeserializeParameters(serverComponent.ParameterDefinitions, serverComponent.ParameterValues, out parameters))
274213
{
214+
// TryDeserializeParameters does appropriate logging.
275215
return default;
276216
}
277217

278-
var componentDescriptor = new ComponentDescriptor
279-
{
280-
ComponentType = componentType,
281-
Parameters = parameters,
282-
Sequence = serverComponent.Sequence,
283-
};
284-
285-
return (componentDescriptor, serverComponent);
218+
return true;
286219
}
287220

288221
private bool TryDeserializeServerComponent(ComponentMarker record, out ServerComponent result)
@@ -330,25 +263,92 @@ private bool TryDeserializeServerComponent(ComponentMarker record, out ServerCom
330263
}
331264
}
332265

333-
private bool TryDeserializeComponentTypeAndParameters(ServerComponent serverComponent, [NotNullWhen(true)] out Type? componentType, out ParameterView parameters)
266+
private (ComponentDescriptor, ServerComponent) DeserializeComponentDescriptor(ComponentMarker record)
334267
{
335-
parameters = default;
336-
componentType = _rootComponentTypeCache
337-
.GetRootComponent(serverComponent.AssemblyName, serverComponent.TypeName);
338-
339-
if (componentType == null)
268+
if (!TryDeserializeServerComponent(record, out var serverComponent))
340269
{
341-
Log.FailedToFindComponent(_logger, serverComponent.TypeName, serverComponent.AssemblyName);
342-
return false;
270+
return default;
343271
}
344272

345-
if (!_parametersDeserializer.TryDeserializeParameters(serverComponent.ParameterDefinitions, serverComponent.ParameterValues, out parameters))
273+
if (!TryDeserializeComponentTypeAndParameters(serverComponent, out var componentType, out var parameters))
346274
{
347-
// TryDeserializeParameters does appropriate logging.
348275
return default;
349276
}
350277

351-
return true;
278+
var componentDescriptor = new ComponentDescriptor
279+
{
280+
ComponentType = componentType,
281+
Parameters = parameters,
282+
Sequence = serverComponent.Sequence,
283+
};
284+
285+
return (componentDescriptor, serverComponent);
286+
}
287+
288+
public bool TryDeserializeRootComponentOperations(string serializedComponentOperations, [NotNullWhen(true)] out CircuitRootComponentOperation[]? operations)
289+
{
290+
int[]? seenComponentIdsStorage = null;
291+
try
292+
{
293+
var result = JsonSerializer.Deserialize<RootComponentOperation[]>(
294+
serializedComponentOperations,
295+
ServerComponentSerializationSettings.JsonSerializationOptions);
296+
297+
operations = new CircuitRootComponentOperation[result.Length];
298+
299+
Span<int> seenSsrComponentIds = result.Length <= 128
300+
? stackalloc int[result.Length]
301+
: (seenComponentIdsStorage = ArrayPool<int>.Shared.Rent(result.Length)).AsSpan(0, result.Length);
302+
var currentSsrComponentIdIndex = 0;
303+
for (var i = 0; i < result.Length; i++)
304+
{
305+
var operation = result[i];
306+
if (seenSsrComponentIds[0..currentSsrComponentIdIndex].Contains(operation.SsrComponentId))
307+
{
308+
Log.InvalidRootComponentOperation(_logger, operation.Type, message: "Duplicate component ID.");
309+
operations = null;
310+
return false;
311+
}
312+
313+
seenSsrComponentIds[currentSsrComponentIdIndex++] = operation.SsrComponentId;
314+
315+
if (operation.Type == RootComponentOperationType.Remove)
316+
{
317+
operations[i] = new(operation, null);
318+
continue;
319+
}
320+
321+
if (operation.Marker == null)
322+
{
323+
Log.InvalidRootComponentOperation(_logger, operation.Type, message: "Missing marker.");
324+
operations = null;
325+
return false;
326+
}
327+
328+
if (!TryDeserializeWebRootComponentDescriptor(operation.Marker.Value, out var descriptor))
329+
{
330+
operations = null;
331+
return false;
332+
}
333+
334+
operations[i] = new(operation, descriptor);
335+
}
336+
337+
return true;
338+
}
339+
catch (Exception ex)
340+
{
341+
Log.FailedToProcessRootComponentOperations(_logger, ex);
342+
operations = null;
343+
return false;
344+
}
345+
finally
346+
{
347+
if (seenComponentIdsStorage != null)
348+
{
349+
ArrayPool<int>.Shared.Return(seenComponentIdsStorage);
350+
}
351+
}
352352
}
353353

354354
private static partial class Log

0 commit comments

Comments
 (0)