Skip to content

Commit

Permalink
Add proxy server timeout config options (#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
benbjohnson authored Nov 16, 2023
1 parent e769043 commit d861e91
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/litefs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func NewConfig() Config {

config.Proxy.MaxLag = http.DefaultMaxLag
config.Proxy.PrimaryRedirectTimeout = http.DefaultPrimaryRedirectTimeout
config.Proxy.ReadTimeout = http.DefaultReadTimeout
config.Proxy.ReadHeaderTimeout = http.DefaultReadHeaderTimeout
config.Proxy.WriteTimeout = http.DefaultWriteTimeout
config.Proxy.IdleTimeout = http.DefaultIdleTimeout

config.Tracing.Enabled = true
config.Tracing.MaxSize = DefaultTracingMaxSize
Expand Down Expand Up @@ -130,6 +134,11 @@ type ProxyConfig struct {
Passthrough []string `yaml:"passthrough"`
AlwaysForward []string `yaml:"always-forward"`
PrimaryRedirectTimeout time.Duration `yaml:"primary-redirect-timeout"`

ReadTimeout time.Duration `yaml:"read-timeout"`
ReadHeaderTimeout time.Duration `yaml:"read-header-timeout"`
WriteTimeout time.Duration `yaml:"write-timeout"`
IdleTimeout time.Duration `yaml:"idle-timeout"`
}

// LeaseConfig represents a generic configuration for all lease types.
Expand Down
5 changes: 5 additions & 0 deletions cmd/litefs/mount_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,11 @@ func (c *MountCommand) runProxyServer(ctx context.Context) error {
server.Passthroughs = passthroughs
server.AlwaysForward = alwaysForward
server.PrimaryRedirectTimeout = c.Config.Proxy.PrimaryRedirectTimeout
server.ReadTimeout = c.Config.Proxy.ReadTimeout
server.ReadHeaderTimeout = c.Config.Proxy.ReadHeaderTimeout
server.WriteTimeout = c.Config.Proxy.WriteTimeout
server.IdleTimeout = c.Config.Proxy.IdleTimeout

if err := server.Listen(); err != nil {
return err
}
Expand Down
20 changes: 20 additions & 0 deletions http/proxy_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ const (
DefaultMaxLag = 10 * time.Second

DefaultCookieExpiry = 5 * time.Minute

DefaultReadTimeout = 0
DefaultReadHeaderTimeout = 10 * time.Second
DefaultWriteTimeout = 0
DefaultIdleTimeout = 30 * time.Second
)

var ErrProxyServerClosed = fmt.Errorf("canceled, proxy server closed")
Expand Down Expand Up @@ -76,6 +81,12 @@ type ProxyServer struct {
// Time before cookie expires on client.
CookieExpiry time.Duration

// HTTP server timeouts
ReadTimeout time.Duration
ReadHeaderTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration

HTTPTransport *http.Transport
}

Expand All @@ -89,6 +100,10 @@ func NewProxyServer(store *litefs.Store) *ProxyServer {
MaxLag: DefaultMaxLag,
CookieExpiry: DefaultCookieExpiry,
PrimaryRedirectTimeout: DefaultPrimaryRedirectTimeout,
ReadTimeout: DefaultReadTimeout,
ReadHeaderTimeout: DefaultReadHeaderTimeout,
WriteTimeout: DefaultWriteTimeout,
IdleTimeout: DefaultIdleTimeout,
}

s.ctx, s.cancel = context.WithCancelCause(context.Background())
Expand Down Expand Up @@ -129,6 +144,11 @@ func (s *ProxyServer) Listen() (err error) {
}

func (s *ProxyServer) Serve() {
s.httpServer.ReadTimeout = s.ReadTimeout
s.httpServer.ReadHeaderTimeout = s.ReadHeaderTimeout
s.httpServer.WriteTimeout = s.WriteTimeout
s.httpServer.IdleTimeout = s.IdleTimeout

s.g.Go(func() error {
if err := s.httpServer.Serve(s.ln); s.ctx.Err() != nil {
return err
Expand Down

0 comments on commit d861e91

Please sign in to comment.