mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 08:23:45 +00:00
fix(multiplayer): reconcile authoritative initial connections
This commit is contained in:
@@ -20,6 +20,7 @@ func main() {
|
||||
dsn := flag.String("dsn", os.Getenv("COSMIC_CLASH_POSTGRES_DSN"), "PostgreSQL connection string")
|
||||
migrationDir := flag.String("migrations", "migrations", "directory containing numbered SQL migrations")
|
||||
interval := flag.Duration("interval", time.Minute, "maintenance poll interval")
|
||||
initialConnectInterval := flag.Duration("initial-connect-interval", time.Second, "initial-connect reconciliation poll interval")
|
||||
batch := flag.Int("batch", 100, "maximum player rollovers per pass")
|
||||
stalledAllocationDeadline := flag.Duration("stalled-allocation-deadline", 2*time.Minute, "reclaim a match stuck in ALLOCATING/PROCESS_READY/ASSIGNMENT_READY (server crashed or was reclaimed before registering) after this long, requeuing every participant without penalty")
|
||||
stalledAllocationBatch := flag.Int("stalled-allocation-batch", 100, "maximum stalled matches reclaimed per pass")
|
||||
@@ -28,7 +29,7 @@ func main() {
|
||||
if *dsn == "" {
|
||||
fatalf("--dsn or COSMIC_CLASH_POSTGRES_DSN is required")
|
||||
}
|
||||
if *interval <= 0 || *batch < 1 || *batch > 1000 {
|
||||
if *interval <= 0 || *initialConnectInterval <= 0 || *batch < 1 || *batch > 1000 {
|
||||
fatalf("invalid interval or batch")
|
||||
}
|
||||
if *stalledAllocationDeadline <= 0 || *stalledAllocationBatch < 1 || *stalledAllocationBatch > 1000 {
|
||||
@@ -52,8 +53,7 @@ func main() {
|
||||
}
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
for {
|
||||
now := time.Now().UTC()
|
||||
runGeneral := func(now time.Time) {
|
||||
count, err := store.RolloverDueSeasons(ctx, db, now, *batch)
|
||||
if err != nil {
|
||||
fatalf("season maintenance: %v", err)
|
||||
@@ -68,6 +68,8 @@ func main() {
|
||||
if reclaimed > 0 {
|
||||
log.Printf("reclaimed %d stalled allocations, requeuing their participants", reclaimed)
|
||||
}
|
||||
}
|
||||
runInitialConnect := func(now time.Time) {
|
||||
reconciled, err := store.ReconcileInitialConnect(ctx, db, now, *initialConnectBatch)
|
||||
if err != nil {
|
||||
fatalf("initial-connect maintenance: %v", err)
|
||||
@@ -75,12 +77,22 @@ func main() {
|
||||
if reconciled > 0 {
|
||||
log.Printf("reconciled %d initial-connect outcomes", reconciled)
|
||||
}
|
||||
timer := time.NewTimer(*interval)
|
||||
}
|
||||
|
||||
runGeneral(time.Now().UTC())
|
||||
runInitialConnect(time.Now().UTC())
|
||||
generalTicker := time.NewTicker(*interval)
|
||||
initialConnectTicker := time.NewTicker(*initialConnectInterval)
|
||||
defer generalTicker.Stop()
|
||||
defer initialConnectTicker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
timer.Stop()
|
||||
return
|
||||
case <-timer.C:
|
||||
case now := <-generalTicker.C:
|
||||
runGeneral(now.UTC())
|
||||
case now := <-initialConnectTicker.C:
|
||||
runInitialConnect(now.UTC())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user