# ppy/osu-server-spectator architecture

> Architecture diagram of the GitHub repository ppy/osu-server-spectator, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ppy/osu-server-spectator
- Repository: https://github.com/ppy/osu-server-spectator

## Overview

##### Project type and purpose
- This is a backend web service (ASP.NET Core) that handles real-time “spectator” data for osu! matches.
- It exposes multiple SignalR hubs for multiplayer events, metadata broadcasting, score uploading, logging, and spectator streams.
- It persists data to MySQL (via Dapper), optionally to AWS S3 or local file storage, and integrates with other osu! web services over HTTP.

##### File structure
- Top-level directories:
- osu.Server.Spectator – the main server project
- osu.Server.Spectator.Tests – unit/integration tests
- SampleMultiplayerClient & SampleSpectatorClient – example .NET client apps
- Key modules inside osu.Server.Spectator:
- Hubs/… – SignalR hubs (MetadataHub, MultiplayerHub, SpectatorHub, LoggingHub, StatefulUserHub)
- Services/… – business logic, shared interop with osu-web
- Database/… – Dapper wrappers, factory, data models
- Storage/… – IScoreStorage implementations (FileScoreStorage, S3ScoreStorage)
- Authentication, Extensions, GracefulShutdownManager, ConcurrentConnectionLimiter
- Configuration & deployment files: Dockerfile, appsettings.json, CI workflows.

##### README for additional insights
- Environment variables define ports, Redis, MySQL, S3 credentials, Sentry/Datadog, replay storage options, and shared-interop settings.
- The service is designed to plug into a full osu! server stack and interacts with the osu-web instance for shared interop.

A. Clients
- Game Clients (Multiplayer spectators & broadcasters)
- SampleMultiplayerClient & SampleSpectatorClient (for testing)
B. Load Balancer / API Gateway (optional)
- Routes WebSocket (SignalR) and HTTP (shared interop) traffic to the server cluster
C. osu-Server-Spectator Service (ASP.NET Core app)
1. SignalR Hubs Layer (Presentation)
– MetadataHub (broadcasts build counts, daily challenges)
– MultiplayerHub (match events, queues, room management)
– SpectatorHub (real-time score streams)
– LoggingHub (client logs)
– StatefulUserHub (user-state subscriptions)
2. Services Layer (Application logic)
– SharedInterop service (HTTP calls to osu-web)
– ConcurrentConnectionLimiter, ChatFilters, ScoreUploader, etc.
3. Data Access Layer
– DatabaseFactory & DatabaseAccess (Dapper)
– EntityStore (in-memory caches)
4. Storage Layer
– MySQL database (osu-web schema)
– Redis (interop messaging, optional)
– Replay storage (local file system or AWS S3)
5. Cross-cutting concerns
– Authentication (JWT bearer)
– GracefulShutdownManager
– Dependency Injection (ServiceCollectionExtensions)
– Logging (Sentry, Datadog via DD_AGENT_HOST)
D. External Systems
- osu-web HTTP API (shared interop domain)
- MySQL (DB_HOST: DB_PORT)
- Redis (REDIS_HOST)
- AWS S3 (REPLAYS_BUCKET)
- Monitoring & Error Tracking (Datadog, Sentry)

##### Relationships & data flow
- Within the server: Hubs → call into Services → call into Data Access → talk to MySQL or Redis
- SpectatorHub & ScoreUploader → write replay frames → local FS or S3
- Services → HTTP → osu-web (shared interop)
- Logging & metrics emitted to Datadog/Sentry via DI-injected logging pipeline
- GracefulShutdownManager coordinates shutdown across hubs and background tasks

