From 2e997410f28efe8106fc606d21cb655be1b05dee Mon Sep 17 00:00:00 2001 From: Hans Hasselberg Date: Thu, 27 Feb 2020 06:19:10 -0500 Subject: [PATCH] futures can react to shutdown (#390) --- future.go | 13 +++++++++---- snapshot.go | 1 + 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/future.go b/future.go index 6346b453b19c..6de2daea4f9a 100644 --- a/future.go +++ b/future.go @@ -84,9 +84,10 @@ func (e errorFuture) Index() uint64 { // deferError can be embedded to allow a future // to provide an error in the future. type deferError struct { - err error - errCh chan error - responded bool + err error + errCh chan error + responded bool + ShutdownCh chan struct{} } func (d *deferError) init() { @@ -103,7 +104,11 @@ func (d *deferError) Error() error { if d.errCh == nil { panic("waiting for response on nil channel") } - d.err = <-d.errCh + select { + case d.err = <-d.errCh: + case <-d.ShutdownCh: + d.err = ErrRaftShutdown + } return d.err } diff --git a/snapshot.go b/snapshot.go index f4c39451453c..805a09d7074b 100644 --- a/snapshot.go +++ b/snapshot.go @@ -146,6 +146,7 @@ func (r *Raft) takeSnapshot() (string, error) { // We have to use the future here to safely get this information since // it is owned by the main thread. configReq := &configurationsFuture{} + configReq.ShutdownCh = r.shutdownCh configReq.init() select { case r.configurationsCh <- configReq: