@@ -123,44 +123,42 @@ func createTimeoutContext() (context.Context, context.CancelFunc, error) {
123
123
return ctx , cancel , nil
124
124
}
125
125
126
+ // Cleans up the data associated with the user before ending the websocket connection.
127
+ // If user is already removed, then nothing happens.
128
+ func cleanUpUser (username string ) {
129
+ // Cleanup Redis
130
+ processes .CleanUpUser (processes .GetRedisClient (), username , context .Background ())
131
+
132
+ // Removes the match context and active
133
+ if cancelFunc , exists := matchContexts [username ]; exists {
134
+ cancelFunc ()
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
+ }
144
+
126
145
// waitForResult waits for a match result, timeout, or cancellation.
127
146
func waitForResult (ws * websocket.Conn , ctx , timeoutCtx , matchCtx context.Context , matchFoundChan chan models.MatchFound , username string ) {
128
147
select {
129
148
case <- ctx .Done ():
130
149
log .Println ("Matching cancelled" )
131
- // Cleanup Redis
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
-
150
+ cleanUpUser (username )
144
151
return
145
152
case <- timeoutCtx .Done ():
146
153
log .Println ("Connection timed out" )
147
- // Cleanup Redis
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
-
160
154
sendTimeoutResponse (ws )
155
+ cleanUpUser (username )
161
156
return
162
157
case <- matchCtx .Done ():
163
158
log .Println ("Match found for user: " + username )
159
+
160
+ // NOTE: user is already cleaned-up in the other process,
161
+ // so there is no need to clean up again.
164
162
return
165
163
case result , ok := <- matchFoundChan :
166
164
if ! ok {
@@ -170,7 +168,10 @@ func waitForResult(ws *websocket.Conn, ctx, timeoutCtx, matchCtx context.Context
170
168
}
171
169
log .Println ("Match found for user: " + username )
172
170
// Notify the users about the match
173
- notifyMatch (result .User , result .MatchedUser , result )
171
+ notifyMatches (result .User , result .MatchedUser , result )
172
+
173
+ // NOTE: user and other user are already cleaned up in a separate matching algorithm process
174
+ // so no clean up is required here.
174
175
return
175
176
}
176
177
}
@@ -186,7 +187,7 @@ func sendTimeoutResponse(ws *websocket.Conn) {
186
187
}
187
188
}
188
189
189
- func notifyMatch (username , matchedUsername string , result models.MatchFound ) {
190
+ func notifyMatches (username , matchedUsername string , result models.MatchFound ) {
190
191
mu .Lock ()
191
192
defer mu .Unlock ()
192
193
@@ -204,27 +205,4 @@ func notifyMatch(username, matchedUsername string, result models.MatchFound) {
204
205
log .Printf ("Error sending message to user %s: %v\n " , username , err )
205
206
}
206
207
}
207
-
208
- // Remove the match context for both users and cancel for matched user
209
- if cancelFunc , exists := matchContexts [username ]; exists {
210
- cancelFunc ()
211
- delete (matchContexts , username )
212
- }
213
-
214
- if cancelFunc2 , exists := matchContexts [matchedUsername ]; exists {
215
- cancelFunc2 ()
216
- delete (matchContexts , matchedUsername )
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 )
225
- }
226
-
227
- // Remove users from the activeConnections map
228
- delete (activeConnections , username )
229
- delete (activeConnections , matchedUsername )
230
208
}
0 commit comments