Skip to content

Commit

Permalink
Merge pull request #7585 from fasaxc/v3.26-typha-div-0
Browse files Browse the repository at this point in the history
[v3.26] Fix typha divide by 0
  • Loading branch information
fasaxc committed Apr 25, 2023
2 parents d3d6be5 + 7a5ea61 commit 47c416c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
31 changes: 31 additions & 0 deletions typha/fv-tests/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,37 @@ var _ = Describe("With an in-process Server", func() {
})
})

var _ = Describe("with no client connections", func() {
var h *ServerHarness

BeforeEach(func() {
log.SetLevel(log.InfoLevel) // Debug too verbose for tests with a few clients.
h = NewHarness()
})

AfterEach(func() {
h.Stop()
})

JustBeforeEach(func() {
h.Start()
})

Describe("300s shutdown timer and 1s max drop interval", func() {
It("should shut drop 1 client per second", func() {
Expect(h.Server.NumActiveConnections()).To(Equal(0))

h.Server.ShutDownGracefully()
finishedC := make(chan struct{})
go func() {
defer close(finishedC)
h.Server.Finished.Wait()
}()
Eventually(finishedC).Should(BeClosed())
})
})
})

var _ = Describe("with 5 client connections", func() {
const numClients = 5

Expand Down
8 changes: 7 additions & 1 deletion typha/pkg/syncserver/sync_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,12 @@ func (s *Server) handleGracefulShutDown(cxt context.Context, serverCancelFn cont
}

numConns := s.NumActiveConnections()
if numConns == 0 {
logCxt.Info("No active connections; shutting down immediately.")
serverCancelFn()
return
}

// Aim to close connections within 95% of the allotted time.
dropInterval := s.config.ShutdownTimeout * 95 / 100 / time.Duration(numConns)
logCtx := log.WithFields(log.Fields{
Expand Down Expand Up @@ -601,7 +607,7 @@ func (s *Server) handleGracefulShutDown(cxt context.Context, serverCancelFn cont
// We don't need to worry about that because serverCancelFn will shut down all remaining connections
// by canceling their parent context.
serverCancelFn()
break
return
}
case <-cxt.Done():
logCxt.Info("Context asked us to stop")
Expand Down

0 comments on commit 47c416c

Please sign in to comment.