implement alternative match-making approach with Lua scripts in Redis

This commit is contained in:
2026-10-25 15:57:24 +03:00
parent 582ecb7c74
commit acdaebd491
19 changed files with 796 additions and 139 deletions
@@ -0,0 +1,25 @@
# Matchmaking configuration. All values can be overridden via environment variables.
mm = Rails.application.config.x.matchmaking = ActiveSupport::OrderedOptions.new
mm.game_server_url = ENV.fetch("GAME_SERVER_URL", "ws://localhost:8080")
mm.default_rating = ENV.fetch("MATCHMAKING_DEFAULT_RATING", 1000).to_i
# How long a found match (and its game token) stays valid, in seconds.
mm.match_ttl = ENV.fetch("MATCHMAKING_MATCH_TTL", 120).to_i
mm.token_ttl = ENV.fetch("MATCHMAKING_TOKEN_TTL", 120).to_i
# How long a player waits in the queue before a timeout, in seconds.
mm.queue_ttl = ENV.fetch("MATCHMAKING_QUEUE_TTL", 60).to_i
# How long a found (but not yet finalized) pair is kept in the "pending"
# state before the players are put back into the queue, in seconds.
mm.pending_ttl = ENV.fetch("MATCHMAKING_PENDING_TTL", 15).to_i
# Rating tolerance: players within `base_range` are matched immediately;
# the tolerance grows by `range_per_second` of waiting up to `max_range`.
mm.base_range = ENV.fetch("MATCHMAKING_BASE_RANGE", 100).to_i
mm.range_per_second = ENV.fetch("MATCHMAKING_RANGE_PER_SECOND", 50).to_i
mm.max_range = ENV.fetch("MATCHMAKING_MAX_RANGE", 1000).to_i
# How often the background sweep job re-checks the queue, in seconds.
mm.sweep_interval = ENV.fetch("MATCHMAKING_SWEEP_INTERVAL", 2).to_i
+5 -1
View File
@@ -2,6 +2,10 @@
# Development/test: localhost:6379 (default)
# Production: set REDIS_URL env variable, e.g. redis://redis.example.com:6379/0
# Use an isolated Redis logical database in tests to avoid clobbering dev data.
url = ENV.fetch("REDIS_URL", "redis://localhost:6380/0")
url = url.sub(%r{/\d+$}, "/15") if Rails.env.test?
RedisConnection = ConnectionPool.new(size: 5, timeout: 5) do
Redis.new(url: ENV.fetch("REDIS_URL", "redis://localhost:6380/0"))
Redis.new(url: url)
end