Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
68cfbf890f |
No files matched your search
@@ -20,7 +20,6 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
"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
|
// 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.
|
// is dropped: that of a WebSocket that closes while rproxy stops, for one.
|
||||||
func (s *Server) WaitForReports() {
|
func (s *Server) WaitForReports() {
|
||||||
s.h.reportsMu.Lock()
|
s.h.reports.stopAndWait()
|
||||||
s.h.reportsStopped = true
|
|
||||||
s.h.reportsMu.Unlock()
|
|
||||||
|
|
||||||
s.h.reports.Wait()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// handler is the proxy. It holds what every request shares; what belongs to
|
// handler is the proxy. It holds what every request shares; what belongs to
|
||||||
@@ -136,11 +131,8 @@ type handler struct {
|
|||||||
// the time.
|
// the time.
|
||||||
breaker breaker
|
breaker breaker
|
||||||
breakerClock func() time.Time
|
breakerClock func() time.Time
|
||||||
// reports waits for the reports being sent. reportsMu guards starting one
|
// reports are the goroutines sending reports to waf.
|
||||||
// against reportsStopped, which is set once nothing is to wait for another.
|
reports reportGoroutines
|
||||||
reports sync.WaitGroup
|
|
||||||
reportsMu sync.Mutex
|
|
||||||
reportsStopped bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// newTransport returns what carries requests to the app. It never goes through
|
// newTransport returns what carries requests to the app. It never goes through
|
||||||
|
|||||||
+33
-8
@@ -8,6 +8,7 @@ import (
|
|||||||
"maps"
|
"maps"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/rules"
|
"sneak.berlin/go/smallwebwaf/internal/rules"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/wafprotocol"
|
"sneak.berlin/go/smallwebwaf/internal/wafprotocol"
|
||||||
@@ -210,14 +211,7 @@ func (h *handler) sendReport(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
report *wafprotocol.Report,
|
report *wafprotocol.Report,
|
||||||
) {
|
) {
|
||||||
h.reportsMu.Lock()
|
h.reports.startUnlessStopped(func() {
|
||||||
defer h.reportsMu.Unlock()
|
|
||||||
|
|
||||||
if h.reportsStopped {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
h.reports.Go(func() {
|
|
||||||
err := h.postReport(context.WithoutCancel(ctx), report)
|
err := h.postReport(context.WithoutCancel(ctx), report)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.processLog.Warn("a report is dropped", "error", err.Error())
|
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
|
// postReport posts report to waf, and waits for its answer for up to
|
||||||
// SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS.
|
// SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS.
|
||||||
func (h *handler) postReport(
|
func (h *handler) postReport(
|
||||||
|
|||||||
Reference in new issue
Block a user