Skip to content

Commit

Permalink
fix issue with random concurrency issue on interaction reaction
Browse files Browse the repository at this point in the history
  • Loading branch information
RaidMax committed Oct 24, 2022
1 parent d81e3e4 commit 4557597
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
37 changes: 26 additions & 11 deletions Application/Misc/InteractionRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public InteractionRegistration(ILogger<InteractionRegistration> logger, IService

public void RegisterScriptInteraction(string interactionName, string source, Delegate interactionRegistration)
{
if (string.IsNullOrWhiteSpace(source))
{
throw new ArgumentException("Script interaction source cannot be null");
}

_logger.LogDebug("Registering script interaction {InteractionName} from {Source}", interactionName, source);

var plugin = _serviceProvider.GetRequiredService<IEnumerable<IPlugin>>()
.FirstOrDefault(plugin => plugin.Name == source);

Expand All @@ -36,45 +43,50 @@ public void RegisterScriptInteraction(string interactionName, string source, Del
return;
}

var wrappedDelegate = (int? clientId, Reference.Game? game, CancellationToken token) =>
Task<IInteractionData> WrappedDelegate(int? clientId, Reference.Game? game, CancellationToken token) =>
Task.FromResult(
scriptPlugin.WrapDelegate<IInteractionData>(interactionRegistration, clientId, game, token));
scriptPlugin.WrapDelegate<IInteractionData>(interactionRegistration, token, clientId, game, token));

if (!_interactions.ContainsKey(interactionName))
{
_interactions.TryAdd(interactionName, wrappedDelegate);
_interactions.TryAdd(interactionName, WrappedDelegate);
}
else
{
_interactions[interactionName] = wrappedDelegate;
_interactions[interactionName] = WrappedDelegate;
}
}

public void RegisterInteraction(string interactionName, InteractionRegistrationCallback interactionRegistration)
{
if (!_interactions.ContainsKey(interactionName))
{
_logger.LogDebug("Registering interaction {InteractionName}", interactionName);
_interactions.TryAdd(interactionName, interactionRegistration);
}
else
{
_logger.LogDebug("Updating interaction {InteractionName}", interactionName);
_interactions[interactionName] = interactionRegistration;
}
}

public void UnregisterInteraction(string interactionName)
{
if (_interactions.ContainsKey(interactionName))
if (!_interactions.ContainsKey(interactionName))
{
_interactions.TryRemove(interactionName, out _);
return;
}

_logger.LogDebug("Unregistering interaction {InteractionName}", interactionName);
_interactions.TryRemove(interactionName, out _);
}

public async Task<IEnumerable<IInteractionData>> GetInteractions(string interactionPrefix = null,
int? clientId = null,
Reference.Game? game = null, CancellationToken token = default)
{
return await GetInteractionsPrivate(interactionPrefix, clientId, game, token);
return await GetInteractionsInternal(interactionPrefix, clientId, game, token);
}

public async Task<string> ProcessInteraction(string interactionId, int originId, int? targetId = null,
Expand Down Expand Up @@ -103,7 +115,7 @@ public async Task<string> ProcessInteraction(string interactionId, int originId,
continue;
}

return scriptPlugin.ExecuteAction<string>(interaction.ScriptAction, originId, targetId, game, meta,
return scriptPlugin.ExecuteAction<string>(interaction.ScriptAction, token, originId, targetId, game, meta,
token);
}
}
Expand All @@ -118,10 +130,10 @@ public async Task<string> ProcessInteraction(string interactionId, int originId,
return null;
}

private async Task<IEnumerable<IInteractionData>> GetInteractionsPrivate(string prefix = null, int? clientId = null,
private async Task<IEnumerable<IInteractionData>> GetInteractionsInternal(string prefix = null, int? clientId = null,
Reference.Game? game = null, CancellationToken token = default)
{
return (await Task.WhenAll(_interactions
var interactions = _interactions
.Where(interaction => string.IsNullOrWhiteSpace(prefix) || interaction.Key.StartsWith(prefix)).Select(
async kvp =>
{
Expand All @@ -137,6 +149,9 @@ private async Task<IEnumerable<IInteractionData>> GetInteractionsPrivate(string
clientId);
return null;
}
}))).Where(interaction => interaction is not null);
}).Where(interaction => interaction is not null)
.ToList();

return await Task.WhenAll(interactions);
}
}
11 changes: 11 additions & 0 deletions WebfrontCore/Controllers/ActionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ public IActionResult DynamicActionForm(int? id, string meta)
}
});
}

if (meta is null)
{
return BadRequest(new[]
{
new CommandResponseInfo
{
Response = "INVALID"
}
});
}

var metaDict = JsonSerializer.Deserialize<Dictionary<string, string>>(meta.TrimEnd('"').TrimStart('"'));

Expand Down

0 comments on commit 4557597

Please sign in to comment.