🛡️ Resilient services

Altruist was designed with fault-tolerance and graceful recovery at its core. One of its foundational features is the concept of Resilient Services — components that handle external connections (e.g., Redis, ScyllaDB) and automatically integrate with Altruist's server status lifecycle.


Note:

Resilient Services help you centralize connection logic and ensure your game engine doesn't run unless all critical services are healthy.

What Is a Resilient Service?

A Resilient Service is any service that implements the IConnectable interface:


public class ResilientService : IConnectable {...}

Once implemented, Altruist will automatically:

  • Attempt to connect your service on startup.
  • Retry if connection fails.
  • Detect and log connection loss or restore.
  • Prevent engine startup until all services are online.
  • Gracefully pause engine activity during outages.

🛠️ How to Build a Resilient Service

1. Implement IConnectable

Just implement the interface in your service class. Make sure to raise the appropriate events during connection lifecycle changes.

public class ResilientService : IConnectable
{
    public bool IsConnected { get; private set; }

    public event Action? OnConnected;
    public event Action<Exception>? OnFailed;
    public event Action<Exception>? OnRetryExhausted;

    public async Task ConnectAsync(int maxRetries = 30, int delayMilliseconds = 2000)
    {
        // Your retry connection logic here
    }

    public void RaiseConnectedEvent() => OnConnected?.Invoke();
    public void RaiseFailedEvent(Exception ex) => OnFailed?.Invoke(ex);
    public void RaiseOnRetryExhaustedEvent(Exception ex) => OnRetryExhausted?.Invoke(ex);
}

2. Register the Service in DI

Make sure to register your service as IConnectable in your Startup or composition root:

services.AddSingleton<ResilientService>();
services.AddSingleton<IConnectable>(sp => sp.GetRequiredService<ResilientService>());

Note:

This tells Altruist to track and manage it as a Resilient Service.


If you wish to use the IConnectable interface without registering it as resilient service, simply skip this part: services.AddSingleton<IConnectable>(sp => sp.GetRequiredService<ResilientService>());

🚦 Lifecycle Integration

Altruist will track all IConnectable services. If any of them disconnects, it will:

  • Mark the server as degraded
  • Stop the engine
  • Resume operations only when all services are healthy again

This guarantees a consistent and reliable game server experience — even under unreliable infrastructure.