Skip to content

Commit

Permalink
Fix: Gracefully handle ErrRaftShutdown during Node shutdown
Browse files Browse the repository at this point in the history
Updated the `Shutdown` method in `raft.go` to gracefully handle the `ErrRaftShutdown` error. This change ensures that if the Raft node is already shut down, the error is ignored, preventing unnecessary error handling.
  • Loading branch information
sinadarbouy committed Dec 13, 2024
1 parent 586efd7 commit f6aba9f
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,10 @@ func (n *Node) forwardToLeader(data []byte, timeout time.Duration) error {
return nil
}

// Update Shutdown to clean up RPC resources.
// Shutdown gracefully stops the Node by stopping the gRPC server, closing RPC client connections,
// and shutting down the underlying Raft node. It returns an error if the Raft node fails to
// shutdown properly, ignoring the ErrRaftShutdown error which indicates the node was already
// shutdown.
func (n *Node) Shutdown() error {
if n.rpcServer != nil {
n.rpcServer.GracefulStop()
Expand All @@ -277,7 +280,7 @@ func (n *Node) Shutdown() error {
n.rpcClient.close()
}

if err := n.raft.Shutdown().Error(); err != nil {
if err := n.raft.Shutdown().Error(); err != nil && !errors.Is(err, raft.ErrRaftShutdown) {
return fmt.Errorf("failed to shutdown raft node: %w", err)
}
return nil
Expand Down

0 comments on commit f6aba9f

Please sign in to comment.