go.uber.org/fx builds each daemon's parts in internal/sww from one small provider per part, and runs each background loop as one lifecycle hook. This replaces newServer, loadStateFiles, the inBackground list and both serve functions. One serve hook and one run function serve both daemons. fx's own events go to the process log at debug. Start-up errors keep their messages through startError. RunWAF and RunRProxy use fx's Start and Stop, not its signal handling. The last state write is registered before the loops that feed it, so that it runs after they end. Model: opus-5-5
This commit is contained in:
1 parent
dadd5768d5
commit
37098fa9b7
7 files changed
+381
-302
No files matched your search
+36
-59
@@ -2,12 +2,13 @@ package sww
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"go.uber.org/fx"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/rproxy"
|
||||
@@ -30,26 +31,26 @@ func RunRProxy(ctx context.Context, params Params) int {
|
||||
|
||||
cfg, err := config.RProxyFromEnvironment(params.Viper)
|
||||
if err != nil {
|
||||
processLog.Error("invalid setting", "error", err.Error())
|
||||
|
||||
return 1
|
||||
return cannotStart(processLog, startError{"invalid setting", err})
|
||||
}
|
||||
|
||||
server := rproxy.New(rproxy.Params{
|
||||
Config: cfg,
|
||||
RequestLog: params.Stdout,
|
||||
ProcessLog: processLog,
|
||||
})
|
||||
var listener net.Listener
|
||||
|
||||
listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", cfg.ListenAddr)
|
||||
app := fx.New(
|
||||
fx.Supply(cfg, processLog, requestLog{params.Stdout}),
|
||||
fx.Provide(
|
||||
newRProxyServer,
|
||||
func(server *rproxy.Server) *http.Server { return server.Server },
|
||||
newRProxyListener,
|
||||
),
|
||||
fx.Invoke(serve),
|
||||
fx.Populate(&listener),
|
||||
fx.WithLogger(fxEventLogger(processLog)),
|
||||
)
|
||||
|
||||
err = app.Err()
|
||||
if err != nil {
|
||||
processLog.Error(
|
||||
"cannot listen on SWW_RPROXY_LISTEN_ADDR",
|
||||
"error",
|
||||
err.Error(),
|
||||
)
|
||||
|
||||
return 1
|
||||
return cannotStart(processLog, err)
|
||||
}
|
||||
|
||||
processLog.Info("starting",
|
||||
@@ -57,52 +58,28 @@ func RunRProxy(ctx context.Context, params Params) int {
|
||||
"address", listener.Addr().String(),
|
||||
"settings", cfg)
|
||||
|
||||
return serveRProxy(ctx, server, listener, processLog)
|
||||
return run(ctx, app, processLog)
|
||||
}
|
||||
|
||||
// serveRProxy serves requests on listener until ctx is done, and then stops as
|
||||
// RunRProxy says.
|
||||
func serveRProxy(
|
||||
ctx context.Context, server *rproxy.Server, listener net.Listener,
|
||||
// newRProxyServer returns the server rproxy runs. Once it has stopped
|
||||
// serving, the stop waits for the reports being sent.
|
||||
func newRProxyServer(
|
||||
lc fx.Lifecycle, cfg *config.RProxy, out requestLog,
|
||||
processLog *slog.Logger,
|
||||
) int {
|
||||
served := make(chan error, 1)
|
||||
) *rproxy.Server {
|
||||
server := rproxy.New(rproxy.Params{
|
||||
Config: cfg,
|
||||
RequestLog: out,
|
||||
ProcessLog: processLog,
|
||||
})
|
||||
|
||||
go func() {
|
||||
served <- server.Serve(listener)
|
||||
}()
|
||||
// fx stops in the reverse order, so this follows the stop of serving.
|
||||
lc.Append(fx.StopHook(server.WaitForReports))
|
||||
|
||||
select {
|
||||
case err := <-served:
|
||||
processLog.Error("serving failed", "error", err.Error())
|
||||
return server
|
||||
}
|
||||
|
||||
return 1
|
||||
case <-ctx.Done():
|
||||
}
|
||||
|
||||
processLog.Info("stopping")
|
||||
|
||||
shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx),
|
||||
shutdownTimeout)
|
||||
defer cancel()
|
||||
|
||||
err := server.Shutdown(shutdownCtx)
|
||||
if err != nil {
|
||||
processLog.Warn("requests still in progress were cut off",
|
||||
"error", err.Error())
|
||||
|
||||
_ = server.Close()
|
||||
}
|
||||
|
||||
err = <-served
|
||||
if !errors.Is(err, http.ErrServerClosed) {
|
||||
processLog.Error("serving failed", "error", err.Error())
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
server.WaitForReports()
|
||||
processLog.Info("stopped")
|
||||
|
||||
return 0
|
||||
// newRProxyListener listens on SWW_RPROXY_LISTEN_ADDR.
|
||||
func newRProxyListener(cfg *config.RProxy) (net.Listener, error) {
|
||||
return listen(cfg.ListenAddr, "SWW_RPROXY_LISTEN_ADDR")
|
||||
}
|
||||
Reference in new issue
Block a user