Skip to content

Commit d3d3e69

Browse files
committed
Added LastOperationResource.RetryAfter
1 parent 6644364 commit d3d3e69

5 files changed

Lines changed: 34 additions & 5 deletions

File tree

src/Model/LastOperationResource.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ public class LastOperationResource : StatusBase, IEquatable<LastOperationResourc
1616
[JsonConverter(typeof(StringEnumConverter))]
1717
public LastOperationResourceState State { get; set; }
1818

19+
/// <summary>
20+
/// Indicates how long the platform should wait before polling again.
21+
/// Only used on server-side. See <c>LastOperationEndpoint.PollingInterval</c> instead on client-side.
22+
/// </summary>
23+
[JsonIgnore]
24+
public TimeSpan? RetryAfter { get; set; }
25+
1926
public bool Equals(LastOperationResource other)
2027
=> other != null
2128
&& base.Equals(other)

src/Server/Bindings/ServiceBindingsController.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,12 @@ public Task<IActionResult> GetLastOperation(
139139
var context = Context(instanceId, bindingId);
140140
return Do(acceptsIncomplete: true,
141141
blocking: _ => throw new NotSupportedException("This server does not support asynchronous operations."),
142-
deferred: async x => Ok(await x.GetLastOperationAsync(context, serviceId, planId, operation)));
142+
deferred: async x =>
143+
{
144+
var resource = await x.GetLastOperationAsync(context, serviceId, planId, operation);
145+
if (resource.RetryAfter is { } retryAfter) SetRetryAfter(retryAfter);
146+
return Ok(resource);
147+
});
143148
}
144149

145150
private ServiceBindingContext Context(string instanceId, string bindingId)

src/Server/BrokerControllerBase.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Globalization;
12
using Microsoft.AspNetCore.Mvc;
23
using Microsoft.Extensions.DependencyInjection;
34
using OpenServiceBroker.Errors;
@@ -98,4 +99,12 @@ protected OriginatingIdentity? OriginatingIdentity
9899
return string.IsNullOrEmpty(headerValue) ? null : OriginatingIdentity.Parse(headerValue);
99100
}
100101
}
102+
103+
/// <summary>
104+
/// Sets the Retry-After response header.
105+
/// </summary>
106+
protected void SetRetryAfter(TimeSpan retryAfter)
107+
{
108+
HttpContext.Response.Headers.RetryAfter = retryAfter.TotalSeconds.ToString("0", CultureInfo.InvariantCulture);
109+
}
101110
}

src/Server/Instances/ServiceInstancesController.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,12 @@ public Task<IActionResult> GetLastOperation(
168168
var context = Context(instanceId);
169169
return Do(acceptsIncomplete: true,
170170
blocking: _ => throw new NotSupportedException("This server does not support asynchronous operations."),
171-
deferred: async x => Ok(await x.GetLastOperationAsync(context, serviceId, planId, operation)));
171+
deferred: async x =>
172+
{
173+
var resource = await x.GetLastOperationAsync(context, serviceId, planId, operation);
174+
if (resource.RetryAfter is { } retryAfter) SetRetryAfter(retryAfter);
175+
return Ok(resource);
176+
});
172177
}
173178

174179
private ServiceInstanceContext Context(string? instanceId)

src/UnitTests/Bindings/ServiceBindingDeferredFacts.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,16 @@ public async Task GetLastOperation()
129129
{
130130
var response = new LastOperationResource
131131
{
132-
State = LastOperationResourceState.InProgress
132+
State = LastOperationResourceState.InProgress,
133+
RetryAfter = TimeSpan.FromSeconds(5)
133134
};
134-
135135
Mock.Setup(x => x.GetLastOperationAsync(new("123", "456"), "abc", "xyz", "my operation"))
136136
.ReturnsAsync(response);
137-
var result = await Client.ServiceInstancesDeferred["123"].ServiceBindings["456"].LastOperation("abc", "xyz", "my operation").ReadAsync();
137+
138+
var endpoint = Client.ServiceInstancesDeferred["123"].ServiceBindings["456"].LastOperation("abc", "xyz", "my operation");
139+
var result = await endpoint.ReadAsync();
138140
result.Should().BeEquivalentTo(response);
141+
endpoint.PollingInterval.Should().Be(TimeSpan.FromSeconds(5));
139142
}
140143

141144
[Fact]

0 commit comments

Comments
 (0)