@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"errors"
22
22
"fmt"
23
+ "sync"
23
24
24
25
sandboxstore "github.com/containerd/containerd/v2/internal/cri/store/sandbox"
25
26
"github.com/containerd/errdefs"
@@ -34,20 +35,33 @@ func (c *criService) ListPodSandboxStats(
34
35
r * runtime.ListPodSandboxStatsRequest ,
35
36
) (* runtime.ListPodSandboxStatsResponse , error ) {
36
37
sandboxes := c .sandboxesForListPodSandboxStatsRequest (r )
38
+ stats , errs := make ([]* runtime.PodSandboxStats , len (sandboxes )), make ([]error , len (sandboxes ))
39
+
40
+ var wg sync.WaitGroup
41
+ for i , sandbox := range sandboxes {
42
+ i := i
43
+ wg .Add (1 )
44
+ go func () {
45
+ defer wg .Done ()
46
+ sandboxStats , err := c .podSandboxStats (ctx , sandbox )
47
+ switch {
48
+ case errdefs .IsUnavailable (err ), errdefs .IsNotFound (err ):
49
+ log .G (ctx ).WithField ("podsandboxid" , sandbox .ID ).WithError (err ).Debug ("failed to get pod sandbox stats, this is likely a transient error" )
50
+ case errors .Is (err , ttrpc .ErrClosed ):
51
+ log .G (ctx ).WithField ("podsandboxid" , sandbox .ID ).WithError (err ).Debug ("failed to get pod sandbox stats, connection closed" )
52
+ case err != nil :
53
+ errs [i ] = fmt .Errorf ("failed to decode sandbox container metrics for sandbox %q: %w" , sandbox .ID , err )
54
+ default :
55
+ stats [i ] = sandboxStats
56
+ }
57
+ }()
58
+ }
59
+ wg .Wait ()
37
60
38
- var errs []error
39
61
podSandboxStats := new (runtime.ListPodSandboxStatsResponse )
40
- for _ , sandbox := range sandboxes {
41
- sandboxStats , err := c .podSandboxStats (ctx , sandbox )
42
- switch {
43
- case errdefs .IsUnavailable (err ), errdefs .IsNotFound (err ):
44
- log .G (ctx ).WithField ("podsandboxid" , sandbox .ID ).Debugf ("failed to get pod sandbox stats, this is likely a transient error: %v" , err )
45
- case errors .Is (err , ttrpc .ErrClosed ):
46
- log .G (ctx ).WithField ("podsandboxid" , sandbox .ID ).Debugf ("failed to get pod sandbox stats, connection closed: %v" , err )
47
- case err != nil :
48
- errs = append (errs , fmt .Errorf ("failed to decode sandbox container metrics for sandbox %q: %w" , sandbox .ID , err ))
49
- default :
50
- podSandboxStats .Stats = append (podSandboxStats .Stats , sandboxStats )
62
+ for _ , stat := range stats {
63
+ if stat != nil {
64
+ podSandboxStats .Stats = append (podSandboxStats .Stats , stat )
51
65
}
52
66
}
53
67
0 commit comments