From 4629c9faed5ff3c7e26eec739427862a36d86aa6 Mon Sep 17 00:00:00 2001 From: Ivan Panayotov Date: Mon, 5 Jan 2026 19:35:54 +0200 Subject: [PATCH 1/2] Update CD Configuration (#2) --- k8s/deployment.yml | 17 +---------------- k8s/namespace.yml | 4 ++++ k8s/service.yml | 14 ++++++-------- 3 files changed, 11 insertions(+), 24 deletions(-) create mode 100644 k8s/namespace.yml diff --git a/k8s/deployment.yml b/k8s/deployment.yml index 4abe24f..d710a28 100644 --- a/k8s/deployment.yml +++ b/k8s/deployment.yml @@ -19,7 +19,7 @@ spec: spec: containers: - name: spotify-remastered - image: ${{ secrets.DOCKER_USERNAME }}/spotifyremastered:latest + image: imrshadow/spotifyremastered:latest imagePullPolicy: Always ports: - containerPort: 1337 @@ -28,18 +28,3 @@ spec: value: "0.0.0.0" - name: SERVER_PORT value: "1337" ---- -apiVersion: v1 -kind: Service -metadata: - name: spotify-remastered - namespace: spotify-app -spec: - type: NodePort - selector: - app: spotify-remastered - ports: - - protocol: TCP - port: 80 - targetPort: 1337 - nodePort: 30007 \ No newline at end of file diff --git a/k8s/namespace.yml b/k8s/namespace.yml new file mode 100644 index 0000000..65a32da --- /dev/null +++ b/k8s/namespace.yml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: spotify-app diff --git a/k8s/service.yml b/k8s/service.yml index 4cdb085..77a592a 100644 --- a/k8s/service.yml +++ b/k8s/service.yml @@ -1,17 +1,15 @@ apiVersion: v1 kind: Service metadata: - name: spotify-service + name: spotify-remastered namespace: spotify-app - labels: - app: spotify spec: + type: NodePort selector: - app: spotify + app: spotify-remastered ports: - - port: 1337 + - protocol: TCP + port: 1337 targetPort: 1337 - name: socket - protocol: TCP + nodePort: 30007 - type: NodePort \ No newline at end of file From c13b9862a103645517d7721b5f80028be705d834 Mon Sep 17 00:00:00 2001 From: Ivan Panayotov Date: Tue, 6 Jan 2026 01:37:42 +0200 Subject: [PATCH 2/2] Refactor to use ConcurrentHashMap for thread safety --- .../server/SpotifyManager.java | 42 +++++++++---------- .../server/SpotifyServer.java | 4 +- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/spotifyremastered/server/SpotifyManager.java b/src/main/java/com/spotifyremastered/server/SpotifyManager.java index dce6099..c759b25 100644 --- a/src/main/java/com/spotifyremastered/server/SpotifyManager.java +++ b/src/main/java/com/spotifyremastered/server/SpotifyManager.java @@ -2,15 +2,16 @@ import com.spotifyremastered.server.music.MusicManager; import com.spotifyremastered.server.user.UserManager; + import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; public class SpotifyManager { private final UserManager userManager; private final MusicManager musicManager; - private final Map portsManager; - private final Object lock = new Object(); + private final Map portsManager = new ConcurrentHashMap<>(); private static final short FIRST_AVAILABLE_PORT = 1338; private static final short MAX_PORTS = 1000; @@ -18,13 +19,11 @@ public class SpotifyManager { public SpotifyManager() { this.userManager = new UserManager(new HashMap<>(), new HashMap<>(), UserManager.USER_DATA_FILE_PATH); this.musicManager = new MusicManager(); - this.portsManager = new HashMap<>(); } public SpotifyManager(UserManager userManager, MusicManager musicManager) { this.userManager = userManager; this.musicManager = musicManager; - this.portsManager = new HashMap<>(); } public UserManager getUserManager() { @@ -36,39 +35,38 @@ public MusicManager getMusicManager() { } public Map getPortsManager() { - synchronized (lock) { - return new HashMap<>(portsManager); - } + return Map.copyOf(portsManager); } public int getNewPort(Client client) { - synchronized (lock) { - int maxPort = FIRST_AVAILABLE_PORT + MAX_PORTS; - for (int i = FIRST_AVAILABLE_PORT; i < maxPort; i++) { - if (isPortAvailable(i)) { - portsManager.put(client, i); + Integer existingPort = portsManager.get(client); + if (existingPort != null) { + return existingPort; + } + + int maxPort = FIRST_AVAILABLE_PORT + MAX_PORTS; + for (int i = FIRST_AVAILABLE_PORT; i < maxPort; i++) { + if (isPortAvailable(i)) { + Integer previous = portsManager.putIfAbsent(client, i); + if (previous == null) { return i; + } else { + return previous; } } - throw new RuntimeException("No available ports"); } + throw new RuntimeException("No available ports"); } public void removePort(Client client) { - synchronized (lock) { - portsManager.remove(client); - } + portsManager.remove(client); } public boolean isPortAvailable(int port) { - synchronized (lock) { - return !portsManager.containsValue(port); - } + return !portsManager.containsValue(port); } public boolean isPortAvailable(Client client) { - synchronized (lock) { - return portsManager.containsValue(portsManager.get(client)); - } + return portsManager.containsKey(client); } } diff --git a/src/main/java/com/spotifyremastered/server/SpotifyServer.java b/src/main/java/com/spotifyremastered/server/SpotifyServer.java index d35c378..d877b88 100644 --- a/src/main/java/com/spotifyremastered/server/SpotifyServer.java +++ b/src/main/java/com/spotifyremastered/server/SpotifyServer.java @@ -17,6 +17,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; public class SpotifyServer { @@ -27,10 +28,9 @@ public class SpotifyServer { private static boolean online = true; private final SpotifyManager spotifyManager; - private final Map clients; + private final Map clients = new ConcurrentHashMap<>(); public SpotifyServer() { - this.clients = new HashMap<>(); this.spotifyManager = new SpotifyManager(); }