hotfix #186

Merged
vtolstov merged 8 commits from kgorbunov/micro-server-http:hotfix into master 2024-03-26 14:53:15 +03:00
7 changed files with 1577 additions and 97 deletions
Showing only changes of commit cb4309412b - Show all commits

24
.gitignore vendored Normal file
View File

@ -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

21
go.mod
View File

@ -5,20 +5,21 @@ go 1.19
require ( require (
go.unistack.org/micro-codec-yaml/v4 v4.0.0 go.unistack.org/micro-codec-yaml/v4 v4.0.0
go.unistack.org/micro-proto/v4 v4.0.1 go.unistack.org/micro-proto/v4 v4.0.1
go.unistack.org/micro/v4 v4.0.6 go.unistack.org/micro/v4 v4.0.17
go.unistack.org/protoc-gen-go-micro/v4 v4.0.7 go.unistack.org/protoc-gen-go-micro/v4 v4.0.13
golang.org/x/net v0.12.0 golang.org/x/net v0.22.0
) )
require ( require (
github.com/fatih/structtag v1.2.0 // indirect github.com/fatih/structtag v1.2.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic v0.6.9 // indirect github.com/google/gnostic v0.7.0 // indirect
golang.org/x/mod v0.12.0 // indirect github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect
golang.org/x/sys v0.10.0 // indirect golang.org/x/mod v0.16.0 // indirect
golang.org/x/tools v0.11.0 // indirect golang.org/x/sys v0.18.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect golang.org/x/tools v0.19.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect sigs.k8s.io/yaml v1.4.0 // indirect
) )

1407
go.sum

File diff suppressed because it is too large Load Diff

View File

