-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
57 lines (53 loc) · 1.56 KB
/
server.go
File metadata and controls
57 lines (53 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package httpx
import (
"context"
"errors"
"net/http"
"time"
)
// ListenAndAutoShutdown starts an HTTP server and automatically handles graceful shutdown.
// It listens for context cancellation to trigger shutdown with the specified timeout.
// Returns any error from server startup or shutdown, prioritizing startup errors.
func ListenAndAutoShutdown(ctx context.Context, server *http.Server, closeTimeout time.Duration) error {
errChan := make(chan error, 1)
go func() {
defer close(errChan)
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
errChan <- err
}
}()
select {
case <-ctx.Done():
shutdownCtx, cancel := context.WithTimeout(context.Background(), closeTimeout)
defer cancel()
shutdownErr := server.Shutdown(shutdownCtx)
listenErr := <-errChan
if listenErr != nil {
return listenErr
}
return shutdownErr
case err := <-errChan:
return err
}
}
// Start begins serving HTTP requests on the configured address.
// It ignores http.ErrServerClosed which is expected during graceful shutdown.
// Returns any other error that occurs during server startup.
func Start(server *http.Server) error {
if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
}
// Close gracefully shuts down the HTTP server using the provided context.
// It handles nil server gracefully and returns any shutdown error.
func Close(ctx context.Context, server *http.Server) error {
if server == nil {
return nil
}
err := server.Shutdown(ctx)
if err != nil {
return err
}
return nil
}