24
24
activeConnections = make (map [string ]* websocket.Conn )
25
25
// A map to hold user's match ctx cancel function
26
26
matchContexts = make (map [string ]context.CancelFunc )
27
- mu sync.Mutex // Mutex for thread-safe access to activeConnections
27
+ // A map to hold user's match channels
28
+ matchFoundChannels = make (map [string ]chan models.MatchFound )
29
+ mu sync.Mutex // Mutex for thread-safe access to activeConnections
28
30
)
29
31
30
32
// handleConnections manages WebSocket connections and matching logic.
@@ -67,6 +69,9 @@ func HandleWebSocketConnections(w http.ResponseWriter, r *http.Request) {
67
69
activeConnections [matchRequest .Username ] = ws
68
70
matchCtx , matchCancel := context .WithCancel (context .Background ())
69
71
matchContexts [matchRequest .Username ] = matchCancel
72
+
73
+ matchFoundChan := make (chan models.MatchFound )
74
+ matchFoundChannels [matchRequest .Username ] = matchFoundChan
70
75
mu .Unlock ()
71
76
72
77
// Create a context for cancellation
@@ -84,11 +89,9 @@ func HandleWebSocketConnections(w http.ResponseWriter, r *http.Request) {
84
89
}
85
90
defer timeoutCancel ()
86
91
87
- matchFoundChan := make (chan models.MatchFound )
88
-
89
92
// Start goroutines for handling messages and performing matching.
90
93
go processes .ReadMessages (ws , ctx , cancel )
91
- go processes .PerformMatching (matchRequest , ctx , matchFoundChan ) // Perform matching
94
+ go processes .PerformMatching (matchRequest , context . Background (), matchFoundChannels ) // Perform matching
92
95
93
96
// Wait for a match, timeout, or cancellation.
94
97
waitForResult (ws , ctx , timeoutCtx , matchCtx , matchFoundChan , matchRequest .Username )
@@ -127,29 +130,47 @@ func waitForResult(ws *websocket.Conn, ctx, timeoutCtx, matchCtx context.Context
127
130
log .Println ("Matching cancelled" )
128
131
// Cleanup Redis
129
132
processes .CleanUpUser (processes .GetRedisClient (), username , context .Background ())
133
+ // Remove the match context and active
134
+ if _ , exists := matchContexts [username ]; exists {
135
+ delete (matchContexts , username )
136
+ }
137
+ if _ , exists := activeConnections [username ]; exists {
138
+ delete (activeConnections , username )
139
+ }
140
+ if _ , exists := matchFoundChannels [username ]; exists {
141
+ delete (matchFoundChannels , username )
142
+ }
143
+
130
144
return
131
145
case <- timeoutCtx .Done ():
132
146
log .Println ("Connection timed out" )
133
147
// Cleanup Redis
134
148
processes .CleanUpUser (processes .GetRedisClient (), username , context .Background ())
149
+ // Remove the match context and active
150
+ if _ , exists := matchContexts [username ]; exists {
151
+ delete (matchContexts , username )
152
+ }
153
+ if _ , exists := activeConnections [username ]; exists {
154
+ delete (activeConnections , username )
155
+ }
156
+ if _ , exists := matchFoundChannels [username ]; exists {
157
+ delete (matchFoundChannels , username )
158
+ }
159
+
135
160
sendTimeoutResponse (ws )
136
161
return
137
162
case <- matchCtx .Done ():
138
- log .Println ("Match found for user HEREEE : " + username )
163
+ log .Println ("Match found for user: " + username )
139
164
return
140
165
case result , ok := <- matchFoundChan :
141
166
if ! ok {
142
167
// Channel closed without a match, possibly due to context cancellation
143
168
log .Println ("Match channel closed without finding a match" )
144
169
return
145
170
}
146
- log .Println ("Match found for user: " + result . User )
171
+ log .Println ("Match found for user: " + username )
147
172
// Notify the users about the match
148
173
notifyMatch (result .User , result .MatchedUser , result )
149
-
150
- // if err := ws.WriteJSON(result); err != nil {
151
- // log.Printf("write error: %v", err)
152
- // }
153
174
return
154
175
}
155
176
}
@@ -185,13 +206,22 @@ func notifyMatch(username, matchedUsername string, result models.MatchFound) {
185
206
}
186
207
187
208
// Remove the match context for both users and cancel for matched user
188
- if _ , exists := matchContexts [username ]; exists {
209
+ if cancelFunc , exists := matchContexts [username ]; exists {
210
+ cancelFunc ()
189
211
delete (matchContexts , username )
190
212
}
191
213
192
- if cancelFunc , exists := matchContexts [matchedUsername ]; exists {
214
+ if cancelFunc2 , exists := matchContexts [matchedUsername ]; exists {
215
+ cancelFunc2 ()
193
216
delete (matchContexts , matchedUsername )
194
- defer cancelFunc () // TODO: CancelFunction here is not causing the matchCtx to be done
217
+ }
218
+
219
+ // Remove the match channels
220
+ if _ , exists := matchFoundChannels [username ]; exists {
221
+ delete (matchFoundChannels , username )
222
+ }
223
+ if _ , exists := matchFoundChannels [matchedUsername ]; exists {
224
+ delete (matchFoundChannels , matchedUsername )
195
225
}
196
226
197
227
// Remove users from the activeConnections map
0 commit comments