Skip to content

Commit

Permalink
Added LastOperationResource.RetryAfter
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianeicher committed Mar 6, 2024
1 parent 6644364 commit d3d3e69
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/Model/LastOperationResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ public class LastOperationResource : StatusBase, IEquatable<LastOperationResourc
[JsonConverter(typeof(StringEnumConverter))]
public LastOperationResourceState State { get; set; }

/// <summary>
/// Indicates how long the platform should wait before polling again.
/// Only used on server-side. See <c>LastOperationEndpoint.PollingInterval</c> instead on client-side.
/// </summary>
[JsonIgnore]
public TimeSpan? RetryAfter { get; set; }

public bool Equals(LastOperationResource other)
=> other != null
&& base.Equals(other)
Expand Down
7 changes: 6 additions & 1 deletion src/Server/Bindings/ServiceBindingsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ public Task<IActionResult> GetLastOperation(
var context = Context(instanceId, bindingId);
return Do(acceptsIncomplete: true,
blocking: _ => throw new NotSupportedException("This server does not support asynchronous operations."),
deferred: async x => Ok(await x.GetLastOperationAsync(context, serviceId, planId, operation)));
deferred: async x =>
{
var resource = await x.GetLastOperationAsync(context, serviceId, planId, operation);
if (resource.RetryAfter is { } retryAfter) SetRetryAfter(retryAfter);
return Ok(resource);
});
}

private ServiceBindingContext Context(string instanceId, string bindingId)
Expand Down
9 changes: 9 additions & 0 deletions src/Server/BrokerControllerBase.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Globalization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using OpenServiceBroker.Errors;
Expand Down Expand Up @@ -98,4 +99,12 @@ protected OriginatingIdentity? OriginatingIdentity
return string.IsNullOrEmpty(headerValue) ? null : OriginatingIdentity.Parse(headerValue);
}
}

/// <summary>
/// Sets the Retry-After response header.
/// </summary>
protected void SetRetryAfter(TimeSpan retryAfter)
{
HttpContext.Response.Headers.RetryAfter = retryAfter.TotalSeconds.ToString("0", CultureInfo.InvariantCulture);
}
}
7 changes: 6 additions & 1 deletion src/Server/Instances/ServiceInstancesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ public Task<IActionResult> GetLastOperation(
var context = Context(instanceId);
return Do(acceptsIncomplete: true,
blocking: _ => throw new NotSupportedException("This server does not support asynchronous operations."),
deferred: async x => Ok(await x.GetLastOperationAsync(context, serviceId, planId, operation)));
deferred: async x =>
{
var resource = await x.GetLastOperationAsync(context, serviceId, planId, operation);
if (resource.RetryAfter is { } retryAfter) SetRetryAfter(retryAfter);
return Ok(resource);
});
}

private ServiceInstanceContext Context(string? instanceId)
Expand Down
9 changes: 6 additions & 3 deletions src/UnitTests/Bindings/ServiceBindingDeferredFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,16 @@ public async Task GetLastOperation()
{
var response = new LastOperationResource
{
State = LastOperationResourceState.InProgress
State = LastOperationResourceState.InProgress,
RetryAfter = TimeSpan.FromSeconds(5)
};

Mock.Setup(x => x.GetLastOperationAsync(new("123", "456"), "abc", "xyz", "my operation"))
.ReturnsAsync(response);
var result = await Client.ServiceInstancesDeferred["123"].ServiceBindings["456"].LastOperation("abc", "xyz", "my operation").ReadAsync();

var endpoint = Client.ServiceInstancesDeferred["123"].ServiceBindings["456"].LastOperation("abc", "xyz", "my operation");
var result = await endpoint.ReadAsync();
result.Should().BeEquivalentTo(response);
endpoint.PollingInterval.Should().Be(TimeSpan.FromSeconds(5));
}

[Fact]
Expand Down

0 comments on commit d3d3e69

Please sign in to comment.