From 4b3466a1b0854ac335bbeb04c58da258b2a237a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Untersch=C3=BCtz?= Date: Sun, 4 Jan 2026 17:41:46 +0100 Subject: [PATCH] Revise room-to-pod assignment logic with hash-based distribution for improved load balancing and better replica handling. --- cmd/server/main.go | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index ec2ce21..1a41acd 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -275,34 +275,30 @@ func isResponsibleForRoom(roomID string) bool { return true } - // Hash-basierte Zuweisung: RoomID → Pod - // Einfacher Ansatz: erster Buchstabe von RoomID - // A-M → Pod 0, N-Z → Pod 1 if len(roomID) == 0 { return true } - firstChar := roomID[0] + // Hash-basierte Zuweisung: RoomID → Pod + // Verwende einfachen Hash für bessere Verteilung + hash := 0 + for _, c := range roomID { + hash = (hash*31 + int(c)) % 2 + } - // Für 2 Replicas: A-M und N-Z splitten - if totalReplicas == "2" { - // Pod Namen sind meist: escape-game-0, escape-game-1, etc. - if firstChar <= 'M' || firstChar <= 'm' { - return serverID[len(serverID)-1] == '0' || !hasDigitSuffix() + // Bestimme Pod-Nummer aus ServerID + podNumber := 0 + if len(serverID) > 0 { + lastChar := serverID[len(serverID)-1] + if lastChar >= '0' && lastChar <= '9' { + podNumber = int(lastChar - '0') } - return serverID[len(serverID)-1] == '1' || hasDigitSuffix() } - // Fallback: immer zuständig - return true -} + responsible := (hash % 2) == (podNumber % 2) + log.Printf("🎯 Room '%s' → Hash=%d, Pod=%d (%s) → Responsible=%v", roomID, hash, podNumber, serverID, responsible) -func hasDigitSuffix() bool { - if len(serverID) == 0 { - return false - } - lastChar := serverID[len(serverID)-1] - return lastChar >= '0' && lastChar <= '9' + return responsible } func loadServerAssets(w *game.World) {