Non-idempotent endpoints
HTTP methods that have side effects upon repeat calls are considered to be non-idempotent. This means that if a client were to call the endpoint and a network timeout occurs, it is not safe to retry the method because the resource may have been updated but the network wasn’t able to notify the caller that it was successful. Upon error, instead of retrying, the client must first query to see if the call was successful. Only if the call was not successful then should it retry. In the XBOX Services API, some APIs are internally marked as calling non-idempotent endpoints. This means that if failures occur when calling these endpoints, the APIs will not automatically retry the endpoint. The full list of non-idempotent APIs are:- XblMatchmakingCreateMatchTicketAsync
- XblMultiplayerWriteSessionAsync
- XblMultiplayerWriteSessionByHandleAsync
- XblMultiplayerSendInvitesAsync
- XblSocialSubmitReputationFeedbackAsync
- XblSocialSubmitBatchReputationFeedbackAsync
Idempotent methods
Idempotent HTTP methods on the other hand do not leave side effects. This in turn means they are safe to be retried. In the XBOX Services API, all idempotent methods are automatically retried under certain conditions. The full list of idempotent APIs are all APIs that were not listed above as being non-idempotent.Retry logic Best Practices
For idempotent calls, these conditions should be automatically retried:- All network errors
- 401: Unauthorized
- 408: RequestTimeout
- 429: Too Many Requests
- 500: InternalError
- 502: BadGateway
- 503: ServiceUnavailable
- 504: GatewayTimeout
Dynamic adjustment of the internal HTTP timeout
XSAPI dynamically adjusts the internal HTTP timeout, based on how much time remains in the XblContextSettingsGetHttpTimeoutWindow. The internal HTTP timeout controls how long the OS spends doing the HTTP network operation before it aborts. The call will not be retried unless there remains at least 5 seconds left in the XblContextSettingsGetHttpTimeoutWindow, to give an enough reasonable time for the call to complete. This rule doesn’t apply to the first call, so setting the XblContextSettingsSetHttpTimeoutWindow to 0 is acceptable, and will result in a single call. This logic has the effect that XblContextSettingsGetHttpTimeoutWindow is more deterministic about when the API call will return. If a “Retry-After” header was returned, no retries will be made until after the “Retry-After” time has been reached. If the “Retry-After” time is after the XblContextSettingsGetHttpTimeoutWindow, then the call return at the end of the XblContextSettingsGetHttpTimeoutWindow.Error handling
Title developers should always use proper error handling for every service call, they need to ensure that they are handling failed responses properly. There are many real-world conditions that can result in a request to XBOX services to return failure codes, such as:- Network is not available. For example, the device lost 4G, lost Wi-Fi, or the network went down.
- Too much load on services over load (503).
- A failure happened on the service (500).
- Too many requests where sent to the service (429).
- Write operation conflict (412). For example, another player in a multiplayer session submitted a change first.
- The user has been banned or does not have permission.
- User has signed-out.
Best calling patterns
Use batching requests
Some endpoints support batching or aggregating of a set of requests into a single call. For example, with the XBOX service’s profile service you can ask for a single user’s profile or a set of users profiles. So if you need a user profiles for a set of users, it would be very inefficient to call the endpoint or API one at a time for each user profile. Each call adds a lot of authentication overhead. So instead, pass all the users you want information about at once to the API, so that the endpoint can process all the user profiles at the same time and return a single response.Use the Real Time Activity (RTA) service instead of polling
A best practice is use the Real-Time Activity (RTA) service instead periodic polling. The Real-Time Activity service exposes a web socket that sends a notification to clients when target resources change on the service. The RTA service gives notifications on presence changes, statistic changes, multiplayer session document changes and social relationship changes. To know what the information client is interested in, the client must first subscribe to the item over the web socket. This avoids polling the service to detect changes since you will be told exactly when the item changes. XSAPI exposes the RTA service as a set of subscribe APIs that clients can use. Each of these APIs have corresponding*ChangedHandler APIs which take in a callback function that will be called when an item changes.
- XblPresenceSubscribeToDevicePresenceChange
- XblPresenceSubscribeToTitlePresenceChange
- XblUserStatisticsSubscribeToStatisticChange
- XblSocialSubscribeToSocialRelationshipChange
Use XSAPI client-side managers
XSAPI has a set of managers which act as cache and state machines that do all the heavy lifting for certain scenarios.Social Manager
The Social Manager does all the heavy lifting around friends lists and profiles. The Social Manager keeps your friends list, their profiles, and their presence data up to date using the RTA service. The Social Manager exposes a synchronous API that is very game-engine friendly. Games can call the Social Manager APIs frequently, because Social Manager maintains an in-memory cache of the latest information from the service. See Social Manager.Multiplayer Manager
For multiplayer session management, the Multiplayer Manager is a drop-in solution for traditional multiplayer games. The Multiplayer Manager API includes player roster and session management, handles game invites, join in progress, matchmaking, and plugs into your existing networking solution. It does all the heavy lifting around implementing traditional multiplayer flows. See Multiplayer Manager.Throttling (fine grained rate limiting)
XBOX services have throttling in place to prevent any single device from putting extreme load on the service. It’s important to know when your title was throttled. To determine whether your title was throttled, use any of these approaches:- Monitoring for HTTP Status Code 429
- Using debug asserts
- Using the XBOX services Trace Analyzer tool
