summaryrefslogtreecommitdiff
path: root/server/server.go
diff options
context:
space:
mode:
authorLibravatar Terin Stock <terinjokes@gmail.com>2026-10-18 17:42:00 +0200
committerLibravatar Terin Stock <terinjokes@gmail.com>2026-10-18 18:19:51 +0200
commit91a0ef4cb11a54099c56033ce3da8750e90f2c59 (patch)
tree452b7fe050fa000ab0ad2baebc7738db7d5d84f3 /server/server.go
parentchore: change license to EUPL-1.2 (diff)
downloadcgit-httpd-25e09867a2b73acd07b9007a4c45e689c4041b19.tar.xz
feat: use suture supervisorv0.6.0
Replace the adhoc errgroup-based manager with the suture supervisor. Simply the HTTP server due to suture's service restart logic.
Diffstat (limited to 'server/server.go')
-rw-r--r--server/server.go102
1 files changed, 30 insertions, 72 deletions
diff --git a/server/server.go b/server/server.go
index 92f590c..cd63820 100644
--- a/server/server.go
+++ b/server/server.go
@@ -1,96 +1,54 @@
-// Copyright 2022 The Kubernetes Authors.
-// SPDX-License-Identifier: Apache-2.0
-/*
-Copyright 2022 The Kubernetes Authors.
-
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-*/
+// Copyright 2026 Terin Stock
+// SPDX-License-Identifier: EUPL-1.2
package server
import (
+ "cmp"
"context"
"errors"
"log/slog"
- "net"
"net/http"
"time"
)
-// Server is a general purpose HTTP server Runnable for a manager.
-type Server struct {
- // Name is an optional string that describes the purpose of the server. It is used in logs to distinguish
- // among multiple servers.
- Name string
-
- // Server is the HTTP server to run. It is required.
- Server *http.Server
-
- // Listener is an optional listener to use. If not set, the server start a listener using the server.Addr.
- // Using a listener is useful when the port reservation needs to happen in advance of this runnable starting.
- Listener net.Listener
+// HTTPServer implements a service that serves the [http.Server] returned by NewServer.
+type HTTPServer struct {
+ // NewServer is called each time the HTTPServer is started. Do not return the
+ // same instance, as an [http.Server] can not be reused after shutdown.
+ NewServer func() *http.Server
- // ShutdownTimeout is an optional duration that indicates how long to wait for the server to shutdown gracefully. If not set,
- // the server will wait indefinitely for all connections to close.
+ // ShutdownTimeout is the amount of time to gracefully shutdown, defaults
+ // to 5 seconds. Should be less than the supervisor's Timeout.
ShutdownTimeout *time.Duration
}
-// Start starts the server. It will block until the server is stopped or an error occurs.
-func (s *Server) Start(ctx context.Context) error {
- serverShutdown := make(chan struct{})
+// Serve starts the [http.Server] returned by NewServer. The function blocks until the HTTP server
+// is stopped. On context cancellation the HTTP server's Shutdown method is called for a graceful
+// shutdown of at least ShutdownTimeout.
+func (s *HTTPServer) Serve(ctx context.Context) error {
+ srv := s.NewServer()
+ errCh := make(chan error, 1)
- logger := slog.With("addr", s.addr())
- if s.Name != "" {
- logger = logger.With("name", s.Name)
- }
-
- go func() {
- <-ctx.Done()
- logger.Info("shutting down server")
- shutdownCtx := context.Background()
- if s.ShutdownTimeout != nil {
- var shutdownCancel context.CancelFunc
- shutdownCtx, shutdownCancel = context.WithTimeout(context.Background(), *s.ShutdownTimeout)
- defer shutdownCancel()
- }
+ go func() { errCh <- srv.ListenAndServe() }()
- if err := s.Server.Shutdown(shutdownCtx); err != nil {
- logger.Error("error shutting down server", "error", err)
+ select {
+ case err := <-errCh:
+ if errors.Is(err, http.ErrServerClosed) {
+ return nil
}
- close(serverShutdown)
- }()
-
- logger.Info("starting server")
- if err := s.serve(); err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
+ case <-ctx.Done():
}
- <-serverShutdown
- return nil
-}
-
-func (s *Server) addr() string {
- if s.Listener != nil {
- return s.Listener.Addr().String()
- }
+ timeout := cmp.Or(*s.ShutdownTimeout, 5*time.Second)
+ shutdownCtx, cancel := context.WithTimeout(context.Background(), timeout)
+ defer cancel()
- return s.Server.Addr
-}
-
-func (s *Server) serve() error {
- if s.Listener != nil {
- return s.Server.Serve(s.Listener)
+ if err := srv.Shutdown(shutdownCtx); err != nil {
+ slog.Error("error shutting down server", "error", err)
+ _ = srv.Close()
}
-
- return s.Server.ListenAndServe()
+ <-errCh
+ return nil
}