Skip to content

Commit

Permalink
Switch back from fasthttp to net/http to enable flushing again
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed Oct 2, 2024
1 parent f9858be commit 3755f33
Showing 1 changed file with 2 additions and 28 deletions.
30 changes: 2 additions & 28 deletions engine/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package engine

import (
"errors"
"io"
"log"
"net/http"
"os"
"strings"
Expand All @@ -14,8 +12,6 @@ import (
"github.com/caddyserver/certmagic"
"github.com/sirupsen/logrus"
"github.com/tylerb/graceful"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpadaptor"
"github.com/xyproto/env/v2"
"golang.org/x/net/http2"
)
Expand Down Expand Up @@ -59,28 +55,6 @@ func (ac *Config) NewGracefulServer(mux *http.ServeMux, http2support bool, addr
return gracefulServer
}

// NewFastHTTPServer creates a new fasthttp server configuration
func (ac *Config) NewFastHTTPServer(mux *http.ServeMux) *fasthttp.Server {
fastHTTPHandler := fasthttpadaptor.NewFastHTTPHandler(mux)
return &fasthttp.Server{
Handler: fastHTTPHandler,
Name: ac.serverHeaderName,

// The timeout values is also the maximum time it can take
// for a complete page of Server-Sent Events (SSE).
ReadTimeout: 10 * time.Second,
WriteTimeout: time.Duration(ac.writeTimeout) * time.Second,

ErrorHandler: func(ctx *fasthttp.RequestCtx, err error) {
if strings.Contains(err.Error(), "error when reading request headers: ") {
return // Suppress this particular error message
}
logrus.Errorf("error when serving connection %q<->%q: %v", ctx.RemoteAddr(), ctx.LocalAddr(), err)
},
Logger: log.New(io.Discard, "", 0), // use a discarding logger to avoid duplicate logging
}
}

// GenerateShutdownFunction generates a function that will run the postponed
// shutdown functions. Note that gracefulServer can be nil. It's only used for
// finding out if the server was interrupted (ctrl-c or killed, SIGINT/SIGTERM)
Expand Down Expand Up @@ -153,9 +127,9 @@ func (ac *Config) Serve(mux *http.ServeMux, done, ready chan bool) error {
logrus.Info("Serving HTTP on http://" + ac.serverAddr + "/")
}
servingHTTP.Store(true)
HTTPserver := ac.NewFastHTTPServer(mux)
httpServer := ac.NewGracefulServer(mux, false, ac.serverAddr)
// Start serving. Shut down gracefully at exit.
if err := HTTPserver.ListenAndServe(ac.serverAddr); err != nil {
if err := httpServer.ListenAndServe(); err != nil {
servingHTTP.Store(false)
// If we can't serve regular HTTP on port 80, give up
ac.fatalExit(err)
Expand Down

0 comments on commit 3755f33

Please sign in to comment.