Skip to content

Commit

Permalink
Merge pull request #274 from smoogipoo/remove-interop-retry
Browse files Browse the repository at this point in the history
Retry interop calls only for 5XX responses
  • Loading branch information
bdach authored Mar 3, 2025
2 parents b51fc1f + f5a4158 commit 94959ba
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions osu.Server.Spectator/Services/SharedInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
Expand Down Expand Up @@ -93,6 +94,22 @@ private async Task<string> runCommand(HttpMethod method, string command, dynamic
}
catch (Exception e)
{
if (e is SharedInteropRequestFailedException interopException)
{
switch (interopException.StatusCode)
{
// Allow retry for potentially relevant 5XX responses.
case HttpStatusCode.InternalServerError:
case HttpStatusCode.BadGateway:
case HttpStatusCode.ServiceUnavailable:
case HttpStatusCode.GatewayTimeout:
break;

default:
throw;
}
}

if (retryCount-- > 0)
{
logger.LogError(e, "Shared interop request to {url} failed, retrying ({retries} remaining)", url, retryCount);
Expand Down Expand Up @@ -167,9 +184,12 @@ public RoomWithHostId(MultiplayerRoom room)
[Serializable]
private class SharedInteropRequestFailedException : HubException
{
private SharedInteropRequestFailedException(string message, Exception innerException)
public readonly HttpStatusCode StatusCode;

private SharedInteropRequestFailedException(HttpStatusCode statusCode, string message, Exception innerException)
: base(message, innerException)
{
StatusCode = statusCode;
}

public static async Task<SharedInteropRequestFailedException> Create(string url, HttpResponseMessage response)
Expand All @@ -187,7 +207,8 @@ public static async Task<SharedInteropRequestFailedException> Create(string url,
}

// Outer exception message is serialised to clients, inner exception is logged to the server and NOT serialised to the client.
return new SharedInteropRequestFailedException(errorMessage, new Exception($"Shared interop request to {url} failed with {response.StatusCode} ({response.ReasonPhrase})."));
return new SharedInteropRequestFailedException(response.StatusCode, errorMessage,
new Exception($"Shared interop request to {url} failed with {response.StatusCode} ({response.ReasonPhrase})."));
}

[Serializable]
Expand Down

0 comments on commit 94959ba

Please sign in to comment.