sigs.k8s.io/kueue@v0.6.2/pkg/util/routine/wrapper.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package routine 18 19 // Wrapper is used to wrap a function that will run in a goroutine. 20 type Wrapper interface { 21 Run(func()) 22 } 23 24 var _ Wrapper = &wrapper{} 25 26 var DefaultWrapper Wrapper = NewWrapper(nil, nil) 27 28 // wrapper implement the Wrapper interface. 29 // before() will be executed before the function starts, and after() 30 // will be executed after the function ends. 31 type wrapper struct { 32 before func() 33 after func() 34 } 35 36 func (l *wrapper) Run(f func()) { 37 if l.before != nil { 38 l.before() 39 } 40 go func() { 41 if l.after != nil { 42 defer l.after() 43 } 44 f() 45 }() 46 } 47 48 func NewWrapper(before, after func()) Wrapper { 49 return &wrapper{ 50 before: before, 51 after: after, 52 } 53 }