mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-16 10:11:28 +00:00
Fix memory leak in GameServer (#731)
* Convert entities to serializable after aggregating rather before * Cache instances of CustomXmlSerializer and create readonly constants for reused settings * Change CustomXmlSerializer and serializer cache to work with deserializer
This commit is contained in:
parent
0253864f5e
commit
2210541894
16 changed files with 125 additions and 147 deletions
|
@ -37,9 +37,8 @@ public class CollectionController : ControllerBase
|
|||
|
||||
GameTokenEntity token = this.GetToken();
|
||||
|
||||
List<SlotBase> slots = await this.database.Slots.Where(s => targetPlaylist.SlotIds.Contains(s.SlotId))
|
||||
.Select(s => SlotBase.CreateFromEntity(s, token))
|
||||
.ToListAsync();
|
||||
List<SlotBase> slots = (await this.database.Slots.Where(s => targetPlaylist.SlotIds.Contains(s.SlotId)).ToListAsync())
|
||||
.ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
|
||||
|
||||
int total = targetPlaylist.SlotIds.Length;
|
||||
|
||||
|
@ -101,9 +100,8 @@ public class CollectionController : ControllerBase
|
|||
|
||||
private async Task<PlaylistResponse> GetUserPlaylists(int userId)
|
||||
{
|
||||
List<GamePlaylist> playlists = await this.database.Playlists.Where(p => p.CreatorId == userId)
|
||||
.Select(p => GamePlaylist.CreateFromEntity(p))
|
||||
.ToListAsync();
|
||||
List<GamePlaylist> playlists = (await this.database.Playlists.Where(p => p.CreatorId == userId)
|
||||
.ToListAsync()).ToSerializableList(GamePlaylist.CreateFromEntity);
|
||||
int total = this.database.Playlists.Count(p => p.CreatorId == userId);
|
||||
|
||||
return new PlaylistResponse
|
||||
|
@ -189,16 +187,16 @@ public class CollectionController : ControllerBase
|
|||
|
||||
if (category is CategoryWithUser categoryWithUser)
|
||||
{
|
||||
slots = categoryWithUser.GetSlots(this.database, user, pageStart, pageSize)
|
||||
.Select(s => SlotBase.CreateFromEntity(s, token))
|
||||
.ToList();
|
||||
slots = (await categoryWithUser.GetSlots(this.database, user, pageStart, pageSize)
|
||||
.ToListAsync())
|
||||
.ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
|
||||
totalSlots = categoryWithUser.GetTotalSlots(this.database, user);
|
||||
}
|
||||
else
|
||||
{
|
||||
slots = category.GetSlots(this.database, pageStart, pageSize)
|
||||
.Select(s => SlotBase.CreateFromEntity(s, token))
|
||||
.ToList();
|
||||
.ToList()
|
||||
.ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
|
||||
totalSlots = category.GetTotalSlots(this.database);
|
||||
}
|
||||
|
||||
|
|
|
@ -47,10 +47,11 @@ public class ListController : ControllerBase
|
|||
|
||||
if (pageSize <= 0) return this.BadRequest();
|
||||
|
||||
List<SlotBase> queuedLevels = await this.filterListByRequest(gameFilterType, dateFilterType, token.GameVersion, username, ListFilterType.Queue)
|
||||
List<SlotBase> queuedLevels = (await this.filterListByRequest(gameFilterType, dateFilterType, token.GameVersion, username, ListFilterType.Queue)
|
||||
.Skip(Math.Max(0, pageStart - 1))
|
||||
.Take(Math.Min(pageSize, 30))
|
||||
.Select(s => SlotBase.CreateFromEntity(s, token)).ToListAsync();
|
||||
.ToListAsync())
|
||||
.ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
|
||||
|
||||
int total = await this.database.QueuedLevels.CountAsync(q => q.UserId == token.UserId);
|
||||
int start = pageStart + Math.Min(pageSize, 30);
|
||||
|
@ -119,11 +120,11 @@ public class ListController : ControllerBase
|
|||
UserEntity? targetUser = await this.database.Users.FirstOrDefaultAsync(u => u.Username == username);
|
||||
if (targetUser == null) return this.Forbid();
|
||||
|
||||
List<SlotBase> heartedLevels = await this.filterListByRequest(gameFilterType, dateFilterType, token.GameVersion, username, ListFilterType.FavouriteSlots)
|
||||
List<SlotBase> heartedLevels = (await this.filterListByRequest(gameFilterType, dateFilterType, token.GameVersion, username, ListFilterType.FavouriteSlots)
|
||||
.Skip(Math.Max(0, pageStart - 1))
|
||||
.Take(Math.Min(pageSize, 30))
|
||||
.Select(s => SlotBase.CreateFromEntity(s, token))
|
||||
.ToListAsync();
|
||||
.ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
|
||||
|
||||
|
||||
int total = await this.database.HeartedLevels.CountAsync(q => q.UserId == targetUser.UserId);
|
||||
int start = pageStart + Math.Min(pageSize, 30);
|
||||
|
@ -191,13 +192,12 @@ public class ListController : ControllerBase
|
|||
int targetUserId = await this.database.UserIdFromUsername(username);
|
||||
if (targetUserId == 0) return this.Forbid();
|
||||
|
||||
List<GamePlaylist> heartedPlaylists = await this.database.HeartedPlaylists.Where(p => p.UserId == targetUserId)
|
||||
List<GamePlaylist> heartedPlaylists = (await this.database.HeartedPlaylists.Where(p => p.UserId == targetUserId)
|
||||
.Include(p => p.Playlist)
|
||||
.Include(p => p.Playlist.Creator)
|
||||
.OrderByDescending(p => p.HeartedPlaylistId)
|
||||
.Select(p => p.Playlist)
|
||||
.Select(p => GamePlaylist.CreateFromEntity(p))
|
||||
.ToListAsync();
|
||||
.ToListAsync()).ToSerializableList(GamePlaylist.CreateFromEntity);
|
||||
|
||||
int total = await this.database.HeartedPlaylists.CountAsync(p => p.UserId == targetUserId);
|
||||
|
||||
|
@ -250,15 +250,13 @@ public class ListController : ControllerBase
|
|||
|
||||
if (pageSize <= 0) return this.BadRequest();
|
||||
|
||||
List<GameUser> heartedProfiles = await this.database.HeartedProfiles.Include
|
||||
(h => h.HeartedUser)
|
||||
List<GameUser> heartedProfiles = (await this.database.HeartedProfiles.Include(h => h.HeartedUser)
|
||||
.OrderBy(h => h.HeartedProfileId)
|
||||
.Where(h => h.UserId == targetUser.UserId)
|
||||
.Select(h => h.HeartedUser)
|
||||
.Skip(Math.Max(0, pageStart - 1))
|
||||
.Take(Math.Min(pageSize, 30))
|
||||
.Select(h => GameUser.CreateFromEntity(h, token.GameVersion))
|
||||
.ToListAsync();
|
||||
.ToListAsync()).ToSerializableList(u => GameUser.CreateFromEntity(u, token.GameVersion));
|
||||
|
||||
int total = await this.database.HeartedProfiles.CountAsync(h => h.UserId == targetUser.UserId);
|
||||
|
||||
|
|
|
@ -153,14 +153,13 @@ public class ReviewController : ControllerBase
|
|||
SlotEntity? slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == slotId);
|
||||
if (slot == null) return this.BadRequest();
|
||||
|
||||
List<GameReview> reviews = await this.database.Reviews.ByGameVersion(gameVersion, true)
|
||||
List<GameReview> reviews = (await this.database.Reviews.ByGameVersion(gameVersion, true)
|
||||
.Where(r => r.SlotId == slotId)
|
||||
.OrderByDescending(r => r.ThumbsUp - r.ThumbsDown)
|
||||
.ThenByDescending(r => r.Timestamp)
|
||||
.Skip(Math.Max(0, pageStart - 1))
|
||||
.Take(Math.Min(pageSize, 30))
|
||||
.Select(r => GameReview.CreateFromEntity(r, token))
|
||||
.ToListAsync();
|
||||
.ToListAsync()).ToSerializableList(r => GameReview.CreateFromEntity(r, token));
|
||||
|
||||
|
||||
return this.Ok(new ReviewResponse(reviews, reviews.LastOrDefault()?.Timestamp ?? TimeHelper.TimestampMillis, pageStart + Math.Min(pageSize, 30)));
|
||||
|
@ -179,13 +178,12 @@ public class ReviewController : ControllerBase
|
|||
|
||||
if (targetUserId == 0) return this.BadRequest();
|
||||
|
||||
List<GameReview> reviews = await this.database.Reviews.ByGameVersion(gameVersion, true)
|
||||
List<GameReview> reviews = (await this.database.Reviews.ByGameVersion(gameVersion, true)
|
||||
.Where(r => r.ReviewerId == targetUserId)
|
||||
.OrderByDescending(r => r.Timestamp)
|
||||
.Skip(Math.Max(0, pageStart - 1))
|
||||
.Take(Math.Min(pageSize, 30))
|
||||
.Select(r => GameReview.CreateFromEntity(r, token))
|
||||
.ToListAsync();
|
||||
.ToListAsync()).ToSerializableList(r => GameReview.CreateFromEntity(r, token));
|
||||
|
||||
return this.Ok(new ReviewResponse(reviews, reviews.LastOrDefault()?.Timestamp ?? TimeHelper.TimestampMillis, pageStart));
|
||||
}
|
||||
|
|
|
@ -322,7 +322,7 @@ public class ScoreController : ControllerBase
|
|||
// Paginated viewing: if not requesting pageStart, get results around user
|
||||
var pagedScores = rankedScores.Skip(options.PageStart != -1 || myScore == null ? options.PageStart - 1 : myScore.Rank - 3).Take(Math.Min(options.PageSize, 30));
|
||||
|
||||
List<GameScore> gameScores = pagedScores.Select(ps => GameScore.CreateFromEntity(ps.Score, ps.Rank)).ToList();
|
||||
List<GameScore> gameScores = pagedScores.ToSerializableList(ps => GameScore.CreateFromEntity(ps.Score, ps.Rank));
|
||||
|
||||
return new ScoreboardResponse(options.RootName, gameScores, rankedScores.Count, myScore?.Score.Points ?? 0, myScore?.Rank ?? 0);
|
||||
}
|
||||
|
|
|
@ -61,10 +61,10 @@ public class SearchController : ControllerBase
|
|||
s.SlotId.ToString().Equals(keyword)
|
||||
);
|
||||
|
||||
List<SlotBase> slots = await dbQuery.Skip(Math.Max(0, pageStart - 1))
|
||||
List<SlotBase> slots = (await dbQuery.Skip(Math.Max(0, pageStart - 1))
|
||||
.Take(Math.Min(pageSize, 30))
|
||||
.Select(s => SlotBase.CreateFromEntity(s, token))
|
||||
.ToListAsync();
|
||||
.ToListAsync())
|
||||
.ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
|
||||
|
||||
return this.Ok(new GenericSlotResponse(keyName, slots, await dbQuery.CountAsync(), 0));
|
||||
}
|
||||
|
|
|
@ -40,12 +40,11 @@ public class SlotsController : ControllerBase
|
|||
|
||||
int usedSlots = this.database.Slots.Count(s => s.CreatorId == targetUserId);
|
||||
|
||||
List<SlotBase> slots = await this.database.Slots.Where(s => s.CreatorId == targetUserId)
|
||||
List<SlotBase> slots = (await this.database.Slots.Where(s => s.CreatorId == targetUserId)
|
||||
.ByGameVersion(token.GameVersion, token.UserId == targetUserId)
|
||||
.Skip(Math.Max(0, pageStart - 1))
|
||||
.Take(Math.Min(pageSize, usedSlots))
|
||||
.Select(s => SlotBase.CreateFromEntity(s, token))
|
||||
.ToListAsync();
|
||||
.ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
|
||||
|
||||
int start = pageStart + Math.Min(pageSize, usedSlots);
|
||||
int total = await this.database.Slots.CountAsync(s => s.CreatorId == targetUserId);
|
||||
|
@ -158,12 +157,11 @@ public class SlotsController : ControllerBase
|
|||
|
||||
GameVersion gameVersion = token.GameVersion;
|
||||
|
||||
List<SlotBase> slots = await this.database.Slots.ByGameVersion(gameVersion, false, true)
|
||||
List<SlotBase> slots = (await this.database.Slots.ByGameVersion(gameVersion, false, true)
|
||||
.OrderByDescending(s => s.FirstUploaded)
|
||||
.Skip(Math.Max(0, pageStart - 1))
|
||||
.Take(Math.Min(pageSize, 30))
|
||||
.Select(s => SlotBase.CreateFromEntity(s, token))
|
||||
.ToListAsync();
|
||||
.ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
|
||||
|
||||
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
|
||||
int total = await StatisticsHelper.SlotCountForGame(this.database, token.GameVersion);
|
||||
|
@ -192,13 +190,12 @@ public class SlotsController : ControllerBase
|
|||
.Select(r => r.SlotId)
|
||||
.ToList();
|
||||
|
||||
List<SlotBase> slots = await this.database.Slots.ByGameVersion(gameVersion, false, true)
|
||||
List<SlotBase> slots = (await this.database.Slots.ByGameVersion(gameVersion, false, true)
|
||||
.Where(s => slotIdsWithTag.Contains(s.SlotId))
|
||||
.OrderByDescending(s => s.PlaysLBP1)
|
||||
.Skip(Math.Max(0, pageStart - 1))
|
||||
.Take(Math.Min(pageSize, 30))
|
||||
.Select(s => SlotBase.CreateFromEntity(s, token))
|
||||
.ToListAsync();
|
||||
.ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
|
||||
|
||||
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
|
||||
int total = slotIdsWithTag.Count;
|
||||
|
@ -215,13 +212,12 @@ public class SlotsController : ControllerBase
|
|||
|
||||
GameVersion gameVersion = token.GameVersion;
|
||||
|
||||
List<SlotBase> slots = await this.database.Slots.ByGameVersion(gameVersion, false, true)
|
||||
List<SlotBase> slots = (await this.database.Slots.ByGameVersion(gameVersion, false, true)
|
||||
.ToAsyncEnumerable()
|
||||
.OrderByDescending(s => s.RatingLBP1)
|
||||
.Skip(Math.Max(0, pageStart - 1))
|
||||
.Take(Math.Min(pageSize, 30))
|
||||
.Select(s => SlotBase.CreateFromEntity(s, token))
|
||||
.ToListAsync();
|
||||
.ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
|
||||
|
||||
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
|
||||
int total = await StatisticsHelper.SlotCount(this.database);
|
||||
|
@ -241,13 +237,12 @@ public class SlotsController : ControllerBase
|
|||
.Select(s => s.SlotId)
|
||||
.ToListAsync();
|
||||
|
||||
List<SlotBase> slots = await this.database.Slots.Where(s => slotIdsWithTag.Contains(s.SlotId))
|
||||
List<SlotBase> slots = (await this.database.Slots.Where(s => slotIdsWithTag.Contains(s.SlotId))
|
||||
.ByGameVersion(token.GameVersion, false, true)
|
||||
.OrderByDescending(s => s.PlaysLBP1)
|
||||
.Skip(Math.Max(0, pageStart - 1))
|
||||
.Take(Math.Min(pageSize, 30))
|
||||
.Select(s => SlotBase.CreateFromEntity(s, token))
|
||||
.ToListAsync();
|
||||
.ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
|
||||
|
||||
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
|
||||
int total = slotIdsWithTag.Count;
|
||||
|
@ -262,13 +257,12 @@ public class SlotsController : ControllerBase
|
|||
|
||||
if (pageSize <= 0) return this.BadRequest();
|
||||
|
||||
List<SlotBase> slots = await this.database.Slots.Where(s => s.TeamPick)
|
||||
List<SlotBase> slots = (await this.database.Slots.Where(s => s.TeamPick)
|
||||
.ByGameVersion(token.GameVersion, false, true)
|
||||
.OrderByDescending(s => s.LastUpdated)
|
||||
.Skip(Math.Max(0, pageStart - 1))
|
||||
.Take(Math.Min(pageSize, 30))
|
||||
.Select(s => SlotBase.CreateFromEntity(s, token))
|
||||
.ToListAsync();
|
||||
.ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
|
||||
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
|
||||
int total = await StatisticsHelper.TeamPickCountForGame(this.database, token.GameVersion);
|
||||
|
||||
|
@ -284,11 +278,10 @@ public class SlotsController : ControllerBase
|
|||
|
||||
GameVersion gameVersion = token.GameVersion;
|
||||
|
||||
List<SlotBase> slots = await this.database.Slots.ByGameVersion(gameVersion, false, true)
|
||||
List<SlotBase> slots = (await this.database.Slots.ByGameVersion(gameVersion, false, true)
|
||||
.OrderBy(_ => EF.Functions.Random())
|
||||
.Take(Math.Min(pageSize, 30))
|
||||
.Select(s => SlotBase.CreateFromEntity(s, token))
|
||||
.ToListAsync();
|
||||
.ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
|
||||
|
||||
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
|
||||
int total = await StatisticsHelper.SlotCountForGame(this.database, token.GameVersion);
|
||||
|
@ -313,14 +306,13 @@ public class SlotsController : ControllerBase
|
|||
|
||||
Random rand = new();
|
||||
|
||||
List<SlotBase> slots = await this.filterByRequest(gameFilterType, dateFilterType, token.GameVersion)
|
||||
List<SlotBase> slots = (await this.filterByRequest(gameFilterType, dateFilterType, token.GameVersion)
|
||||
.AsAsyncEnumerable()
|
||||
.OrderByDescending(s => s.Thumbsup)
|
||||
.ThenBy(_ => rand.Next())
|
||||
.Skip(Math.Max(0, pageStart - 1))
|
||||
.Take(Math.Min(pageSize, 30))
|
||||
.Select(s => SlotBase.CreateFromEntity(s, token))
|
||||
.ToListAsync();
|
||||
.ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
|
||||
|
||||
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
|
||||
int total = await StatisticsHelper.SlotCountForGame(this.database, token.GameVersion);
|
||||
|
@ -345,7 +337,7 @@ public class SlotsController : ControllerBase
|
|||
|
||||
Random rand = new();
|
||||
|
||||
List<SlotBase> slots = await this.filterByRequest(gameFilterType, dateFilterType, token.GameVersion)
|
||||
List<SlotBase> slots = (await this.filterByRequest(gameFilterType, dateFilterType, token.GameVersion)
|
||||
.AsAsyncEnumerable()
|
||||
.OrderByDescending(
|
||||
// probably not the best way to do this?
|
||||
|
@ -363,8 +355,7 @@ public class SlotsController : ControllerBase
|
|||
.ThenBy(_ => rand.Next())
|
||||
.Skip(Math.Max(0, pageStart - 1))
|
||||
.Take(Math.Min(pageSize, 30))
|
||||
.Select(s => SlotBase.CreateFromEntity(s, token))
|
||||
.ToListAsync();
|
||||
.ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
|
||||
|
||||
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
|
||||
int total = await StatisticsHelper.SlotCountForGame(this.database, token.GameVersion);
|
||||
|
@ -387,14 +378,13 @@ public class SlotsController : ControllerBase
|
|||
|
||||
if (pageSize <= 0) return this.BadRequest();
|
||||
|
||||
List<SlotBase> slots = await this.filterByRequest(gameFilterType, dateFilterType, token.GameVersion)
|
||||
List<SlotBase> slots = (await this.filterByRequest(gameFilterType, dateFilterType, token.GameVersion)
|
||||
.AsAsyncEnumerable()
|
||||
.OrderByDescending(s => s.Hearts)
|
||||
.ThenBy(_ => RandomNumberGenerator.GetInt32(int.MaxValue))
|
||||
.Skip(Math.Max(0, pageStart - 1))
|
||||
.Take(Math.Min(pageSize, 30))
|
||||
.Select(s => SlotBase.CreateFromEntity(s, token))
|
||||
.ToListAsync();
|
||||
.ToListAsync()).ToSerializableList(s => SlotBase.CreateFromEntity(s, token));
|
||||
|
||||
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
|
||||
int total = await StatisticsHelper.SlotCountForGame(this.database, token.GameVersion);
|
||||
|
@ -443,13 +433,12 @@ public class SlotsController : ControllerBase
|
|||
|
||||
foreach (int slotId in orderedPlayersBySlotId)
|
||||
{
|
||||
SlotBase? slot = await this.database.Slots.ByGameVersion(token.GameVersion, false, true)
|
||||
SlotEntity? slot = await this.database.Slots.ByGameVersion(token.GameVersion, false, true)
|
||||
.Where(s => s.SlotId == slotId)
|
||||
.Select(s => SlotBase.CreateFromEntity(s, token))
|
||||
.FirstOrDefaultAsync();
|
||||
if (slot == null) continue; // shouldn't happen ever unless the room is borked
|
||||
|
||||
slots.Add(slot);
|
||||
slots.Add(SlotBase.CreateFromEntity(slot, token));
|
||||
}
|
||||
|
||||
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue