Debian Codegraph @

api/internal/utils/timedcall.go

raw
1 // Copyright 2020 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package utils
5
6 import (
7 "time"
8 )
9
10 // TimedCall runs fn, failing if it doesn't complete in the given duration.
11 // The description is used in the timeout error message.
12 func TimedCall(description string, d time.Duration, fn func() error) error {
13 done := make(chan error, 1)
14 timer := time.NewTimer(d)
15 defer timer.Stop()
16 go func() { done <- fn() }()
17 select {
18 case err := <-done:
19 return err
20 case <-timer.C:
21 return NewErrTimeOut(d, description)
22 }
23 }