Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

独立服务http超时不生效 #3460

Closed
wants to merge 10 commits into from
54 changes: 36 additions & 18 deletions transport/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ func Timeout(timeout time.Duration) ServerOption {
}
}

// ReadTimeout with server timeout.
func ReadTimeout(timeout time.Duration) ServerOption {
return func(s *Server) {
s.readTimeout = timeout
}
}

// WriteTimeout with server timeout.
func WriteTimeout(timeout time.Duration) ServerOption {
return func(s *Server) {
s.writeTimeout = timeout
}
}

// Logger with server logger.
// Deprecated: use global logger instead.
func Logger(log.Logger) ServerOption {
Expand Down Expand Up @@ -156,22 +170,24 @@ func MethodNotAllowedHandler(handler http.Handler) ServerOption {
// Server is an HTTP server wrapper.
type Server struct {
*http.Server
lis net.Listener
tlsConf *tls.Config
endpoint *url.URL
err error
network string
address string
timeout time.Duration
filters []FilterFunc
middleware matcher.Matcher
decVars DecodeRequestFunc
decQuery DecodeRequestFunc
decBody DecodeRequestFunc
enc EncodeResponseFunc
ene EncodeErrorFunc
strictSlash bool
router *mux.Router
lis net.Listener
tlsConf *tls.Config
endpoint *url.URL
err error
network string
address string
timeout time.Duration
readTimeout time.Duration
writeTimeout time.Duration
filters []FilterFunc
middleware matcher.Matcher
decVars DecodeRequestFunc
decQuery DecodeRequestFunc
decBody DecodeRequestFunc
enc EncodeResponseFunc
ene EncodeErrorFunc
strictSlash bool
router *mux.Router
}

// NewServer creates an HTTP server by options.
Expand All @@ -197,8 +213,10 @@ func NewServer(opts ...ServerOption) *Server {
srv.router.StrictSlash(srv.strictSlash)
srv.router.Use(srv.filter())
srv.Server = &http.Server{
Handler: FilterChain(srv.filters...)(srv.router),
TLSConfig: srv.tlsConf,
Handler: FilterChain(srv.filters...)(srv.router),
TLSConfig: srv.tlsConf,
ReadTimeout: srv.readTimeout,
WriteTimeout: srv.writeTimeout,
}
return srv
}
Expand Down