From 43aeb830691128961e78aa0befb59682ac873645 Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Sun, 10 Feb 2019 13:44:01 +0100 Subject: [PATCH] improve notifier performance --- core/cache.go | 5 ++++- core/notifier_xmpp.go | 51 ++++++++++++++++++++++++++++--------------- core/notifiers.go | 16 +++++++++----- 3 files changed, 47 insertions(+), 25 deletions(-) diff --git a/core/cache.go b/core/cache.go index 877d6f3..7cf8066 100644 --- a/core/cache.go +++ b/core/cache.go @@ -53,11 +53,14 @@ func (results BoxCheckResults) filterChangedFromCache() BoxCheckResults { return remaining } -func updateCache(box *Box, results []CheckResult) error { +func updateCache(box *Box, results []CheckResult) { for _, result := range results { key := fmt.Sprintf("watchcache.%s.%s", box.Id, result.EventID()) cache.Set(key+".laststatus", result.Status) } +} + +func writeCache() error { return cache.WriteConfig() } diff --git a/core/notifier_xmpp.go b/core/notifier_xmpp.go index c649573..5fab50c 100644 --- a/core/notifier_xmpp.go +++ b/core/notifier_xmpp.go @@ -8,6 +8,8 @@ import ( "github.com/spf13/viper" ) +var client = &xmpp.Client{} // @Hacky + // box config required for the XmppNotifier (TransportConfig.Options) type XmppNotifier struct { Recipients []string @@ -23,6 +25,16 @@ func (n XmppNotifier) New(config TransportConfig) (AbstractNotifier, error) { } } + // establish connection with server once, and share it accross instances + // @Hacky + if client.JID() == "" { + c, err := connectXmpp() + if err != nil { + return nil, err + } + client = c + } + // assign configuration to the notifier after ensuring the correct type. // lesson of this project: golang requires us to fuck around with type // assertions, instead of providing us with proper inheritance. @@ -53,26 +65,12 @@ func (n XmppNotifier) New(config TransportConfig) (AbstractNotifier, error) { } func (n XmppNotifier) Submit(notification Notification) error { - // :TransportConfSourceHack - xmppOpts := xmpp.Options{ - Host: viper.GetString("xmpp.host"), - User: viper.GetString("xmpp.user"), - Password: viper.GetString("xmpp.pass"), - Resource: "osem_notify", - } - - if viper.GetBool("xmpp.starttls") { - xmppOpts.NoTLS = true - xmppOpts.StartTLS = true - } - - client, err := xmppOpts.NewClient() - if err != nil { - return err + if client.JID() == "" { + return fmt.Errorf("xmpp client not correctly initialized!") } for _, recipient := range n.Recipients { - _, err = client.Send(xmpp.Chat{ + _, err := client.Send(xmpp.Chat{ Remote: recipient, Subject: notification.Subject, Text: fmt.Sprintf("%s\n\n%s", notification.Subject, notification.Body), @@ -83,5 +81,22 @@ func (n XmppNotifier) Submit(notification Notification) error { } } - return err + return nil +} + +func connectXmpp() (*xmpp.Client, error) { + // :TransportConfSourceHack + xmppOpts := xmpp.Options{ + Host: viper.GetString("xmpp.host"), + User: viper.GetString("xmpp.user"), + Password: viper.GetString("xmpp.pass"), + Resource: "osem_notify", + } + + if viper.GetBool("xmpp.starttls") { + xmppOpts.NoTLS = true + xmppOpts.StartTLS = true + } + + return xmppOpts.NewClient() } diff --git a/core/notifiers.go b/core/notifiers.go index a6ffa8d..14c87ec 100644 --- a/core/notifiers.go +++ b/core/notifiers.go @@ -98,11 +98,7 @@ func (results BoxCheckResults) SendNotifications(notifyTypes []string, useCache // update cache (with /all/ changed results to reset status) if useCache { notifyLog.Debug("updating cache") - cacheError := updateCache(box, resultsBox) - if cacheError != nil { - notifyLog.Error("could not cache notification results: ", cacheError) - errs = append(errs, cacheError.Error()) - } + updateCache(box, resultsBox) } if len(resultsDue) != 0 { @@ -110,6 +106,15 @@ func (results BoxCheckResults) SendNotifications(notifyTypes []string, useCache } } + // persist changes to cache + if useCache { + err := writeCache() + if err != nil { + log.Error("could not write cache of notification results: ", err) + errs = append(errs, err.Error()) + } + } + if len(errs) != 0 { return fmt.Errorf(strings.Join(errs, "\n")) } @@ -147,4 +152,3 @@ func ComposeNotification(box *Box, checks []CheckResult) Notification { time.Now().Round(time.Minute), box.Name, errList, resolvedList, box.Id), } } -