@ -22,7 +22,7 @@ var (
DefaultErrorHandler = func(ctx context.Context, s interface{}, w http.ResponseWriter, r *http.Request, err error, status int) { DefaultErrorHandler = func(ctx context.Context, s interface{}, w http.ResponseWriter, r *http.Request, err error, status int) {
w.WriteHeader(status) w.WriteHeader(status)
if _, cerr := w.Write([]byte(err.Error())); cerr != nil { if _, cerr := w.Write([]byte(err.Error())); cerr != nil {
logger.DefaultLogger.Errorf(ctx, "write failed: %v", cerr) logger.DefaultLogger.Error(ctx, fmt.Sprintf("write failed: %v", cerr))
} }
} }
DefaultContentType = "application/json" DefaultContentType = "application/json"
@ -109,16 +109,22 @@ func (h *Server) HTTPHandlerFunc(handler interface{}) (http.HandlerFunc, error)
md = metadata.New(len(r.Header) + 8) md = metadata.New(len(r.Header) + 8)
} }
for k, v := range r.Header { for k, v := range r.Header {
md[k] = strings.Join(v, ", ") md[k] = v
} }
md["RemoteAddr"] = r.RemoteAddr md["RemoteAddr"] = []string{r.RemoteAddr}
md["Method"] = r.Method md["Method"] = []string{r.Method}
md["URL"] = r.URL.String() md["URL"] = []string{r.URL.String()}
md["Proto"] = r.Proto md["Proto"] = []string{r.Proto}
md["ContentLength"] = fmt.Sprintf("%d", r.ContentLength) md["Content-Length"] = []string{fmt.Sprintf("%d", r.ContentLength)}
md["TransferEncoding"] = strings.Join(r.TransferEncoding, ",") md["Transfer-Encoding"] = r.TransferEncoding
md["Host"] = r.Host md["Host"] = []string{r.Host}
md["RequestURI"] = r.RequestURI md["RequestURI"] = []string{r.RequestURI}
if r.TLS != nil {
md["TLS"] = []string{"true"}
md["TLS-ALPN"] = []string{r.TLS.NegotiatedProtocol}
md["TLS-ServerName"] = []string{r.TLS.ServerName}
}
ctx = metadata.NewIncomingContext(ctx, md) ctx = metadata.NewIncomingContext(ctx, md)
path := r.URL.Path path := r.URL.Path
@ -257,10 +263,9 @@ func (h *Server) HTTPHandlerFunc(handler interface{}) (http.HandlerFunc, error)
} }
if nmd, ok := metadata.FromOutgoingContext(fctx); ok { if nmd, ok := metadata.FromOutgoingContext(fctx); ok {
for k, v := range nmd { for k, v := range nmd {
md.Set(k, v) md[k] = v
} }
} }
metadata.SetOutgoingContext(ctx, md)
return err return err
} }
@ -285,14 +290,12 @@ func (h *Server) HTTPHandlerFunc(handler interface{}) (http.HandlerFunc, error)
w.Header().Set(metadata.HeaderContentType, ct) w.Header().Set(metadata.HeaderContentType, ct)
if md, ok := metadata.FromOutgoingContext(ctx); ok { if md, ok := metadata.FromOutgoingContext(ctx); ok {
for k, v := range md { for k, v := range md {
w.Header().Set(k, v) w.Header()[k] = v
} }
} }
if md := getRspHeader(ctx); md != nil { if md := getRspHeader(ctx); md != nil {
for k, v := range md { for k, v := range md {
for _, vv := range v { w.Header()[k] = v
w.Header().Add(k, vv)
}
} }
} }
if nct := w.Header().Get(metadata.HeaderContentType); nct != ct { if nct := w.Header().Get(metadata.HeaderContentType); nct != ct {
@ -318,7 +321,7 @@ func (h *Server) HTTPHandlerFunc(handler interface{}) (http.HandlerFunc, error)
} }
if err != nil && handler.sopts.Logger.V(logger.ErrorLevel) { if err != nil && handler.sopts.Logger.V(logger.ErrorLevel) {
handler.sopts.Logger.Errorf(handler.sopts.Context, "handler err: %v", err) handler.sopts.Logger.Error(handler.sopts.Context, fmt.Sprintf("handler err: %v", err))
return return
} }
@ -328,7 +331,7 @@ func (h *Server) HTTPHandlerFunc(handler interface{}) (http.HandlerFunc, error)
w.WriteHeader(scode) w.WriteHeader(scode)
if _, cerr := w.Write(buf); cerr != nil { if _, cerr := w.Write(buf); cerr != nil {
handler.sopts.Logger.Errorf(ctx, "write failed: %v", cerr) handler.sopts.Logger.Error(ctx, fmt.Sprintf("write failed: %v", cerr))
} }
}, nil }, nil
} }
@ -346,23 +349,23 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
md = metadata.New(len(r.Header) + 8) md = metadata.New(len(r.Header) + 8)
} }
for k, v := range r.Header { for k, v := range r.Header {
md[k] = strings.Join(v, ", ") md[k] = v
} }
md["RemoteAddr"] = r.RemoteAddr md["RemoteAddr"] = []string{r.RemoteAddr}
if r.TLS != nil { if r.TLS != nil {
md["Scheme"] = "https" md["Scheme"] = []string{"https"}
} else { } else {
md["Scheme"] = "http" md["Scheme"] = []string{"http"}
} }
md["Method"] = r.Method md["Method"] = []string{r.Method}
md["URL"] = r.URL.String() md["URL"] = []string{r.URL.String()}
md["Proto"] = r.Proto md["Proto"] = []string{r.Proto}
md["ContentLength"] = fmt.Sprintf("%d", r.ContentLength) md["Content-Length"] = []string{fmt.Sprintf("%d", r.ContentLength)}
if len(r.TransferEncoding) > 0 { if len(r.TransferEncoding) > 0 {
md["TransferEncoding"] = strings.Join(r.TransferEncoding, ",") md["Transfer-Encoding"] = r.TransferEncoding
} }
md["Host"] = r.Host md["Host"] = []string{r.Host}
md["RequestURI"] = r.RequestURI md["RequestURI"] = []string{r.RequestURI}
ctx = metadata.NewIncomingContext(ctx, md) ctx = metadata.NewIncomingContext(ctx, md)
path := r.URL.Path path := r.URL.Path
@ -520,10 +523,9 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
if nmd, ok := metadata.FromOutgoingContext(fctx); ok { if nmd, ok := metadata.FromOutgoingContext(fctx); ok {
for k, v := range nmd { for k, v := range nmd {
md.Set(k, v) md[k] = v
} }
} }
metadata.SetOutgoingContext(ctx, md)
return err return err
} }
@ -548,7 +550,7 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set(metadata.HeaderContentType, ct) w.Header().Set(metadata.HeaderContentType, ct)
if md, ok := metadata.FromOutgoingContext(ctx); ok { if md, ok := metadata.FromOutgoingContext(ctx); ok {
for k, v := range md { for k, v := range md {
w.Header().Set(k, v) w.Header()[k] = v
} }
} }
if md := getRspHeader(ctx); md != nil { if md := getRspHeader(ctx); md != nil {
@ -581,7 +583,7 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
if err != nil && handler.sopts.Logger.V(logger.ErrorLevel) { if err != nil && handler.sopts.Logger.V(logger.ErrorLevel) {
handler.sopts.Logger.Errorf(handler.sopts.Context, "handler err: %v", err) handler.sopts.Logger.Error(handler.sopts.Context, fmt.Sprintf("handler err: %v", err))
return return
} }
@ -591,6 +593,6 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(scode) w.WriteHeader(scode)
if _, cerr := w.Write(buf); cerr != nil { if _, cerr := w.Write(buf); cerr != nil {
handler.sopts.Logger.Errorf(ctx, "write failed: %v", cerr) handler.sopts.Logger.Error(ctx, fmt.Sprintf("write failed: %v", cerr))
} }
} }

