2021-05-11 22:59:05 +00:00
|
|
|
package httputil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
2021-05-11 22:59:05 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Serve listens and serves the internal HTTP server.
|
2021-05-11 22:59:05 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns any error returned by the internal server
|
2021-05-11 22:59:05 +00:00
|
|
|
// except http.ErrServerClosed.
|
|
|
|
//
|
|
|
|
// After Shutdown call, Serve has no effect and
|
2022-04-21 11:28:05 +00:00
|
|
|
// the returned error is always nil.
|
2021-05-11 22:59:05 +00:00
|
|
|
func (x *Server) Serve() error {
|
|
|
|
err := x.srv.ListenAndServe()
|
|
|
|
|
|
|
|
// http.ErrServerClosed is returned on server shutdown
|
|
|
|
// so we ignore this error.
|
|
|
|
if err != nil && errors.Is(err, http.ErrServerClosed) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Shutdown gracefully shuts down the internal HTTP server.
|
2021-05-11 22:59:05 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Shutdown is called with the context which expires after
|
|
|
|
// the configured timeout.
|
2021-05-11 22:59:05 +00:00
|
|
|
//
|
|
|
|
// Once Shutdown has been called on a server, it may not be reused;
|
|
|
|
// future calls to Serve method will have no effect.
|
2024-10-21 13:27:28 +00:00
|
|
|
func (x *Server) Shutdown(ctx context.Context) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), x.shutdownTimeout)
|
2021-05-11 22:59:05 +00:00
|
|
|
|
|
|
|
err := x.srv.Shutdown(ctx)
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|