Skip to content

Commit

Permalink
futures can react to shutdown (hashicorp#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanshasselberg committed Feb 27, 2020
1 parent 9a647f6 commit 2e99741
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
13 changes: 9 additions & 4 deletions future.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 2e99741

Please sign in to comment.