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

     1  // Copyright 2018 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package revert
    16  
    17  // FinalizeFunc is a function returned by a successful function call, which
    18  // finalizes the initial function call. A call that returns an error should
    19  // return a nil FinalizeFunc.
    20  // When a call returns both a RevertFunc and a FinalizeFunc, at most one may be
    21  // called. The side effects of the FinalizeFunc are not reverted by the
    22  // RevertFunc.
    23  type FinalizeFunc func()
    24  
    25  // FinalizeList is a list of FinalizeFuncs to be executed in the same order
    26  // they were appended.
    27  type FinalizeList struct {
    28  	// finalizeFuncs is the list of finalize functions in the order they were
    29  	// appended.
    30  	finalizeFuncs []FinalizeFunc
    31  }
    32  
    33  // Append appends the given FinalizeFunc at the end of this list. If the
    34  // function is nil, it is ignored.
    35  func (f *FinalizeList) Append(finalizeFunc FinalizeFunc) {
    36  	if finalizeFunc != nil {
    37  		f.finalizeFuncs = append(f.finalizeFuncs, finalizeFunc)
    38  	}
    39  }
    40  
    41  // Finalize executes all the FinalizeFuncs in the given list in the same order
    42  // they were pushed.
    43  func (f *FinalizeList) Finalize() {
    44  	for _, f := range f.finalizeFuncs {
    45  		f()
    46  	}
    47  }