@@ -151,11 +151,12 @@ func (s *PostgresStore) CreateLobby(ctx context.Context, game, lobbyCode, peerID
151
151
logger .Warn ("peer id too long" , zap .String ("peerID" , peerID ))
152
152
return ErrInvalidPeerID
153
153
}
154
+ now := util .Now (ctx )
154
155
res , err := s .DB .Exec (ctx , `
155
- INSERT INTO lobbies (code, game, public)
156
- VALUES ($1, $2, true)
156
+ INSERT INTO lobbies (code, game, public, created_at, updated_at )
157
+ VALUES ($1, $2, true, $3, $3 )
157
158
ON CONFLICT DO NOTHING
158
- ` , lobbyCode , game )
159
+ ` , lobbyCode , game , now )
159
160
if err != nil {
160
161
return err
161
162
}
@@ -171,6 +172,9 @@ func (s *PostgresStore) JoinLobby(ctx context.Context, game, lobbyCode, peerID s
171
172
logger .Warn ("peer id too long" , zap .String ("peerID" , peerID ))
172
173
return nil , ErrInvalidPeerID
173
174
}
175
+
176
+ now := util .Now (ctx )
177
+
174
178
tx , err := s .DB .Begin (ctx )
175
179
if err != nil {
176
180
return nil , err
@@ -200,10 +204,12 @@ func (s *PostgresStore) JoinLobby(ctx context.Context, game, lobbyCode, peerID s
200
204
201
205
_ , err = tx .Exec (ctx , `
202
206
UPDATE lobbies
203
- SET peers = array_append(peers, $1)
204
- WHERE code = $2
205
- AND game = $3
206
- ` , peerID , lobbyCode , game )
207
+ SET
208
+ peers = array_append(peers, $1),
209
+ updated_at = $2
210
+ WHERE code = $3
211
+ AND game = $4
212
+ ` , peerID , now , lobbyCode , game )
207
213
if err != nil {
208
214
return nil , err
209
215
}
@@ -232,14 +238,18 @@ func (s *PostgresStore) IsPeerInLobby(ctx context.Context, game, lobbyCode, peer
232
238
}
233
239
234
240
func (s * PostgresStore ) LeaveLobby (ctx context.Context , game , lobbyCode , peerID string ) ([]string , error ) {
241
+ now := util .Now (ctx )
242
+
235
243
var peerlist []string
236
244
err := s .DB .QueryRow (ctx , `
237
245
UPDATE lobbies
238
- SET peers = array_remove(peers, $1)
239
- WHERE code = $2
240
- AND game = $3
246
+ SET
247
+ peers = array_remove(peers, $1),
248
+ updated_at = $2
249
+ WHERE code = $3
250
+ AND game = $4
241
251
RETURNING peers
242
- ` , peerID , lobbyCode , game ).Scan (& peerlist )
252
+ ` , peerID , now , lobbyCode , game ).Scan (& peerlist )
243
253
if err != nil && ! errors .Is (err , pgx .ErrNoRows ) {
244
254
return nil , err
245
255
}
@@ -387,3 +397,12 @@ func (s *PostgresStore) ClaimNextTimedOutPeer(ctx context.Context, threshold tim
387
397
388
398
return true , tx .Commit (ctx )
389
399
}
400
+
401
+ func (s * PostgresStore ) CleanEmptyLobbies (ctx context.Context , olderThan time.Time ) error {
402
+ _ , err := s .DB .Exec (ctx , `
403
+ DELETE FROM lobbies
404
+ WHERE updated_at < $1
405
+ AND peers = '{}'
406
+ ` , olderThan )
407
+ return err
408
+ }
0 commit comments