Hold Go's server privately in waf.Server and rproxy.Server (closes #437)
check / check (push) Waiting to run
check / check (push) Waiting to run
Neither Server embeds *http.Server any more. Each holds it as a private field, is its handler, and has Serve, Shutdown, Close and ServeHTTP. sww's serve hook takes the new daemonServer interface of those first three, and fx provides each Server as it. Model: opus-5-5
This commit is contained in:
1 parent
cc77435f2a
commit
ad7ea4608b
15 files changed
+134
-54
No files matched your search
@@ -1,6 +1,14 @@
|
||||
package rproxy
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// HTTPServer is Go's server s holds, for tests that read its settings.
|
||||
func (s *Server) HTTPServer() *http.Server {
|
||||
return s.httpServer
|
||||
}
|
||||
|
||||
// StopBreakerClock has the circuit breaker of s read at as the time, so that how
|
||||
// long requests take cannot matter to it. It is called before s serves.
|
||||
|
||||
@@ -56,7 +56,7 @@ func TestServerHasTheFixedLimits(t *testing.T) {
|
||||
InstanceName: "test",
|
||||
Level: slog.LevelInfo,
|
||||
}),
|
||||
})
|
||||
}).HTTPServer()
|
||||
|
||||
if server.Addr != ":8080" || server.MaxHeaderBytes != 28<<10 ||
|
||||
server.IdleTimeout != 2*time.Minute || server.ReadHeaderTimeout != time.Minute {
|
||||
@@ -709,7 +709,7 @@ func TestABodyLeftUnreadIsReadOnlyUntilTheRequestTimeLimit(t *testing.T) {
|
||||
|
||||
before := time.Now()
|
||||
|
||||
server.Handler.ServeHTTP(w, req)
|
||||
server.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusNoContent {
|
||||
t.Errorf("status %d, want the app's 204", w.Code)
|
||||
@@ -762,7 +762,7 @@ func TestABodyLeftUnreadIsReadNoMoreOnceTheTimeToSendTheAnswerRunsOut(
|
||||
|
||||
before := time.Now()
|
||||
|
||||
server.Handler.ServeHTTP(w, req)
|
||||
server.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status %d, want the app's 200", w.Code)
|
||||
@@ -815,7 +815,7 @@ func TestAnAnswerRProxyGivesItselfHasTheResponseTimeToBeSent(t *testing.T) {
|
||||
|
||||
before := time.Now()
|
||||
|
||||
server.Handler.ServeHTTP(w, req)
|
||||
server.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != tc.status {
|
||||
t.Errorf("status %d, want %d", w.Code, tc.status)
|
||||
@@ -856,7 +856,7 @@ func TestAnAnswerRProxyGivesItselfHasTenSecondsToBeSentAtLeast(
|
||||
|
||||
before := time.Now()
|
||||
|
||||
server.Handler.ServeHTTP(w, req)
|
||||
server.ServeHTTP(w, req)
|
||||
|
||||
if w.writeDeadline.Before(before.Add(least)) {
|
||||
t.Errorf(
|
||||
|
||||
@@ -583,7 +583,7 @@ func TestAQuestionNamesNoZone(t *testing.T) {
|
||||
req.RemoteAddr = tc.remoteAddr
|
||||
req.Header.Set(forwardedForHeader, tc.forwardedFor)
|
||||
|
||||
server.Handler.ServeHTTP(httptest.NewRecorder(), req)
|
||||
server.ServeHTTP(httptest.NewRecorder(), req)
|
||||
|
||||
asked := standIn.question(t)
|
||||
if asked.header.Get(wafprotocol.ClientHeader) != tc.client ||
|
||||
|
||||
+36
-13
@@ -16,6 +16,7 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"slices"
|
||||
@@ -63,9 +64,8 @@ type Params struct {
|
||||
|
||||
// Server is the server `sww rproxy` runs.
|
||||
type Server struct {
|
||||
*http.Server
|
||||
|
||||
h *handler
|
||||
httpServer *http.Server
|
||||
h *handler
|
||||
}
|
||||
|
||||
// New returns the server `sww rproxy` runs. Go's server itself refuses
|
||||
@@ -94,17 +94,40 @@ func New(params Params) *Server {
|
||||
breakerClock: time.Now,
|
||||
}
|
||||
|
||||
return &Server{
|
||||
Server: &http.Server{
|
||||
Addr: params.Config.ListenAddr,
|
||||
Handler: h,
|
||||
ErrorLog: errorLog,
|
||||
ReadHeaderTimeout: params.Config.MaxRequestTime,
|
||||
IdleTimeout: idleTimeout,
|
||||
MaxHeaderBytes: requestHeaderMaxBytes - goServerExtraHeaderBytes,
|
||||
},
|
||||
h: h,
|
||||
s := &Server{h: h}
|
||||
s.httpServer = &http.Server{
|
||||
Addr: params.Config.ListenAddr,
|
||||
Handler: s,
|
||||
ErrorLog: errorLog,
|
||||
ReadHeaderTimeout: params.Config.MaxRequestTime,
|
||||
IdleTimeout: idleTimeout,
|
||||
MaxHeaderBytes: requestHeaderMaxBytes - goServerExtraHeaderBytes,
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// Serve serves requests on listener until Shutdown or Close, as Go's
|
||||
// http.Server.Serve does.
|
||||
func (s *Server) Serve(listener net.Listener) error {
|
||||
return s.httpServer.Serve(listener)
|
||||
}
|
||||
|
||||
// Shutdown stops serving once the requests in progress have finished, or
|
||||
// when ctx is done, as Go's http.Server.Shutdown does.
|
||||
func (s *Server) Shutdown(ctx context.Context) error {
|
||||
return s.httpServer.Shutdown(ctx)
|
||||
}
|
||||
|
||||
// Close stops serving at once and closes every connection, as Go's
|
||||
// http.Server.Close does.
|
||||
func (s *Server) Close() error {
|
||||
return s.httpServer.Close()
|
||||
}
|
||||
|
||||
// ServeHTTP handles one request from a visitor, as handler.ServeHTTP says.
|
||||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
s.h.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// WaitForReports returns once the reports being sent have gone or failed,
|
||||
|
||||
@@ -710,7 +710,7 @@ func TestAnAnswerWithoutAContentTypeGetsNone(t *testing.T) {
|
||||
)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
server.Handler.ServeHTTP(w, req)
|
||||
server.ServeHTTP(w, req)
|
||||
|
||||
// Go's server guesses a type when the first part of the body goes before the
|
||||
// headers are flushed, which is a matter of timing here. So this looks for what
|
||||
|
||||
Reference in new issue
Block a user