[core] Parallelize rule execution

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-12-04 15:05:09 +01:00
parent ee5e7359a2
commit a07ad6fe83
Signed by: luzifer
GPG Key ID: D91C3E91E4CAD6F5

View File

@ -71,44 +71,53 @@ func handleMessage(c *irc.Client, m *irc.Message, event *string, eventData *plug
go notifyEventHandlers(*event, eventData) go notifyEventHandlers(*event, eventData)
} }
for _, r := range config.GetMatchingRules(m, event, eventData) { matchingRules := config.GetMatchingRules(m, event, eventData)
var ( for i := range matchingRules {
ruleEventData = plugins.NewFieldCollection() go handleMessageRuleExecution(c, m, matchingRules[i], eventData)
preventCooldown bool }
) }
if eventData != nil { func handleMessageRuleExecution(c *irc.Client, m *irc.Message, r *plugins.Rule, eventData *plugins.FieldCollection) {
ruleEventData.SetFromData(eventData.Data()) var (
} ruleEventData = plugins.NewFieldCollection()
preventCooldown bool
ActionsLoop: )
for _, a := range r.Actions {
apc, err := triggerAction(c, m, r, a, ruleEventData) if eventData != nil {
switch { ruleEventData.SetFromData(eventData.Data())
case err == nil: }
// Rule execution did not cause an error, we store the
// cooldown modifier and continue ActionsLoop:
preventCooldown = preventCooldown || apc for _, a := range r.Actions {
continue ActionsLoop apc, err := triggerAction(c, m, r, a, ruleEventData)
switch {
case errors.Is(err, plugins.ErrStopRuleExecution): case err == nil:
// Action has asked to stop executing this rule so we store // Rule execution did not cause an error, we store the
// the cooldown modifier and stop executing the actions stack // cooldown modifier and continue
preventCooldown = preventCooldown || apc
break ActionsLoop preventCooldown = preventCooldown || apc
continue ActionsLoop
default:
// Action experienced an error: We don't store the cooldown case errors.Is(err, plugins.ErrStopRuleExecution):
// state of this action and stop executing the actions stack // Action has asked to stop executing this rule so we store
// for this rule // the cooldown modifier and stop executing the actions stack
log.WithError(err).Error("Unable to trigger action") // Action experienced an error: We don't store the cooldown
break ActionsLoop // Break execution for this rule when one action fails // state of this action and stop executing the actions stack
} // for this rule
}
preventCooldown = preventCooldown || apc
// Lock command break ActionsLoop
if !preventCooldown {
r.SetCooldown(timerService, m, eventData) default:
} // Break execution for this rule when one action fails
// Lock command
log.WithError(err).Error("Unable to trigger action")
break ActionsLoop
}
}
if !preventCooldown {
r.SetCooldown(timerService, m, eventData)
} }
} }