sourcegraph/internal/httpserver/server.go
Joe Chen 2589fef13e
lib/background: upgrade Routine interface with context and errors (#62136)
This PR is a result/followup of the improvements we've made in the [SAMS repo](https://github.com/sourcegraph/sourcegraph-accounts/pull/199) that allows call sites to pass down a context (primarily to indicate deadline, and of course, cancellation if desired) and collects the error returned from `background.Routine`s `Stop` method.

Note that I did not adopt returning error from `Stop` method because I realize in monorepo, the more common (and arguably the desired) pattern is to hang on the call of `Start` method until `Stop` is called, so it is meaningless to collect errors from `Start` methods as return values anyway, and doing that would also complicate the design and semantics more than necessary.

All usages of the the `background.Routine` and `background.CombinedRoutines` are updated, I DID NOT try to interpret the code logic and make anything better other than fixing compile and test errors.

The only file that contains the core change is the [`lib/background/background.go`](https://github.com/sourcegraph/sourcegraph/pull/62136/files#diff-65c3228388620e91f8c22d91c18faac3f985fc67d64b08612df18fa7c04fafcd).
2024-05-24 10:04:55 -04:00

86 lines
2.2 KiB
Go

package httpserver
import (
"context"
"net"
"net/http"
"os"
"sync"
"time"
"github.com/inconshreveable/log15" //nolint:logging // TODO move all logging to sourcegraph/log
"github.com/sourcegraph/sourcegraph/internal/goroutine"
)
type server struct {
server *http.Server
makeListener func() (net.Listener, error)
once sync.Once
preShutdownPause time.Duration
}
type ServerOptions func(s *server)
func WithPreShutdownPause(d time.Duration) ServerOptions {
return func(s *server) { s.preShutdownPause = d }
}
// New returns a BackgroundRoutine that serves the given server on the given listener.
func New(listener net.Listener, httpServer *http.Server, options ...ServerOptions) goroutine.BackgroundRoutine {
makeListener := func() (net.Listener, error) { return listener, nil }
return newServer(httpServer, makeListener, options...)
}
// NewFromAddr returns a BackgroundRoutine that serves the given handler on the given address.
func NewFromAddr(addr string, httpServer *http.Server, options ...ServerOptions) goroutine.BackgroundRoutine {
makeListener := func() (net.Listener, error) { return NewListener(addr) }
return newServer(httpServer, makeListener, options...)
}
func newServer(httpServer *http.Server, makeListener func() (net.Listener, error), options ...ServerOptions) goroutine.BackgroundRoutine {
s := &server{
server: httpServer,
makeListener: makeListener,
}
for _, option := range options {
option(s)
}
return s
}
func (s *server) Name() string {
return "HTTP server"
}
func (s *server) Start() {
listener, err := s.makeListener()
if err != nil {
log15.Error("Failed to create listener", "error", err)
os.Exit(1)
}
if err := s.server.Serve(listener); err != http.ErrServerClosed {
log15.Error("Failed to start server", "error", err)
os.Exit(1)
}
}
func (s *server) Stop(ctx context.Context) error {
s.once.Do(func() {
if s.preShutdownPause > 0 {
time.Sleep(s.preShutdownPause)
}
ctx, cancel := context.WithTimeout(ctx, goroutine.GracefulShutdownTimeout)
defer cancel()
if err := s.server.Shutdown(ctx); err != nil {
log15.Error("Failed to shutdown server", "error", err)
}
})
return nil
}