-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathProtocolOperationHelpers.cs
More file actions
222 lines (194 loc) · 9.95 KB
/
ProtocolOperationHelpers.cs
File metadata and controls
222 lines (194 loc) · 9.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#nullable enable
using System;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core.Pipeline;
namespace Azure.Core
{
internal static class ProtocolOperationHelpers
{
public static Operation<TTo> Convert<TFrom, TTo>(Operation<TFrom> operation, Func<Response, TTo> convertFunc, ClientDiagnostics diagnostics, string scopeName)
where TFrom : notnull
where TTo : notnull
=> new ConvertOperation<TFrom, TTo>(operation, diagnostics, scopeName, convertFunc);
public static ValueTask<Operation<VoidValue>> ProcessMessageWithoutResponseValueAsync(HttpPipeline pipeline, HttpMessage message, ClientDiagnostics clientDiagnostics, string scopeName, OperationFinalStateVia finalStateVia, RequestContext? requestContext, WaitUntil waitUntil)
=> ProcessMessageAsync(pipeline, message, clientDiagnostics, scopeName, finalStateVia, requestContext, waitUntil, _ => new VoidValue());
public static Operation<VoidValue> ProcessMessageWithoutResponseValue(HttpPipeline pipeline, HttpMessage message, ClientDiagnostics clientDiagnostics, string scopeName, OperationFinalStateVia finalStateVia, RequestContext? requestContext, WaitUntil waitUntil)
=> ProcessMessage(pipeline, message, clientDiagnostics, scopeName, finalStateVia, requestContext, waitUntil, _ => new VoidValue());
public static ValueTask<Operation<BinaryData>> ProcessMessageAsync(HttpPipeline pipeline, HttpMessage message, ClientDiagnostics clientDiagnostics, string scopeName, OperationFinalStateVia finalStateVia, RequestContext? requestContext, WaitUntil waitUntil)
=> ProcessMessageAsync(pipeline, message, clientDiagnostics, scopeName, finalStateVia, requestContext, waitUntil, r => r.Content);
public static Operation<BinaryData> ProcessMessage(HttpPipeline pipeline, HttpMessage message, ClientDiagnostics clientDiagnostics, string scopeName, OperationFinalStateVia finalStateVia, RequestContext? requestContext, WaitUntil waitUntil)
=> ProcessMessage(pipeline, message, clientDiagnostics, scopeName, finalStateVia, requestContext, waitUntil, r => r.Content);
public static async ValueTask<Operation<T>> ProcessMessageAsync<T>(HttpPipeline pipeline, HttpMessage message, ClientDiagnostics clientDiagnostics, string scopeName, OperationFinalStateVia finalStateVia, RequestContext? requestContext, WaitUntil waitUntil, Func<Response, T> resultSelector) where T : notnull
{
var response = await pipeline.ProcessMessageAsync(message, requestContext).ConfigureAwait(false);
var operation = new ProtocolOperation<T>(clientDiagnostics, pipeline, message.Request, response, finalStateVia, scopeName, resultSelector);
if (waitUntil == WaitUntil.Completed)
{
await operation.WaitForCompletionAsync(requestContext?.CancellationToken ?? default).ConfigureAwait(false);
}
return operation;
}
public static Operation<T> ProcessMessage<T>(HttpPipeline pipeline, HttpMessage message, ClientDiagnostics clientDiagnostics, string scopeName, OperationFinalStateVia finalStateVia, RequestContext? requestContext, WaitUntil waitUntil, Func<Response, T> resultSelector) where T : notnull
{
var response = pipeline.ProcessMessage(message, requestContext);
var operation = new ProtocolOperation<T>(clientDiagnostics, pipeline, message.Request, response, finalStateVia, scopeName, resultSelector);
if (waitUntil == WaitUntil.Completed)
{
operation.WaitForCompletion(requestContext?.CancellationToken ?? default);
}
return operation;
}
private class ConvertOperation<TFrom, TTo> : Operation<TTo>
where TFrom : notnull
where TTo : notnull
{
private readonly Operation<TFrom> _operation;
private readonly ClientDiagnostics _diagnostics;
private readonly string _waitForCompletionScopeName;
private readonly string _updateStatusScopeName;
private readonly Func<Response, TTo> _convertFunc;
private Response<TTo>? _response;
public ConvertOperation(Operation<TFrom> operation, ClientDiagnostics diagnostics, string operationName, Func<Response, TTo> convertFunc)
{
_operation = operation;
_diagnostics = diagnostics;
_waitForCompletionScopeName = $"{operationName}.{nameof(WaitForCompletion)}";
_updateStatusScopeName = $"{operationName}.{nameof(UpdateStatus)}";
_convertFunc = convertFunc;
_response = null;
}
public override string Id => _operation.Id;
public override TTo Value => GetOrCreateValue();
public override bool HasValue => _operation.HasValue;
public override bool HasCompleted => _operation.HasCompleted;
public override RehydrationToken? GetRehydrationToken() => _operation.GetRehydrationToken();
public override Response GetRawResponse() => _operation.GetRawResponse();
public override Response UpdateStatus(CancellationToken cancellationToken = default)
{
if (HasCompleted)
{
return GetRawResponse();
}
using var scope = CreateScope(_updateStatusScopeName);
try
{
return _operation.UpdateStatus(cancellationToken);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
public override async ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default)
{
if (HasCompleted)
{
return GetRawResponse();
}
using var scope = CreateScope(_updateStatusScopeName);
try
{
return await _operation.UpdateStatusAsync(cancellationToken).ConfigureAwait(false);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
public override Response<TTo> WaitForCompletion(CancellationToken cancellationToken = default)
{
if (_response != null)
{
return _response;
}
using var scope = CreateScope(_waitForCompletionScopeName);
try
{
var result = _operation.WaitForCompletion(cancellationToken);
return CreateResponseOfTTo(result);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
public override Response<TTo> WaitForCompletion(TimeSpan pollingInterval, CancellationToken cancellationToken)
{
if (_response != null)
{
return _response;
}
using var scope = CreateScope(_waitForCompletionScopeName);
try
{
var result = _operation.WaitForCompletion(pollingInterval, cancellationToken);
return CreateResponseOfTTo(result);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
public override async ValueTask<Response<TTo>> WaitForCompletionAsync(CancellationToken cancellationToken = default)
{
if (_response != null)
{
return _response;
}
using var scope = CreateScope(_waitForCompletionScopeName);
try
{
var result = await _operation.WaitForCompletionAsync(cancellationToken).ConfigureAwait(false);
return CreateResponseOfTTo(result);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
public override async ValueTask<Response<TTo>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken cancellationToken)
{
if (_response != null)
{
return _response;
}
using var scope = CreateScope(_waitForCompletionScopeName);
try
{
var result = await _operation.WaitForCompletionAsync(pollingInterval, cancellationToken).ConfigureAwait(false);
return CreateResponseOfTTo(result);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
private TTo GetOrCreateValue()
=> _response != null ? _response.Value : CreateResponseOfTTo(GetRawResponse()).Value;
private Response<TTo> CreateResponseOfTTo(Response<TFrom> responseTFrom)
=> CreateResponseOfTTo(responseTFrom.GetRawResponse());
private Response<TTo> CreateResponseOfTTo(Response rawResponse)
{
var value = _convertFunc(rawResponse);
var response = Response.FromValue(value, rawResponse);
Interlocked.CompareExchange(ref _response, response, null);
return _response;
}
private DiagnosticScope CreateScope(string name)
{
var scope = _diagnostics.CreateScope(name);
scope.Start();
return scope;
}
}
}
}