potential optimizations (ConfigureAwait, Logging Templates)

This commit is contained in:
Stanley Dimant
2022-08-22 10:46:37 +02:00
parent 98ee6c7f66
commit 927e1aaceb
11 changed files with 182 additions and 179 deletions

View File

@@ -18,14 +18,14 @@ namespace MareSynchronosServer.Hubs
[HubMethodName(Api.SendUserDeleteAccount)]
public async Task DeleteAccount()
{
_logger.LogInformation("User " + AuthenticatedUserId + " deleted their account");
_logger.LogInformation("User {AuthenticatedUserId} deleted their account", AuthenticatedUserId);
string userid = AuthenticatedUserId;
var userEntry = await _dbContext.Users.SingleAsync(u => u.UID == userid);
var ownPairData = await _dbContext.ClientPairs.Where(u => u.User.UID == userid).ToListAsync();
var auth = await _dbContext.Auth.SingleAsync(u => u.UserUID == userid);
var lodestone = await _dbContext.LodeStoneAuth.SingleOrDefaultAsync(a => a.User.UID == userid);
var userEntry = await _dbContext.Users.SingleAsync(u => u.UID == userid).ConfigureAwait(false);
var ownPairData = await _dbContext.ClientPairs.Where(u => u.User.UID == userid).ToListAsync().ConfigureAwait(false);
var auth = await _dbContext.Auth.SingleAsync(u => u.UserUID == userid).ConfigureAwait(false);
var lodestone = await _dbContext.LodeStoneAuth.SingleOrDefaultAsync(a => a.User.UID == userid).ConfigureAwait(false);
if (lodestone != null)
{
@@ -34,7 +34,7 @@ namespace MareSynchronosServer.Hubs
while (_dbContext.Files.Any(f => f.Uploader == userEntry))
{
await Task.Delay(1000);
await Task.Delay(1000).ConfigureAwait(false);
}
SecretKeyAuthenticationHandler.RemoveAuthentication(userid);
@@ -43,9 +43,9 @@ namespace MareSynchronosServer.Hubs
MareMetrics.PairsPaused.Dec(ownPairData.Count(c => c.IsPaused));
_dbContext.RemoveRange(ownPairData);
await _dbContext.SaveChangesAsync();
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
var otherPairData = await _dbContext.ClientPairs.Include(u => u.User)
.Where(u => u.OtherUser.UID == userid).ToListAsync();
.Where(u => u.OtherUser.UID == userid).ToListAsync().ConfigureAwait(false);
foreach (var pair in otherPairData)
{
await Clients.User(pair.User.UID)
@@ -53,38 +53,38 @@ namespace MareSynchronosServer.Hubs
{
OtherUID = userid,
IsRemoved = true
}, userEntry.CharacterIdentification);
}, userEntry.CharacterIdentification).ConfigureAwait(false);
}
MareMetrics.Pairs.Dec(otherPairData.Count());
MareMetrics.Pairs.Dec(otherPairData.Count);
MareMetrics.PairsPaused.Dec(otherPairData.Count(c => c.IsPaused));
MareMetrics.UsersRegistered.Dec();
_dbContext.RemoveRange(otherPairData);
_dbContext.Remove(userEntry);
_dbContext.Remove(auth);
await _dbContext.SaveChangesAsync();
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
}
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
[HubMethodName(Api.InvokeUserGetOnlineCharacters)]
public async Task<List<string>> GetOnlineCharacters()
{
_logger.LogInformation("User " + AuthenticatedUserId + " requested online characters");
_logger.LogInformation("User {AuthenticatedUserId} requested online characters", AuthenticatedUserId);
var ownUser = await GetAuthenticatedUserUntrackedAsync();
var ownUser = await GetAuthenticatedUserUntrackedAsync().ConfigureAwait(false);
var otherUsers = await _dbContext.ClientPairs.AsNoTracking()
.Include(u => u.User)
.Include(u => u.OtherUser)
.Where(w => w.User.UID == ownUser.UID && !w.IsPaused)
.Where(w => !string.IsNullOrEmpty(w.OtherUser.CharacterIdentification))
.Select(e => e.OtherUser).ToListAsync();
.Include(u => u.User)
.Include(u => u.OtherUser)
.Where(w => w.User.UID == ownUser.UID && !w.IsPaused)
.Where(w => !string.IsNullOrEmpty(w.OtherUser.CharacterIdentification))
.Select(e => e.OtherUser).ToListAsync().ConfigureAwait(false);
var otherEntries = await _dbContext.ClientPairs.AsNoTracking()
.Include(u => u.User)
.Where(u => otherUsers.Any(e => e == u.User) && u.OtherUser == ownUser && !u.IsPaused).ToListAsync();
.Where(u => otherUsers.Any(e => e == u.User) && u.OtherUser == ownUser && !u.IsPaused).ToListAsync().ConfigureAwait(false);
await Clients.Users(otherEntries.Select(e => e.User.UID)).SendAsync(Api.OnUserAddOnlinePairedPlayer, ownUser.CharacterIdentification);
await Clients.Users(otherEntries.Select(e => e.User.UID)).SendAsync(Api.OnUserAddOnlinePairedPlayer, ownUser.CharacterIdentification).ConfigureAwait(false);
return otherEntries.Select(e => e.User.CharacterIdentification).Distinct().ToList();
}
@@ -117,7 +117,7 @@ namespace MareSynchronosServer.Hubs
IsSynced = otherEntry != null
};
return (await query.ToListAsync()).Select(f => new ClientPairDto()
return (await query.ToListAsync().ConfigureAwait(false)).Select(f => new ClientPairDto()
{
IsPaused = f.IsPaused,
OtherUID = f.OtherUserUID,
@@ -130,9 +130,9 @@ namespace MareSynchronosServer.Hubs
[HubMethodName(Api.InvokeUserPushCharacterDataToVisibleClients)]
public async Task PushCharacterDataToVisibleClients(CharacterCacheDto characterCache, List<string> visibleCharacterIds)
{
_logger.LogInformation("User " + AuthenticatedUserId + " pushing character data to " + visibleCharacterIds.Count + " visible clients");
_logger.LogInformation("User {AuthenticatedUserId} pushing character data to {visibleCharacterIds} visible clients", AuthenticatedUserId, visibleCharacterIds.Count);
var user = await GetAuthenticatedUserUntrackedAsync();
var user = await GetAuthenticatedUserUntrackedAsync().ConfigureAwait(false);
var query =
from userToOther in _dbContext.ClientPairs
@@ -154,9 +154,9 @@ namespace MareSynchronosServer.Hubs
&& visibleCharacterIds.Contains(userToOther.OtherUser.CharacterIdentification)
select otherToUser.UserUID;
var otherEntries = await query.ToListAsync();
var otherEntries = await query.ToListAsync().ConfigureAwait(false);
await Clients.Users(otherEntries).SendAsync(Api.OnUserReceiveCharacterData, characterCache, user.CharacterIdentification);
await Clients.Users(otherEntries).SendAsync(Api.OnUserReceiveCharacterData, characterCache, user.CharacterIdentification).ConfigureAwait(false);
MareMetrics.UserPushData.Inc();
MareMetrics.UserPushDataTo.Inc(otherEntries.Count);
@@ -168,24 +168,24 @@ namespace MareSynchronosServer.Hubs
{
if (uid == AuthenticatedUserId) return;
uid = uid.Trim();
var user = await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId);
var user = await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId).ConfigureAwait(false);
var otherUser = await _dbContext.Users
.SingleOrDefaultAsync(u => u.UID == uid);
.SingleOrDefaultAsync(u => u.UID == uid).ConfigureAwait(false);
var existingEntry =
await _dbContext.ClientPairs.AsNoTracking()
.FirstOrDefaultAsync(p =>
p.User.UID == AuthenticatedUserId && p.OtherUser.UID == uid);
p.User.UID == AuthenticatedUserId && p.OtherUser.UID == uid).ConfigureAwait(false);
if (otherUser == null || existingEntry != null) return;
_logger.LogInformation("User " + AuthenticatedUserId + " adding " + uid + " to whitelist");
_logger.LogInformation("User {AuthenticatedUserId} adding {uid} to whitelist", AuthenticatedUserId, uid);
ClientPair wl = new ClientPair()
{
IsPaused = false,
OtherUser = otherUser,
User = user
};
await _dbContext.ClientPairs.AddAsync(wl);
await _dbContext.SaveChangesAsync();
await _dbContext.ClientPairs.AddAsync(wl).ConfigureAwait(false);
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
var otherEntry = OppositeEntry(uid);
await Clients.User(user.UID)
.SendAsync(Api.OnUserUpdateClientPairs, new ClientPairDto()
@@ -194,7 +194,7 @@ namespace MareSynchronosServer.Hubs
IsPaused = false,
IsPausedFromOthers = otherEntry?.IsPaused ?? false,
IsSynced = otherEntry != null
}, string.Empty);
}, string.Empty).ConfigureAwait(false);
if (otherEntry != null)
{
await Clients.User(uid).SendAsync(Api.OnUserUpdateClientPairs,
@@ -204,14 +204,14 @@ namespace MareSynchronosServer.Hubs
IsPaused = otherEntry.IsPaused,
IsPausedFromOthers = false,
IsSynced = true
}, user.CharacterIdentification);
}, user.CharacterIdentification).ConfigureAwait(false);
if (!string.IsNullOrEmpty(otherUser.CharacterIdentification))
{
await Clients.User(user.UID)
.SendAsync(Api.OnUserAddOnlinePairedPlayer, otherUser.CharacterIdentification);
.SendAsync(Api.OnUserAddOnlinePairedPlayer, otherUser.CharacterIdentification).ConfigureAwait(false);
await Clients.User(otherUser.UID)
.SendAsync(Api.OnUserAddOnlinePairedPlayer, user.CharacterIdentification);
.SendAsync(Api.OnUserAddOnlinePairedPlayer, user.CharacterIdentification).ConfigureAwait(false);
}
}
@@ -223,15 +223,15 @@ namespace MareSynchronosServer.Hubs
public async Task SendPairedClientPauseChange(string otherUserUid, bool isPaused)
{
if (otherUserUid == AuthenticatedUserId) return;
ClientPair pair = await _dbContext.ClientPairs.SingleOrDefaultAsync(w => w.UserUID == AuthenticatedUserId && w.OtherUserUID == otherUserUid);
ClientPair pair = await _dbContext.ClientPairs.SingleOrDefaultAsync(w => w.UserUID == AuthenticatedUserId && w.OtherUserUID == otherUserUid).ConfigureAwait(false);
if (pair == null) return;
_logger.LogInformation("User " + AuthenticatedUserId + " changed pause status with " + otherUserUid + " to " + isPaused);
_logger.LogInformation("User {AuthenticatedUserId} changed pause status with {otherUserUid} to {isPaused}", AuthenticatedUserId, otherUserUid, isPaused);
pair.IsPaused = isPaused;
_dbContext.Update(pair);
await _dbContext.SaveChangesAsync();
var selfCharaIdent = (await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId)).CharacterIdentification;
var otherCharaIdent = (await _dbContext.Users.SingleAsync(u => u.UID == otherUserUid)).CharacterIdentification;
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
var selfCharaIdent = (await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId).ConfigureAwait(false)).CharacterIdentification;
var otherCharaIdent = (await _dbContext.Users.SingleAsync(u => u.UID == otherUserUid).ConfigureAwait(false)).CharacterIdentification;
var otherEntry = OppositeEntry(otherUserUid);
await Clients.User(AuthenticatedUserId)
@@ -241,7 +241,7 @@ namespace MareSynchronosServer.Hubs
IsPaused = isPaused,
IsPausedFromOthers = otherEntry?.IsPaused ?? false,
IsSynced = otherEntry != null
}, otherCharaIdent);
}, otherCharaIdent).ConfigureAwait(false);
if (otherEntry != null)
{
await Clients.User(otherUserUid).SendAsync(Api.OnUserUpdateClientPairs, new ClientPairDto()
@@ -250,7 +250,7 @@ namespace MareSynchronosServer.Hubs
IsPaused = otherEntry.IsPaused,
IsPausedFromOthers = isPaused,
IsSynced = true
}, selfCharaIdent);
}, selfCharaIdent).ConfigureAwait(false);
}
if (isPaused)
@@ -269,37 +269,37 @@ namespace MareSynchronosServer.Hubs
{
if (uid == AuthenticatedUserId) return;
var sender = await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId);
var otherUser = await _dbContext.Users.SingleOrDefaultAsync(u => u.UID == uid);
var sender = await _dbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId).ConfigureAwait(false);
var otherUser = await _dbContext.Users.SingleOrDefaultAsync(u => u.UID == uid).ConfigureAwait(false);
if (otherUser == null) return;
_logger.LogInformation("User " + AuthenticatedUserId + " removed " + uid + " from whitelist");
_logger.LogInformation("User {AuthenticatedUserId} removed {uid} from whitelist", AuthenticatedUserId, uid);
ClientPair wl =
await _dbContext.ClientPairs.SingleOrDefaultAsync(w => w.User == sender && w.OtherUser == otherUser);
await _dbContext.ClientPairs.SingleOrDefaultAsync(w => w.User == sender && w.OtherUser == otherUser).ConfigureAwait(false);
if (wl == null) return;
_dbContext.ClientPairs.Remove(wl);
await _dbContext.SaveChangesAsync();
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
var otherEntry = OppositeEntry(uid);
await Clients.User(sender.UID)
.SendAsync(Api.OnUserUpdateClientPairs, new ClientPairDto()
{
OtherUID = otherUser.UID,
IsRemoved = true
}, otherUser.CharacterIdentification);
}, otherUser.CharacterIdentification).ConfigureAwait(false);
if (otherEntry != null)
{
if (!string.IsNullOrEmpty(otherUser.CharacterIdentification))
{
await Clients.User(sender.UID)
.SendAsync(Api.OnUserRemoveOnlinePairedPlayer, otherUser.CharacterIdentification);
.SendAsync(Api.OnUserRemoveOnlinePairedPlayer, otherUser.CharacterIdentification).ConfigureAwait(false);
await Clients.User(otherUser.UID)
.SendAsync(Api.OnUserRemoveOnlinePairedPlayer, sender.CharacterIdentification);
.SendAsync(Api.OnUserRemoveOnlinePairedPlayer, sender.CharacterIdentification).ConfigureAwait(false);
await Clients.User(otherUser.UID).SendAsync(Api.OnUserUpdateClientPairs, new ClientPairDto()
{
OtherUID = sender.UID,
IsPaused = otherEntry.IsPaused,
IsPausedFromOthers = false,
IsSynced = false
}, sender.CharacterIdentification);
}, sender.CharacterIdentification).ConfigureAwait(false);
}
}