View File

@ -2,14 +2,36 @@ package meter // import "go.unistack.org/micro-server-http/v4/handler/meter"
import ( import (
"bytes" "bytes"
"compress/gzip"
"context" "context"
"io"
"strings"
"sync"
codecpb "go.unistack.org/micro-proto/v4/codec" codecpb "go.unistack.org/micro-proto/v4/codec"
"go.unistack.org/micro/v4/errors" "go.unistack.org/micro/v4/logger"
"go.unistack.org/micro/v4/metadata"
"go.unistack.org/micro/v4/meter" "go.unistack.org/micro/v4/meter"
options "go.unistack.org/micro/v4/options" "go.unistack.org/micro/v4/options"
) )
const (
contentEncodingHeader = "Content-Encoding"
acceptEncodingHeader = "Accept-Encoding"
)
var gzipPool = sync.Pool{
New: func() interface{} {
return gzip.NewWriter(nil)
},
}
var bufPool = sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(nil)
},
}
// guard to fail early // guard to fail early
var _ MeterServiceServer = (*Handler)(nil) var _ MeterServiceServer = (*Handler)(nil)
@ -57,12 +79,46 @@ func NewHandler(opts ...Option) *Handler {
} }
func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb.Frame) error { func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb.Frame) error {
buf := bytes.NewBuffer(nil) log, ok := logger.FromContext(ctx)
if err := h.opts.Meter.Write(buf, h.opts.MeterOptions...); err != nil { if !ok {
return errors.InternalServerError(h.opts.Name, "%v", err) log = logger.DefaultLogger
}
buf := bufPool.Get().(*bytes.Buffer)
defer bufPool.Put(buf)
buf.Reset()
w := io.Writer(buf)
if md, ok := metadata.FromContext(ctx); gzipAccepted(md) && ok {
md.Set(contentEncodingHeader, "gzip")
gz := gzipPool.Get().(*gzip.Writer)
defer gzipPool.Put(gz)
gz.Reset(w)
defer gz.Close()
w = gz
}
if err := h.opts.Meter.Write(w, h.opts.MeterOptions...); err != nil {
log.Error(ctx, "http/meter: write failed", err)
return nil
} }
rsp.Data = buf.Bytes() rsp.Data = buf.Bytes()
return nil return nil
} }
// gzipAccepted returns whether the client will accept gzip-encoded content.
func gzipAccepted(md metadata.Metadata) bool {
a, ok := md.Get(acceptEncodingHeader)
if !ok {
return false
}
if strings.Contains(a, "gzip") {
return true
}
return false
}

