1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 14:17:26 +08:00

Merge pull request #3838 from peppy/fix-api-failure-fail

Fix API getting stuck in connecting state on some exceptions
This commit is contained in:
Dean Herbert 2018-12-14 19:40:28 +09:00 committed by GitHub
commit 0610c34c98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -199,6 +199,12 @@ namespace osu.Game.Online.API
try try
{ {
Logger.Log($@"Performing request {req}", LoggingTarget.Network); Logger.Log($@"Performing request {req}", LoggingTarget.Network);
req.Failure += ex =>
{
if (ex is WebException we)
handleWebException(we);
};
req.Perform(this); req.Perform(this);
//we could still be in initialisation, at which point we don't want to say we're Online yet. //we could still be in initialisation, at which point we don't want to say we're Online yet.
@ -210,37 +216,12 @@ namespace osu.Game.Online.API
} }
catch (WebException we) catch (WebException we)
{ {
HttpStatusCode statusCode = (we.Response as HttpWebResponse)?.StatusCode var removeFromQueue = handleWebException(we);
?? (we.Status == WebExceptionStatus.UnknownError ? HttpStatusCode.NotAcceptable : HttpStatusCode.RequestTimeout);
// special cases for un-typed but useful message responses. if (removeFromQueue)
switch (we.Message) req.Fail(we);
{
case "Unauthorized":
statusCode = HttpStatusCode.Unauthorized;
break;
}
switch (statusCode) return removeFromQueue;
{
case HttpStatusCode.Unauthorized:
Logout(false);
return true;
case HttpStatusCode.RequestTimeout:
failureCount++;
log.Add($@"API failure count is now {failureCount}");
if (failureCount < 3)
//we might try again at an api level.
return false;
State = APIState.Failing;
flushQueue();
return true;
}
req.Fail(we);
return true;
} }
catch (Exception e) catch (Exception e)
{ {
@ -276,6 +257,41 @@ namespace osu.Game.Online.API
} }
} }
private bool handleWebException(WebException we)
{
HttpStatusCode statusCode = (we.Response as HttpWebResponse)?.StatusCode
?? (we.Status == WebExceptionStatus.UnknownError ? HttpStatusCode.NotAcceptable : HttpStatusCode.RequestTimeout);
// special cases for un-typed but useful message responses.
switch (we.Message)
{
case "Unauthorized":
case "Forbidden":
statusCode = HttpStatusCode.Unauthorized;
break;
}
switch (statusCode)
{
case HttpStatusCode.Unauthorized:
Logout(false);
return true;
case HttpStatusCode.RequestTimeout:
failureCount++;
log.Add($@"API failure count is now {failureCount}");
if (failureCount < 3)
//we might try again at an api level.
return false;
State = APIState.Failing;
flushQueue();
return true;
}
return true;
}
public bool IsLoggedIn => LocalUser.Value.Id > 1; public bool IsLoggedIn => LocalUser.Value.Id > 1;
public void Queue(APIRequest request) public void Queue(APIRequest request)