-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
33 lines (30 loc) · 963 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package whalewall
import (
"context"
"errors"
"time"
)
// withTimeout runs f with a timeout derived from [context.WithTimeout].
// Using withTimeout guarantees that:
//
// - ctx is only shadowed in withTimeout's scope
// - The child context will have it's resources released immediately
// after f returns
//
// The main goal of withTimeout is to prevent shadowing ctx with a
// context with a timeout, having that timeout expire and the next call
// that uses ctx immediately fail.
func withTimeout[T, E any](ctx context.Context, timeout time.Duration, f func(ctx context.Context) (T, E)) (T, E) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
return f(ctx)
}
// ignoringErr calls f and discards the error if any error in the error
// tree matches errToIgnore.
func ignoringErr(f func() error, errToIgnore error) error {
err := f()
if err == nil || (err != nil && errors.Is(err, errToIgnore)) {
return nil
}
return err
}