From cc77435f2a9375eb5e1b7a88585458e39d2c5856 Mon Sep 17 00:00:00 2001 From: clawbot <35+clawbot@noreply.example.org> Date: Fri, 20 Nov 2026 01:21:06 +0100 Subject: [PATCH] rproxy: one type for the goroutines sending reports (closes #433) handler held a WaitGroup, a mutex and a stopped flag for the reports, and the rule that no report starts once waiting has begun was split between sendReport and Server.WaitForReports. reportGoroutines now holds all three, and its two methods are the only code that touches the lock and the flag. Model: opus-5-5 --- internal/rproxy/rproxy.go | 14 +++---------- internal/rproxy/waf.go | 41 +++++++++++++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/internal/rproxy/rproxy.go b/internal/rproxy/rproxy.go index 230a647..0c18996 100644 --- a/internal/rproxy/rproxy.go +++ b/internal/rproxy/rproxy.go @@ -20,7 +20,6 @@ import ( "net/url" "slices" "strings" - "sync" "time" "sneak.berlin/go/smallwebwaf/internal/config" @@ -112,11 +111,7 @@ func New(params Params) *Server { // which SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS bounds. Any report after this call // is dropped: that of a WebSocket that closes while rproxy stops, for one. func (s *Server) WaitForReports() { - s.h.reportsMu.Lock() - s.h.reportsStopped = true - s.h.reportsMu.Unlock() - - s.h.reports.Wait() + s.h.reports.stopAndWait() } // handler is the proxy. It holds what every request shares; what belongs to @@ -136,11 +131,8 @@ type handler struct { // the time. breaker breaker breakerClock func() time.Time - // reports waits for the reports being sent. reportsMu guards starting one - // against reportsStopped, which is set once nothing is to wait for another. - reports sync.WaitGroup - reportsMu sync.Mutex - reportsStopped bool + // reports are the goroutines sending reports to waf. + reports reportGoroutines } // newTransport returns what carries requests to the app. It never goes through diff --git a/internal/rproxy/waf.go b/internal/rproxy/waf.go index d5917df..82351fc 100644 --- a/internal/rproxy/waf.go +++ b/internal/rproxy/waf.go @@ -8,6 +8,7 @@ import ( "maps" "net/http" "strings" + "sync" "sneak.berlin/go/smallwebwaf/internal/rules" "sneak.berlin/go/smallwebwaf/internal/wafprotocol" @@ -210,14 +211,7 @@ func (h *handler) sendReport( ctx context.Context, report *wafprotocol.Report, ) { - h.reportsMu.Lock() - defer h.reportsMu.Unlock() - - if h.reportsStopped { - return - } - - h.reports.Go(func() { + h.reports.startUnlessStopped(func() { err := h.postReport(context.WithoutCancel(ctx), report) if err != nil { h.processLog.Warn("a report is dropped", "error", err.Error()) @@ -225,6 +219,37 @@ func (h *handler) sendReport( }) } +// reportGoroutines are the goroutines sending reports to waf. None starts once +// stopAndWait has been called. +type reportGoroutines struct { + mu sync.Mutex + stopped bool + running sync.WaitGroup +} + +// startUnlessStopped runs send on a goroutine of its own, or drops it once +// stopAndWait has been called. +func (g *reportGoroutines) startUnlessStopped(send func()) { + g.mu.Lock() + defer g.mu.Unlock() + + if g.stopped { + return + } + + g.running.Go(send) +} + +// stopAndWait stops any more goroutines from starting, and returns once those +// running have ended. +func (g *reportGoroutines) stopAndWait() { + g.mu.Lock() + g.stopped = true + g.mu.Unlock() + + g.running.Wait() +} + // postReport posts report to waf, and waits for its answer for up to // SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS. func (h *handler) postReport(