##### Architectural patterns & principles
- Layered architecture (Presentation → Application → Data → Infrastructure)
- Real-time pub/sub via SignalR hubs
- Dependency injection for loose coupling (ISharedInterop, IScoreStorage, IDatabaseAccess)
- Single monolithic service with logical separation (no microservices)
- Configuration via environment variables for twelve-factor compliance
- Containerization (Dockerfile)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients
    Clients["Game Clients"]:::external
    SampleMP["SampleMultiplayerClient"]:::external
    SampleSP["SampleSpectatorClient"]:::external

    %% Load Balancer
    Balancer["Load Balancer / API Gateway"]:::external

    %% osu-Server-Spectator Service
    subgraph "osu-Server-Spectator"
        direction TB

        subgraph "SignalR Hubs"
            direction TB
            MetadataHub["MetadataHub (SignalR)"]:::hubService
            BuildUserCountUpdater["BuildUserCountUpdater"]:::hubService
            DailyChallengeUpdater["DailyChallengeUpdater"]:::hubService
            MetadataBroadcaster["MetadataBroadcaster"]:::hubService

            MultiplayerHub["MultiplayerHub (SignalR)"]:::hubService
            MultiplayerQueue["MultiplayerQueue"]:::hubService
            MatchTypeImplementation["MatchTypeImplementation"]:::hubService
            TeamVersus["TeamVersus"]:::hubService
            MultiplayerEventLogger["MultiplayerEventLogger"]:::hubService

            SpectatorHub["SpectatorHub (SignalR)"]:::hubService
            ScoreProcessedSubscriber["ScoreProcessedSubscriber"]:::hubService
            ScoreUploader["ScoreUploader"]:::hubService

            LoggingHub["LoggingHub (SignalR)"]:::hubService
            StatefulUserHub["StatefulUserHub"]:::hubService
        end

        subgraph "Services Layer"
            direction TB
            ISharedInterop["ISharedInterop"]:::service
            SharedInterop["SharedInterop"]:::service
            ChatFilters["ChatFilters"]:::service
            ConcurrentConnectionLimiter["ConcurrentConnectionLimiter"]:::service
        end

        subgraph "Data Access Layer"
            direction TB
            DatabaseFactory["DatabaseFactory"]:::data
            IDatabaseFactory["IDatabaseFactory"]:::data
            DatabaseAccess["DatabaseAccess"]:::data
            IDatabaseAccess["IDatabaseAccess"]:::data
            DatabaseModels["Database Models"]:::data
            IEntityStore["IEntityStore"]:::data
            EntityStore["EntityStore"]:::data
        end

        subgraph "Storage Layer"
            direction TB
            MySQL[(MySQL)]:::db
            Redis[(Redis)]:::db
            FileScoreStorage[(FileScoreStorage)]:::storage
            S3ScoreStorage[(S3ScoreStorage)]:::storage
        end

        subgraph "Cross-Cutting Concerns"
            direction TB
            ConfigureJwtBearerOptions["ConfigureJwtBearerOptions"]:::service
            GracefulShutdownManager["GracefulShutdownManager"]:::service
            ServiceCollectionExtensions["ServiceCollectionExtensions"]:::service
        end
    end

    %% External Systems
    osuWeb["osu-web HTTP API"]:::http
    Datadog["Datadog / Sentry"]:::external

    %% Connections
    Clients --> Balancer --> MetadataHub
    Balancer --> MultiplayerHub
    Balancer --> SpectatorHub
    Balancer --> LoggingHub
    Balancer --> StatefulUserHub

    %% Hub to Services
    MetadataHub --> SharedInterop
    BuildUserCountUpdater --> MetadataBroadcaster
    DailyChallengeUpdater --> MetadataBroadcaster
    MetadataBroadcaster --> SharedInterop

    MultiplayerHub --> MultiplayerQueue
    MultiplayerQueue --> MatchTypeImplementation
    MatchTypeImplementation --> TeamVersus
    MultiplayerHub --> MultiplayerEventLogger
    MultiplayerEventLogger --> SharedInterop

    SpectatorHub --> ScoreProcessedSubscriber
    ScoreUploader --> FileScoreStorage
    ScoreUploader --> S3ScoreStorage
    SpectatorHub --> ScoreUploader

    LoggingHub --> SharedInterop
    StatefulUserHub --> SharedInterop

    %% Services to Data Access
    SharedInterop --> DatabaseAccess
    ChatFilters --> DatabaseAccess
    ConcurrentConnectionLimiter --> DatabaseAccess

    DatabaseFactory --> DatabaseAccess
    DatabaseAccess --> DatabaseModels
    EntityStore --> DatabaseModels
    DatabaseAccess --> IEntityStore
    EntityStore --> IEntityStore

    %% Data Access to Storage
    DatabaseAccess --> MySQL
    SharedInterop --> osuWeb
    DatabaseAccess --> Redis

    %% Storage writes
    ScoreUploader --> FileScoreStorage
    ScoreUploader --> S3ScoreStorage

    %% Logging
    LoggingHub --> Datadog
    SharedInterop --> Datadog

    %% Click Events
    click MetadataHub "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Hubs/Metadata/MetadataHub.cs"
    click BuildUserCountUpdater "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Hubs/Metadata/BuildUserCountUpdater.cs"
    click DailyChallengeUpdater "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Hubs/Metadata/DailyChallengeUpdater.cs"
    click MetadataBroadcaster "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Hubs/Metadata/MetadataBroadcaster.cs"
    click MultiplayerHub "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Hubs/Multiplayer/MultiplayerHub.cs"
    click MultiplayerQueue "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Hubs/Multiplayer/MultiplayerQueue.cs"
    click MatchTypeImplementation "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Hubs/Multiplayer/MatchTypeImplementation.cs"
    click TeamVersus "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Hubs/Multiplayer/TeamVersus.cs"
    click MultiplayerEventLogger "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Hubs/Multiplayer/MultiplayerEventLogger.cs"
    click ScoreUploader "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Hubs/ScoreUploader.cs"
    click SpectatorHub "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Hubs/Spectator/SpectatorHub.cs"
    click ScoreProcessedSubscriber "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Hubs/Spectator/ScoreProcessedSubscriber.cs"
    click LoggingHub "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Hubs/LoggingHub.cs"
    click StatefulUserHub "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Hubs/StatefulUserHub.cs"
    click ISharedInterop "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Services/ISharedInterop.cs"
    click SharedInterop "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Services/SharedInterop.cs"
    click DatabaseAccess "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Database/DatabaseAccess.cs"
    click DatabaseFactory "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Database/DatabaseFactory.cs"
    click IDatabaseAccess "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Database/IDatabaseAccess.cs"
    click IDatabaseFactory "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Database/IDatabaseFactory.cs"
    click DatabaseModels "https://github.com/ppy/osu-server-spectator/tree/master/osu.Server.Spectator/Database/Models/"
    click IEntityStore "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Entities/IEntityStore.cs"
    click EntityStore "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Entities/EntityStore.cs"
    click ChatFilters "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/ChatFilters.cs"
    click ConcurrentConnectionLimiter "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/ConcurrentConnectionLimiter.cs"
    click ConfigureJwtBearerOptions "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Authentication/ConfigureJwtBearerOptions.cs"
    click GracefulShutdownManager "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/GracefulShutdownManager.cs"
    click ServiceCollectionExtensions "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Extensions/ServiceCollectionExtensions.cs"
    click IScoreStorage "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Storage/IScoreStorage.cs"
    click FileScoreStorage "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Storage/FileScoreStorage.cs"
    click S3ScoreStorage "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Storage/S3ScoreStorage.cs"
    click Program "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Program.cs"
    click Startup "https://github.com/ppy/osu-server-spectator/blob/master/osu.Server.Spectator/Startup.cs"
    click SampleMP "https://github.com/ppy/osu-server-spectator/blob/master/SampleMultiplayerClient/Program.cs"
    click SampleSP "https://github.com/ppy/osu-server-spectator/blob/master/SampleSpectatorClient/Program.cs"
    click Balancer "https://github.com/ppy/osu-server-spectator/tree/master/Dockerfile"

    %% Styles
    classDef hubService fill:#cce5ff,stroke:#333,stroke-width:1px
    classDef service fill:#cfe2ff,stroke:#333,stroke-width:1px
    classDef data fill:#d4edda,stroke:#333,stroke-width:1px
    classDef db fill:#d4edda,stroke:#333,stroke-width:1px
    classDef storage fill:#d1ecf1,stroke:#333,stroke-width:1px
    classDef external fill:#f8d7da,stroke:#333,stroke-width:1px
    classDef http fill:#fff3cd,stroke:#333,stroke-width:1px
```
