| | | 1 | | namespace AsyncResponse; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Raw broker/webhook response payload that can be materialized into the active waiter's payload |
| | | 5 | | /// type without first allocating an intermediate JsonElement. |
| | | 6 | | /// </summary> |
| | | 7 | | internal sealed class RawJsonResponse |
| | | 8 | | { |
| | | 9 | | private readonly string _json; |
| | | 10 | | |
| | | 11 | | // One instance is shared across every subscriber of a correlation id, and fan-out dispatch can |
| | | 12 | | // materialize payloads from multiple threads. All memoization state below is guarded by _gate: |
| | | 13 | | // an unsynchronized publication could hand one waiter a torn (type set, payload null) pair, and |
| | | 14 | | // a plain Dictionary corrupts under concurrent writes. The uncontended lock is cheap next to |
| | | 15 | | // the deserialization it guards, and allocation behavior is unchanged. |
| | 3 | 16 | | private readonly object _gate = new(); |
| | | 17 | | private Dictionary<Type, object?>? _typedPayloads; |
| | | 18 | | private Type? _singlePayloadType; |
| | | 19 | | private object? _singlePayload; |
| | | 20 | | private object? _untypedPayload; |
| | | 21 | | private bool _hasUntypedPayload; |
| | | 22 | | |
| | | 23 | | /// <summary>Runs the RawJsonResponse operation.</summary> |
| | 3 | 24 | | public RawJsonResponse(string json) |
| | | 25 | | { |
| | 3 | 26 | | JsonSafety.ThrowIfClearlyNotJson(json); |
| | 3 | 27 | | _json = json; |
| | 3 | 28 | | } |
| | | 29 | | |
| | 3 | 30 | | public string Json => _json; |
| | | 31 | | |
| | | 32 | | /// <summary>Runs the DeserializeUntyped operation.</summary> |
| | | 33 | | public object? DeserializeUntyped() |
| | | 34 | | { |
| | 3 | 35 | | lock (_gate) |
| | | 36 | | { |
| | 3 | 37 | | if (_hasUntypedPayload) |
| | 3 | 38 | | return _untypedPayload; |
| | | 39 | | |
| | 2 | 40 | | _untypedPayload = JsonSafety.SafeDeserialize<object?>(_json); |
| | 3 | 41 | | _hasUntypedPayload = true; |
| | 3 | 42 | | return _untypedPayload; |
| | | 43 | | } |
| | 3 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary>Runs the Deserialize operation.</summary> |
| | 3 | 47 | | public T? Deserialize<T>() => (T?)Deserialize(typeof(T)); |
| | | 48 | | |
| | | 49 | | /// <summary>Runs the Deserialize operation.</summary> |
| | | 50 | | public object? Deserialize(Type payloadType) |
| | | 51 | | { |
| | 2 | 52 | | lock (_gate) |
| | | 53 | | { |
| | 2 | 54 | | if (_singlePayloadType == payloadType) |
| | 2 | 55 | | return _singlePayload; |
| | | 56 | | |
| | 2 | 57 | | if (_typedPayloads?.TryGetValue(payloadType, out var cached) == true) |
| | 2 | 58 | | return cached; |
| | | 59 | | |
| | 2 | 60 | | var payload = JsonSafety.SafeDeserialize(_json, payloadType); |
| | 2 | 61 | | if (_singlePayloadType is null) |
| | | 62 | | { |
| | 2 | 63 | | _singlePayloadType = payloadType; |
| | 2 | 64 | | _singlePayload = payload; |
| | 2 | 65 | | return payload; |
| | | 66 | | } |
| | | 67 | | |
| | 2 | 68 | | if (_typedPayloads is null) |
| | | 69 | | { |
| | 2 | 70 | | _typedPayloads = new Dictionary<Type, object?> |
| | 2 | 71 | | { |
| | 2 | 72 | | [_singlePayloadType] = _singlePayload |
| | 2 | 73 | | }; |
| | | 74 | | } |
| | | 75 | | |
| | 2 | 76 | | _typedPayloads.Add(payloadType, payload); |
| | 2 | 77 | | return payload; |
| | | 78 | | } |
| | 2 | 79 | | } |
| | | 80 | | } |