Skip to content

Commit

Permalink
feat: allow connections during shutdown
Browse files Browse the repository at this point in the history
When the proxy starts its shut down, it will now accept additional
connections when the max-sigterm-delay flag is passed. As before, if any
connections are still open after the delay has elapsed, the Proxy will
report an error.

Fixes #1640.
  • Loading branch information
enocom committed May 22, 2023
1 parent bf890c2 commit a96888f
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,37 +612,25 @@ func (m MultiErr) Error() string {
// Close triggers the proxyClient to shut down.
func (c *Client) Close() error {
mnts := c.mnts

var mErr MultiErr

// If FUSE is enabled, unmount it and save a reference to any existing
// socket mounts.
if c.fuseDir != "" {
if err := c.unmountFUSE(); err != nil {
mErr = append(mErr, err)
}
mnts = c.fuseMounts()
}

// First, close all open socket listeners to prevent additional connections.
for _, m := range mnts {
err := m.Close()
if err != nil {
mErr = append(mErr, err)
}
}
if c.fuseDir != "" {
c.waitForFUSEMounts()
}
// Next, close the dialer to prevent any additional refreshes.
// Close the dialer to prevent any additional refreshes.
cErr := c.dialer.Close()
if cErr != nil {
mErr = append(mErr, cErr)
}
if c.conf.WaitOnClose == 0 {
if len(mErr) > 0 {
return mErr
}
return nil
}

// Start a timer for clean shutdown (where all connections are closed).
// While the timer runs, additional connections will be accepted.
timeout := time.After(c.conf.WaitOnClose)
t := time.NewTicker(100 * time.Millisecond)
defer t.Stop()
Expand All @@ -656,9 +644,22 @@ func (c *Client) Close() error {
}
break
}
// Close all open socket listeners. Time to complete shutdown.
for _, m := range mnts {
err := m.Close()
if err != nil {
mErr = append(mErr, err)
}
}
if c.fuseDir != "" {
c.waitForFUSEMounts()
}
// Verify that all connections are closed.
open := atomic.LoadUint64(&c.connCount)
if open > 0 {
mErr = append(mErr, fmt.Errorf("%d connection(s) still open after waiting %v", open, c.conf.WaitOnClose))
if c.conf.WaitOnClose > 0 && open > 0 {
openErr := fmt.Errorf(
"%d connection(s) still open after waiting %v", open, c.conf.WaitOnClose)
mErr = append(mErr, openErr)
}
if len(mErr) > 0 {
return mErr
Expand Down

0 comments on commit a96888f

Please sign in to comment.