Documentation
¶
Overview ¶
Package httpserver adapts standard-library net/http servers to sup actors.
Getting started ¶
Create an HTTP server actor and place it under a sup Supervisor:
server := httpserver.NewActor("http", func(context.Context) (*http.Server, error) {
return &http.Server{
Addr: ":8080",
Handler: handler,
}, nil
})
supervisor := sup.NewSupervisor("root",
sup.WithPolicy(sup.Transient),
sup.WithActors(server),
)
The ServerFunc is called once for every execution attempt and must return a fresh *http.Server because a server cannot be reused after Shutdown. By default, the actor calls ListenAndServe. Use WithServe for TLS, custom listeners, or another serving mode. Use WithShutdownTimeout to bound the graceful shutdown period.
Context cancellation performs graceful shutdown with the configured timeout. http.ErrServerClosed is treated as a clean stop. Startup and unexpected serving errors are returned to the supervisor and handled by its restart policy.
A nil server produces ErrNilServer, and concurrent calls to Run produce ErrActorRunning. See the package README and the package reference at https://pkg.go.dev/github.com/webermarci/sup/httpserver for the complete API.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrActorRunning is returned when Run is called while the actor is already // serving a server. ErrActorRunning = errors.New("httpserver: actor is already running") // ErrNilServer is returned when a ServerFunc returns a nil server without an // error. ErrNilServer = errors.New("httpserver: server function returned nil") )
Functions ¶
This section is empty.
Types ¶
type Actor ¶
type Actor struct {
// contains filtered or unexported fields
}
Actor creates and serves a fresh net/http Server for each execution attempt. It is safe to call Run sequentially and rejects concurrent calls.
func NewActor ¶
func NewActor(id string, server ServerFunc, options ...Option) *Actor
NewActor creates an HTTP server actor. The server function is called once for each execution attempt and must return a fresh *http.Server.
type Option ¶
type Option func(*Actor)
Option configures an HTTP server actor during construction.
func WithServe ¶
WithServe configures how the HTTP server is started. By default the actor calls (*http.Server).ListenAndServe.
func WithShutdownTimeout ¶
WithShutdownTimeout configures the maximum time allowed for graceful shutdown after the actor context is canceled.