github.com/cilium/cilium@v1.16.2/pkg/revert/finalize.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package revert
     5  
     6  // FinalizeFunc is a function returned by a successful function call, which
     7  // finalizes the initial function call. A call that returns an error should
     8  // return a nil FinalizeFunc.
     9  // When a call returns both a RevertFunc and a FinalizeFunc, at most one may be
    10  // called. The side effects of the FinalizeFunc are not reverted by the
    11  // RevertFunc.
    12  type FinalizeFunc func()
    13  
    14  // FinalizeList is a list of FinalizeFuncs to be executed in the same order
    15  // they were appended.
    16  type FinalizeList struct {
    17  	// finalizeFuncs is the list of finalize functions in the order they were
    18  	// appended.
    19  	finalizeFuncs []FinalizeFunc
    20  }
    21  
    22  // Append appends the given FinalizeFunc at the end of this list. If the
    23  // function is nil, it is ignored.
    24  func (f *FinalizeList) Append(finalizeFunc FinalizeFunc) {
    25  	if finalizeFunc != nil {
    26  		f.finalizeFuncs = append(f.finalizeFuncs, finalizeFunc)
    27  	}
    28  }
    29  
    30  // Finalize executes all the FinalizeFuncs in the given list in the same order
    31  // they were pushed.
    32  func (f *FinalizeList) Finalize() {
    33  	for _, f := range f.finalizeFuncs {
    34  		f()
    35  	}
    36  }