github.com/hernad/nomad@v1.6.112/nomad/structs/batch_future.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package structs 5 6 // BatchFuture is used to wait on a batch update to complete 7 type BatchFuture struct { 8 doneCh chan struct{} 9 err error 10 index uint64 11 } 12 13 // NewBatchFuture creates a new batch future 14 func NewBatchFuture() *BatchFuture { 15 return &BatchFuture{ 16 doneCh: make(chan struct{}), 17 } 18 } 19 20 // Wait is used to block for the future to complete and returns the error 21 func (b *BatchFuture) Wait() error { 22 <-b.doneCh 23 return b.err 24 } 25 26 // WaitCh is used to block for the future to complete 27 func (b *BatchFuture) WaitCh() <-chan struct{} { 28 return b.doneCh 29 } 30 31 // Error is used to return the error of the batch, only after Wait() 32 func (b *BatchFuture) Error() error { 33 return b.err 34 } 35 36 // Index is used to return the index of the batch, only after Wait() 37 func (b *BatchFuture) Index() uint64 { 38 return b.index 39 } 40 41 // Respond is used to unblock the future 42 func (b *BatchFuture) Respond(index uint64, err error) { 43 b.index = index 44 b.err = err 45 close(b.doneCh) 46 }