84
http.go
View File

@ -198,16 +198,16 @@ func (h *Server) newHTTPHandler(handler interface{}, opts ...options.Option) *ht
} }
if method.Name == "" && h.opts.Logger.V(logger.ErrorLevel) { if method.Name == "" && h.opts.Logger.V(logger.ErrorLevel) {
h.opts.Logger.Errorf(h.opts.Context, "nil method for %s", mname) h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("nil method for %s", mname))
continue continue
} }
mtype, err := prepareEndpoint(method) mtype, err := prepareEndpoint(method)
if err != nil && h.opts.Logger.V(logger.ErrorLevel) { if err != nil && h.opts.Logger.V(logger.ErrorLevel) {
h.opts.Logger.Errorf(h.opts.Context, "%v", err) h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("%v", err))
continue continue
} else if mtype == nil { } else if mtype == nil {
h.opts.Logger.Errorf(h.opts.Context, "nil mtype for %s", mname) h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("nil mtype for %s", mname))
continue continue
} }
@ -217,14 +217,14 @@ func (h *Server) newHTTPHandler(handler interface{}, opts ...options.Option) *ht
pth := &patHandler{mtype: mtype, name: name, rcvr: rcvr} pth := &patHandler{mtype: mtype, name: name, rcvr: rcvr}
hdlr.name = name hdlr.name = name
if err := hdlr.handlers.Insert([]string{md["Method"]}, md["Path"], pth); err != nil { if err := hdlr.handlers.Insert(md["Method"], md["Path"][0], pth); err != nil {
h.opts.Logger.Errorf(h.opts.Context, "cant add handler for %s %s", md["Method"], md["Path"]) h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("cant add handler for %s %s", md["Method"][0], md["Path"][0]))
} }
if h.registerRPC { if h.registerRPC {
h.opts.Logger.Infof(h.opts.Context, "register rpc handler for http.MethodPost %s /%s", hn, hn) h.opts.Logger.Info(h.opts.Context, fmt.Sprintf("register rpc handler for http.MethodPost %s /%s", hn, hn))
if err := hdlr.handlers.Insert([]string{http.MethodPost}, "/"+hn, pth); err != nil { if err := hdlr.handlers.Insert([]string{http.MethodPost}, "/"+hn, pth); err != nil {
h.opts.Logger.Errorf(h.opts.Context, "cant add rpc handler for http.MethodPost %s /%s", hn, hn) h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("cant add rpc handler for http.MethodPost %s /%s", hn, hn))
} }
} }
} }
@ -248,16 +248,16 @@ func (h *Server) newHTTPHandler(handler interface{}, opts ...options.Option) *ht
} }
if method.Name == "" && h.opts.Logger.V(logger.ErrorLevel) { if method.Name == "" && h.opts.Logger.V(logger.ErrorLevel) {
h.opts.Logger.Errorf(h.opts.Context, "nil method for %s", mname) h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("nil method for %s", mname))
continue continue
} }
mtype, err := prepareEndpoint(method) mtype, err := prepareEndpoint(method)
if err != nil && h.opts.Logger.V(logger.ErrorLevel) { if err != nil && h.opts.Logger.V(logger.ErrorLevel) {
h.opts.Logger.Errorf(h.opts.Context, "%v", err) h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("%v", err))
continue continue
} else if mtype == nil { } else if mtype == nil {
h.opts.Logger.Errorf(h.opts.Context, "nil mtype for %s", mname) h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("nil mtype for %s", mname))
continue continue
} }
@ -268,13 +268,13 @@ func (h *Server) newHTTPHandler(handler interface{}, opts ...options.Option) *ht
hdlr.name = name hdlr.name = name
if err := hdlr.handlers.Insert([]string{md.Method}, md.Path, pth); err != nil { if err := hdlr.handlers.Insert([]string{md.Method}, md.Path, pth); err != nil {
h.opts.Logger.Errorf(h.opts.Context, "cant add handler for %s %s", md.Method, md.Path) h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("cant add handler for %s %s", md.Method, md.Path))
} }
if h.registerRPC { if h.registerRPC {
h.opts.Logger.Infof(h.opts.Context, "register rpc handler for http.MethodPost %s /%s", hn, hn) h.opts.Logger.Info(h.opts.Context, fmt.Sprintf("register rpc handler for http.MethodPost %s /%s", hn, hn))
if err := hdlr.handlers.Insert([]string{http.MethodPost}, "/"+hn, pth); err != nil { if err := hdlr.handlers.Insert([]string{http.MethodPost}, "/"+hn, pth); err != nil {
h.opts.Logger.Errorf(h.opts.Context, "cant add rpc handler for http.MethodPost %s /%s", hn, hn) h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("cant add rpc handler for http.MethodPost %s /%s", hn, hn))
} }
} }
} }
@ -308,7 +308,7 @@ func (h *Server) Register() error {
if err != nil { if err != nil {
return err return err
} }
service.Nodes[0].Metadata["protocol"] = "http" service.Nodes[0].Metadata.Set("protocol", "http")
service.Endpoints = eps service.Endpoints = eps
h.RLock() h.RLock()
@ -317,7 +317,7 @@ func (h *Server) Register() error {
if !registered { if !registered {
if config.Logger.V(logger.InfoLevel) { if config.Logger.V(logger.InfoLevel) {
config.Logger.Infof(config.Context, "Register [%s] Registering node: %s", config.Register.String(), service.Nodes[0].ID) config.Logger.Info(config.Context, fmt.Sprintf("Register [%s] Registering node: %s", config.Register.String(), service.Nodes[0].ID))
} }
} }
@ -350,7 +350,7 @@ func (h *Server) Deregister() error {
} }
if config.Logger.V(logger.InfoLevel) { if config.Logger.V(logger.InfoLevel) {
config.Logger.Infof(config.Context, "Deregistering node: %s", service.Nodes[0].ID) config.Logger.Info(config.Context, fmt.Sprintf("Deregistering node: %s", service.Nodes[0].ID))
} }
if err := server.DefaultDeregisterFunc(service, config); err != nil { if err := server.DefaultDeregisterFunc(service, config); err != nil {
@ -400,7 +400,7 @@ func (h *Server) Start() error {
} }
if config.Logger.V(logger.InfoLevel) { if config.Logger.V(logger.InfoLevel) {
config.Logger.Infof(config.Context, "Listening on %s", ts.Addr().String()) config.Logger.Info(config.Context, fmt.Sprintf("Listening on %s", ts.Addr().String()))
} }
h.Lock() h.Lock()
@ -408,7 +408,6 @@ func (h *Server) Start() error {
h.Unlock() h.Unlock()
var handler http.Handler var handler http.Handler
var srvFunc func(net.Listener) error
// nolint: nestif // nolint: nestif
if h.opts.Context != nil { if h.opts.Context != nil {
@ -441,7 +440,7 @@ func (h *Server) Start() error {
if err := config.RegisterCheck(h.opts.Context); err != nil { if err := config.RegisterCheck(h.opts.Context); err != nil {
if config.Logger.V(logger.ErrorLevel) { if config.Logger.V(logger.ErrorLevel) {
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s", config.Name, config.ID, err) config.Logger.Error(config.Context, fmt.Sprintf("Server %s-%s register check error: %s", config.Name, config.ID, err))
} }
} else { } else {
if err = h.Register(); err != nil { if err = h.Register(); err != nil {
@ -451,6 +450,7 @@ func (h *Server) Start() error {
fn := handler fn := handler
var hs *http.Server
if h.opts.Context != nil { if h.opts.Context != nil {
if mwf, ok := h.opts.Context.Value(middlewareKey{}).([]func(http.Handler) http.Handler); ok && len(mwf) > 0 { if mwf, ok := h.opts.Context.Value(middlewareKey{}).([]func(http.Handler) http.Handler); ok && len(mwf) > 0 {
// wrap the handler func // wrap the handler func
@ -458,25 +458,19 @@ func (h *Server) Start() error {
fn = mwf[i-1](fn) 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 hs.Handler = fn
srvFunc = hs.Serve } else {
hs = &http.Server{Handler: fn}
} }
} }
if srvFunc != nil { go func() {
go func() { if cerr := hs.Serve(ts); cerr != nil && !errors.Is(cerr, net.ErrClosed) {
if cerr := srvFunc(ts); cerr != nil && !errors.Is(cerr, net.ErrClosed) { h.opts.Logger.Error(h.opts.Context, cerr)
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() { go func() {
t := new(time.Ticker) t := new(time.Ticker)
@ -502,28 +496,28 @@ func (h *Server) Start() error {
// nolint: nestif // nolint: nestif
if rerr != nil && registered { if rerr != nil && registered {
if config.Logger.V(logger.ErrorLevel) { if config.Logger.V(logger.ErrorLevel) {
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s, deregister it", config.Name, config.ID, rerr) config.Logger.Error(config.Context, fmt.Sprintf("Server %s-%s register check error: %s, deregister it", config.Name, config.ID, rerr))
} }
// deregister self in case of error // deregister self in case of error
if err := h.Deregister(); err != nil { if err := h.Deregister(); err != nil {
if config.Logger.V(logger.ErrorLevel) { if config.Logger.V(logger.ErrorLevel) {
config.Logger.Errorf(config.Context, "Server %s-%s deregister error: %s", config.Name, config.ID, err) config.Logger.Error(config.Context, fmt.Sprintf("Server %s-%s deregister error: %s", config.Name, config.ID, err))
} }
} }
} else if rerr != nil && !registered { } else if rerr != nil && !registered {
if config.Logger.V(logger.ErrorLevel) { if config.Logger.V(logger.ErrorLevel) {
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s", config.Name, config.ID, rerr) config.Logger.Error(config.Context, fmt.Sprintf("Server %s-%s register check error: %s", config.Name, config.ID, rerr))
} }
continue continue
} }
if err := h.Register(); err != nil { if err := h.Register(); err != nil {
if config.Logger.V(logger.ErrorLevel) { if config.Logger.V(logger.ErrorLevel) {
config.Logger.Errorf(config.Context, "Server %s-%s register error: %s", config.Name, config.ID, err) config.Logger.Error(config.Context, fmt.Sprintf("Server %s-%s register error: %s", config.Name, config.ID, err))
} }
} }
if err := h.Register(); err != nil { if err := h.Register(); err != nil {
config.Logger.Errorf(config.Context, "Server register error: %s", err) config.Logger.Error(config.Context, fmt.Sprintf("Server register error: %s", err))
} }
// wait for exit // wait for exit
case ch = <-h.exit: case ch = <-h.exit:
@ -533,10 +527,18 @@ func (h *Server) Start() error {
// deregister // deregister
if err := h.Deregister(); err != nil { if err := h.Deregister(); err != nil {
config.Logger.Errorf(config.Context, "Server deregister error: %s", err) config.Logger.Error(config.Context, fmt.Sprintf("Server deregister error: %s", err))
} }
ch <- ts.Close() ctx, cancel := context.WithTimeout(context.Background(), h.opts.GracefulTimeout)
defer cancel()
err := hs.Shutdown(ctx)
if err != nil {
err = hs.Close()
}
ch <- err
}() }()
return nil return nil

View File

@ -31,7 +31,7 @@ func FillRequest(ctx context.Context, req interface{}, opts ...FillRequestOption
} }
} }
cookies := strings.Split(md["Cookie"], ";") cookies := md["Cookie"]
cmd := make(map[string]string, len(cookies)) cmd := make(map[string]string, len(cookies))
for _, cookie := range cookies { for _, cookie := range cookies {
kv := strings.Split(cookie, "=") kv := strings.Split(cookie, "=")