Skip to content

Commit 369baaa

Browse files
committed
feat(httpd): configurable http read timeout
HTTP read timeout is useful to avoid half-open connections due to bad network. See #15410.
1 parent 619f0ab commit 369baaa

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

services/httpd/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type Config struct {
6464
MaxEnqueuedWriteLimit int `toml:"max-enqueued-write-limit"`
6565
EnqueuedWriteTimeout time.Duration `toml:"enqueued-write-timeout"`
6666
TLS *tls.Config `toml:"-"`
67+
ReadTimeout time.Duration `toml:"read-timeout"`
6768
}
6869

6970
// NewConfig returns a new Config with default settings.

services/httpd/service.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ type Service struct {
6565
Handler *Handler
6666

6767
Logger *zap.Logger
68+
69+
ReadTimeout time.Duration
6870
}
6971

7072
// NewService returns a new instance of Service.
@@ -82,6 +84,7 @@ func NewService(c Config) *Service {
8284
bindSocket: c.BindSocket,
8385
Handler: NewHandler(c),
8486
Logger: zap.NewNop(),
87+
ReadTimeout: c.ReadTimeout,
8588
}
8689
if s.tlsConfig == nil {
8790
s.tlsConfig = new(tls.Config)
@@ -246,7 +249,7 @@ func (s *Service) serveUnixSocket() {
246249
func (s *Service) serve(listener net.Listener) {
247250
// The listener was closed so exit
248251
// See https://github.com/golang/go/issues/4373
249-
err := http.Serve(listener, s.Handler)
252+
err := (&http.Server{Handler: s.Handler, ReadTimeout: s.ReadTimeout}).Serve(listener)
250253
if err != nil && !strings.Contains(err.Error(), "closed") {
251254
s.err <- fmt.Errorf("listener failed: addr=%s, err=%s", s.Addr(), err)
252255
}

0 commit comments

Comments
 (0)