| name | async-programming |
| description | Asynchronous programming models including coroutines, async/await, and reactive patterns |
| sasmp_version | 1.3.0 |
| version | 2.0.0 |
| bonded_agent | 01-game-server-architect |
| bond_type | SECONDARY_BOND |
| parameters | {"required":["async_model"],"optional":["concurrency_limit","timeout_ms"],"validation":{"async_model":{"type":"string","enum":["async_await","coroutines","goroutines","futures","reactive"]},"concurrency_limit":{"type":"integer","min":1,"max":10000,"default":1000},"timeout_ms":{"type":"integer","min":100,"max":60000,"default":30000}}} |
| retry_config | {"max_attempts":3,"backoff":"exponential","initial_delay_ms":100,"retryable_errors":["TIMEOUT","CONNECTION_RESET"]} |
| observability | {"logging":{"level":"debug","fields":["task_id","async_model","duration_ms"]},"metrics":[{"name":"async_tasks_active","type":"gauge"},{"name":"async_task_duration_seconds","type":"histogram"},{"name":"async_errors_total","type":"counter"},{"name":"event_loop_lag_ms","type":"gauge"}]} |
Async Programming for Game Servers
Implement efficient asynchronous patterns for scalable game server code.
Async Models Comparison
| Model | Language | Overhead | Complexity | Best For |
|---|
| async/await | C#, JS, Rust | Low | Low | I/O bound |
| Coroutines | C++20, Kotlin | Very Low | Medium | High performance |
| Goroutines | Go | Very Low | Low | Concurrent services |
| Futures | C++, Java | Medium | Medium | Composable async |
| Reactive | All | Low | High | Stream processing |
C# async/await
public class GameServer
{
private readonly SemaphoreSlim _connectionLimit = new(1000);
private readonly CancellationTokenSource _cts = new();
public async Task HandlePlayerAsync(
Player player,
CancellationToken cancellationToken = default)
{
using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(
cancellationToken, _cts.Token);
var token = linkedCts.Token;
try
{
await _connectionLimit.WaitAsync(token);
while (player.Connected && !token.IsCancellationRequested)
{
timeoutCts = CancellationTokenSource(TimeSpan.FromSeconds());
combinedCts = CancellationTokenSource.CreateLinkedTokenSource(
token, timeoutCts.Token);
message = player.ReadMessageAsync(combinedCts.Token);
result = ProcessCommandAsync(message, token);
player.SendAsync(result, token);
}
}
(OperationCanceledException) (token.IsCancellationRequested)
{
}
{
_connectionLimit.Release();
}
}
{
validationTask = ValidateAsync(msg, token);
playerDataTask = LoadPlayerDataAsync(msg.PlayerId, token);
permissionsTask = CheckPermissionsAsync(msg.PlayerId, token);
Task.WhenAll(validationTask, playerDataTask, permissionsTask);
ApplyCommand(msg,
validationTask,
playerDataTask,
permissionsTask);
}
{
_cts.Cancel();
Task.Delay(TimeSpan.FromSeconds());
}
}