File tree 1 file changed +17
-1
lines changed
1 file changed +17
-1
lines changed Original file line number Diff line number Diff line change @@ -4,15 +4,31 @@ import (
4
4
"bufio"
5
5
"bytes"
6
6
"os"
7
+ "sync"
7
8
9
+ "go.uber.org/atomic"
8
10
"golang.org/x/xerrors"
9
11
)
10
12
13
+ var isContainerizedCacheOK atomic.Bool
14
+ var isContainerizedCacheErr atomic.Error
15
+ var isContainerizedCacheOnce sync.Once
16
+
11
17
// IsContainerized returns whether the host is containerized.
12
18
// This is adapted from https://github.com/elastic/go-sysinfo/tree/main/providers/linux/container.go#L31
13
19
// with modifications to support Sysbox containers.
14
20
// On non-Linux platforms, it always returns false.
15
- func IsContainerized () (bool , error ) {
21
+ // The result is only computed once and stored for subsequent calls.
22
+ func IsContainerized () (ok bool , err error ) {
23
+ isContainerizedCacheOnce .Do (func () {
24
+ ok , err = isContainerizedOnce ()
25
+ isContainerizedCacheOK .Store (ok )
26
+ isContainerizedCacheErr .Store (err )
27
+ })
28
+ return isContainerizedCacheOK .Load (), isContainerizedCacheErr .Load ()
29
+ }
30
+
31
+ func isContainerizedOnce () (bool , error ) {
16
32
data , err := os .ReadFile (procOneCgroup )
17
33
if err != nil {
18
34
if os .IsNotExist (err ) { // how?
You can’t perform that action at this time.
0 commit comments