R.B. Boyer d59efd390c
test: general cleanup and fixes for the container integration test suite (#15959)
- remove dep on consul main module
- use 'consul tls' subcommands instead of tlsutil
- use direct json config construction instead of agent/config structs
- merge libcluster and libagent packages together
- more widely use BuildContext
- get the OSS/ENT runner stuff working properly
- reduce some flakiness
- fix some correctness related to http/https API
2023-01-11 15:34:27 -06:00

29 lines
856 B
Go

package utils
// ResettableDefer is a way to capture a series of cleanup functions and
// bulk-cancel them. Ideal to use in a long constructor function before the
// overall Close/Stop/Terminate method is ready to use to tear down all of the
// portions properly.
type ResettableDefer struct {
cleanupFns []func()
}
// Add registers another function to call at Execute time.
func (d *ResettableDefer) Add(f func()) {
d.cleanupFns = append(d.cleanupFns, f)
}
// Reset clears the pending defer work.
func (d *ResettableDefer) Reset() {
d.cleanupFns = nil
}
// Execute actually executes the functions registered by Add in the reverse
// order of their call order (like normal defer blocks).
func (d *ResettableDefer) Execute() {
// Run these in reverse order, like defer blocks.
for i := len(d.cleanupFns) - 1; i >= 0; i-- {
d.cleanupFns[i]()
}
}