From 1d5142d619664bb3bc7e335560306c1ce0b805a8 Mon Sep 17 00:00:00 2001 From: Evstigneev Denis Date: Thu, 7 Mar 2024 18:12:49 +0300 Subject: [PATCH] removed using http.Serve and add using *http.Server + Shutdown --- .gitignore | 24 ++++++++++++++++++++++++ http.go | 48 ++++++++++++++++++++++++++++++++---------------- 2 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e16696 --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib +bin + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work + +# General +.DS_Store +.idea +.vscode \ No newline at end of file diff --git a/http.go b/http.go index 97da7b2..fa4bdc8 100644 --- a/http.go +++ b/http.go @@ -408,7 +408,7 @@ func (h *Server) Start() error { h.Unlock() var handler http.Handler - var srvFunc func(net.Listener) error + //var srvFunc func(net.Listener) error // nolint: nestif if h.opts.Context != nil { @@ -451,6 +451,7 @@ func (h *Server) Start() error { fn := handler + var hs *http.Server if h.opts.Context != nil { if mwf, ok := h.opts.Context.Value(middlewareKey{}).([]func(http.Handler) http.Handler); ok && len(mwf) > 0 { // wrap the handler func @@ -458,25 +459,37 @@ func (h *Server) Start() error { fn = mwf[i-1](fn) } } - if hs, ok := h.opts.Context.Value(serverKey{}).(*http.Server); ok && hs != nil { + var ok bool + if hs, ok = h.opts.Context.Value(serverKey{}).(*http.Server); ok && hs != nil { hs.Handler = fn - srvFunc = hs.Serve + //srvFunc = hs.Serve + } else { + hs = &http.Server{Handler: fn} } } - if srvFunc != nil { - go func() { - if cerr := srvFunc(ts); cerr != nil && !errors.Is(cerr, net.ErrClosed) { - h.opts.Logger.Error(h.opts.Context, cerr) - } - }() - } else { - go func() { - if cerr := http.Serve(ts, fn); cerr != nil && !errors.Is(cerr, net.ErrClosed) { - h.opts.Logger.Error(h.opts.Context, cerr) - } - }() - } + go func() { + if cerr := hs.Serve(ts); cerr != nil && !errors.Is(cerr, net.ErrClosed) { + h.opts.Logger.Error(h.opts.Context, cerr) + } + }() + + /* + if srvFunc != nil { + go func() { + if cerr := srvFunc(ts); cerr != nil && !errors.Is(cerr, net.ErrClosed) { + h.opts.Logger.Error(h.opts.Context, cerr) + } + }() + } else { + go func() { + if cerr := http.Serve(ts, fn); cerr != nil && !errors.Is(cerr, net.ErrClosed) { + h.opts.Logger.Error(h.opts.Context, cerr) + } + }() + } + + */ go func() { t := new(time.Ticker) @@ -536,6 +549,9 @@ func (h *Server) Start() error { config.Logger.Errorf(config.Context, "Server deregister error: %s", err) } + // err ignore and empty cotnext + hs.Shutdown(context.Background()) + ch <- ts.Close() }()