@@ -2,6 +2,7 @@ package docker
2
2
3
3
import (
4
4
"context"
5
+ "sync"
5
6
6
7
"github.com/puzpuzpuz/xsync/v3"
7
8
log "github.com/sirupsen/logrus"
@@ -12,6 +13,7 @@ type ContainerStore struct {
12
13
subscribers * xsync.MapOf [context.Context , chan ContainerEvent ]
13
14
client Client
14
15
statsCollector * StatsCollector
16
+ wg sync.WaitGroup
15
17
}
16
18
17
19
func NewContainerStore (ctx context.Context , client Client ) * ContainerStore {
@@ -20,17 +22,10 @@ func NewContainerStore(ctx context.Context, client Client) *ContainerStore {
20
22
client : client ,
21
23
subscribers : xsync .NewMapOf [context.Context , chan ContainerEvent ](),
22
24
statsCollector : NewStatsCollector (client ),
25
+ wg : sync.WaitGroup {},
23
26
}
24
27
25
- containers , err := s .client .ListContainers ()
26
- if err != nil {
27
- log .Fatalf ("error while listing containers: %v" , err )
28
- }
29
-
30
- for _ , c := range containers {
31
- c := c // create a new variable to avoid capturing the loop variable
32
- s .containers .Store (c .ID , & c )
33
- }
28
+ s .wg .Add (1 )
34
29
35
30
go s .init (ctx )
36
31
go s .statsCollector .StartCollecting (ctx )
@@ -39,6 +34,7 @@ func NewContainerStore(ctx context.Context, client Client) *ContainerStore {
39
34
}
40
35
41
36
func (s * ContainerStore ) List () []Container {
37
+ s .wg .Wait ()
42
38
containers := make ([]Container , 0 )
43
39
s .containers .Range (func (_ string , c * Container ) bool {
44
40
containers = append (containers , * c )
@@ -70,10 +66,14 @@ func (s *ContainerStore) init(ctx context.Context) {
70
66
if containers , err := s .client .ListContainers (); err == nil {
71
67
for _ , c := range containers {
72
68
c := c // create a new variable to avoid capturing the loop variable
73
- s .containers .LoadOrStore (c .ID , & c )
69
+ s .containers .Store (c .ID , & c )
74
70
}
71
+ } else {
72
+ log .Fatalf ("error listing containers: %v" , err )
75
73
}
76
74
75
+ s .wg .Done ()
76
+
77
77
for {
78
78
select {
79
79
case event := <- events :
0 commit comments