Added caching of generated mails to speed up notification delivery
This commit is contained in:
parent
170d6fb353
commit
9bc5e8edce
2 changed files with 23 additions and 9 deletions
19
mail.go
19
mail.go
|
@ -54,7 +54,7 @@ type SmtpSettings struct {
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r Recipient) filterAndSendNotices(notices []WidNotice, template MailTemplate, auth smtp.Auth, smtpConfig SmtpSettings) error {
|
func (r Recipient) filterAndSendNotices(notices []WidNotice, template MailTemplate, auth smtp.Auth, smtpConfig SmtpSettings, cache *map[string][]byte) error {
|
||||||
filteredNotices := []WidNotice{}
|
filteredNotices := []WidNotice{}
|
||||||
for _, f := range r.Filters {
|
for _, f := range r.Filters {
|
||||||
for _, n := range f.filter(notices) {
|
for _, n := range f.filter(notices) {
|
||||||
|
@ -66,15 +66,27 @@ func (r Recipient) filterAndSendNotices(notices []WidNotice, template MailTempla
|
||||||
slices.Reverse(filteredNotices)
|
slices.Reverse(filteredNotices)
|
||||||
logger.debug(fmt.Sprintf("Including %v of %v notices for recipient %v", len(filteredNotices), len(notices), r.Address))
|
logger.debug(fmt.Sprintf("Including %v of %v notices for recipient %v", len(filteredNotices), len(notices), r.Address))
|
||||||
logger.debug("Generating and sending mails to " + r.Address + " ...")
|
logger.debug("Generating and sending mails to " + r.Address + " ...")
|
||||||
|
cacheHits := 0
|
||||||
|
cacheMisses := 0
|
||||||
for _, n := range filteredNotices {
|
for _, n := range filteredNotices {
|
||||||
|
var data []byte
|
||||||
|
cacheResult := (*cache)[n.Uuid]
|
||||||
|
if len(cacheResult) > 0 {
|
||||||
|
cacheHits++
|
||||||
|
data = cacheResult
|
||||||
|
} else {
|
||||||
|
cacheMisses++
|
||||||
mailContent, err := template.generate(n)
|
mailContent, err := template.generate(n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.error("Could not create mail from template")
|
logger.error("Could not create mail from template")
|
||||||
logger.error(err)
|
logger.error(err)
|
||||||
}
|
}
|
||||||
// serialize & send mail
|
// serialize & send mail
|
||||||
data := mailContent.serializeValidMail(smtpConfig.From, r.Address)
|
data = mailContent.serializeValidMail(smtpConfig.From, r.Address)
|
||||||
err = smtp.SendMail(
|
// add to cache
|
||||||
|
(*cache)[n.Uuid] = data
|
||||||
|
}
|
||||||
|
err := smtp.SendMail(
|
||||||
fmt.Sprintf("%v:%v", smtpConfig.ServerHost, smtpConfig.ServerPort),
|
fmt.Sprintf("%v:%v", smtpConfig.ServerHost, smtpConfig.ServerPort),
|
||||||
auth,
|
auth,
|
||||||
smtpConfig.From,
|
smtpConfig.From,
|
||||||
|
@ -85,6 +97,7 @@ func (r Recipient) filterAndSendNotices(notices []WidNotice, template MailTempla
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
logger.debug(fmt.Sprintf("%v mail cache hits, %v misses", cacheHits, cacheMisses))
|
||||||
logger.debug("Successfully sent all mails to " + r.Address)
|
logger.debug("Successfully sent all mails to " + r.Address)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
3
main.go
3
main.go
|
@ -71,6 +71,7 @@ func main() {
|
||||||
for {
|
for {
|
||||||
t1 := time.Now().UnixMilli()
|
t1 := time.Now().UnixMilli()
|
||||||
newNotices := []WidNotice{}
|
newNotices := []WidNotice{}
|
||||||
|
cache := map[string][]byte{}
|
||||||
for _, a := range enabledApiEndpoints {
|
for _, a := range enabledApiEndpoints {
|
||||||
logger.info("Querying endpoint '" + a.Id + "' for new notices ...")
|
logger.info("Querying endpoint '" + a.Id + "' for new notices ...")
|
||||||
n, t, err := a.getNotices(persistent.data.(PersistentData).LastPublished[a.Id])
|
n, t, err := a.getNotices(persistent.data.(PersistentData).LastPublished[a.Id])
|
||||||
|
@ -95,7 +96,7 @@ func main() {
|
||||||
logger.info("Sending email notifications ...")
|
logger.info("Sending email notifications ...")
|
||||||
recipientsNotified := 0
|
recipientsNotified := 0
|
||||||
for _, r := range config.Recipients {
|
for _, r := range config.Recipients {
|
||||||
err := r.filterAndSendNotices(newNotices, mailTemplate, mailAuth, config.SmtpConfiguration)
|
err := r.filterAndSendNotices(newNotices, mailTemplate, mailAuth, config.SmtpConfiguration, &cache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.error(err)
|
logger.error(err)
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue