From f87b35b3068437aa065143fd0a8722ce6164baff Mon Sep 17 00:00:00 2001 From: Ryan D McGuire Date: Sun, 12 Jan 2025 19:10:30 -0500 Subject: [PATCH] Perform proxy updates in goroutines --- pkg/ambient/ambient.go | 47 ++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/pkg/ambient/ambient.go b/pkg/ambient/ambient.go index 2180734..a97be2b 100644 --- a/pkg/ambient/ambient.go +++ b/pkg/ambient/ambient.go @@ -7,6 +7,7 @@ import ( "context" "fmt" "net/http" + "sync" "gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel" "github.com/rs/zerolog" @@ -120,26 +121,40 @@ func (aw *AmbientWeather) handleProviderRequest( // Uses a weather update to allow awn to publish to wunderground and // visa versa. if station := update.StationConfig; station != nil { + // Perform proxy updates in parallel if enabled + var proxyWg sync.WaitGroup + if station.ProxyToAWN { - err := aw.awnProvider.ProxyReq(ctx, update) - if err != nil { - zerolog.Ctx(aw.appCtx).Err(err).Msg("failed to proxy to ambient weather") - return - } - zerolog.Ctx(aw.appCtx).Debug(). - Str("station", station.Name). - Msg("proxied weather update to awn") + proxyWg.Add(1) + go func() { + defer proxyWg.Done() + err := aw.awnProvider.ProxyReq(ctx, update) + if err != nil { + zerolog.Ctx(aw.appCtx).Err(err).Msg("failed to proxy to ambient weather") + return + } + zerolog.Ctx(aw.appCtx).Debug(). + Str("station", station.Name). + Msg("proxied weather update to awn") + }() } + if station.ProxyToWunderground { - err := aw.wuProvider.ProxyReq(ctx, update) - if err != nil { - zerolog.Ctx(aw.appCtx).Err(err).Msg("failed to proxy to ambient weather") - return - } - zerolog.Ctx(aw.appCtx).Debug(). - Str("station", station.Name). - Msg("proxied weather update to wunderground") + proxyWg.Add(1) + go func() { + defer proxyWg.Done() + err := aw.wuProvider.ProxyReq(ctx, update) + if err != nil { + zerolog.Ctx(aw.appCtx).Err(err).Msg("failed to proxy to ambient weather") + return + } + zerolog.Ctx(aw.appCtx).Debug(). + Str("station", station.Name). + Msg("proxied weather update to wunderground") + }() } + + proxyWg.Wait() } }