Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions backend/handlers/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func DeleteRoom(c *gin.Context) {
user, err := authenticateUser(c, deleteRoomRequest.UserID)
if err != nil {
if err == mongo.ErrNoDocuments { // user not found
c.JSON(http.StatusNotFound, gin.H{"error": "user not found"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "user not found"})
return
}
// db error
Expand Down Expand Up @@ -103,7 +103,7 @@ func JoinRoom(c *gin.Context) {
user, err := authenticateUser(c, joinRoomRequest.UserID)
if err != nil {
if err == mongo.ErrNoDocuments { // user not found
c.JSON(http.StatusNotFound, gin.H{"error": "user not found"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "user not found"})
return
}
// db error
Expand Down Expand Up @@ -131,7 +131,7 @@ func LeaveRoom(c *gin.Context) {
_, err := authenticateUser(c, leaveRoomRequest.UserID)
if err != nil {
if err == mongo.ErrNoDocuments { // user not found
c.JSON(http.StatusNotFound, gin.H{"error": "user not found"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "user not found"})
return
}
// db error
Expand Down Expand Up @@ -159,7 +159,7 @@ func UpdateRoom(c *gin.Context) {
_, err := authenticateUser(c, updateRoomRequest.UserID)
if err != nil {
if err == mongo.ErrNoDocuments { // user not found
c.JSON(http.StatusNotFound, gin.H{"error": "user not found"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "user not found"})
return
}
// db error
Expand Down
1 change: 1 addition & 0 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func main() {

go services.RegisterRooms(ctx)
go services.UnregisterRooms(ctx)
go services.ExpireRooms(ctx)

sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
Expand Down
37 changes: 37 additions & 0 deletions backend/services/goroutines.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"goderpad/db"
"goderpad/utils"
)

// RegisterRooms listens for rooms to be registered and adds them to the Hub (in-memory store)
Expand Down Expand Up @@ -39,3 +40,39 @@ func UnregisterRooms(ctx context.Context) {
}
}
}

// ExpireRooms periodically checks for rooms that have been inactive for over a week and deletes them
func ExpireRooms(ctx context.Context) {
ticker := utils.CreateTicker(utils.HOUR)
defer ticker.Stop()

for {
select {
case <-ticker.C:
allRooms, err := db.GetAllRooms(ctx)
if err != nil {
log.Println("Failed to fetch rooms for expiration:", err)
continue
}

now := utils.GetCurrentUnixTimestamp()
for _, room := range allRooms {
if now-room.LastUsed > int64(utils.WEEK) {
// we don't delete from Hub because if it's in memory, it's active
hub := GetHub()
if roomFromHub := hub.GetRoom(room.ID); roomFromHub != nil {
continue
}
err := db.DeleteRoomByID(ctx, room.ID)
if err != nil {
log.Println("Failed to delete expired room:", err)
} else {
log.Println("Expired room deleted:", room.ID)
}
}
}
case <-ctx.Done():
return
}
}
}
2 changes: 0 additions & 2 deletions backend/services/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
)

func CreateRoom(ctx context.Context, request models.CreateRoomRequest) (string, error) {
hub := GetHub()
roomID := utils.GenerateUUID()

room := &models.Room{
Expand All @@ -28,7 +27,6 @@ func CreateRoom(ctx context.Context, request models.CreateRoomRequest) (string,
}

// Owner won't be added right away -> only when they open the room (join), they will be added
hub.Register <- room
return roomID, nil
}

Expand Down
7 changes: 7 additions & 0 deletions backend/utils/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ import (
func GetCurrentUnixTimestamp() int64 {
return time.Now().Unix()
}

func CreateTicker(duration time.Duration) *time.Ticker {
return time.NewTicker(duration)
}

const HOUR = 1 * time.Hour
const WEEK = 7 * 24 * HOUR