@@ -26,6 +26,8 @@ type ContainerStore struct {
2626 filter ContainerFilter
2727}
2828
29+ const defaultTimeout = 10 * time .Second
30+
2931func NewContainerStore (ctx context.Context , client Client , filter ContainerFilter ) * ContainerStore {
3032 log .Debug ().Str ("host" , client .Host ().Name ).Interface ("filter" , filter ).Msg ("initializing container store" )
3133
@@ -64,7 +66,7 @@ func (s *ContainerStore) checkConnectivity() error {
6466 s .connected .Store (false )
6567 }()
6668
67- ctx , cancel := context .WithTimeout (context .Background (), 3 * time . Second ) // 3s is enough to fetch all containers
69+ ctx , cancel := context .WithTimeout (context .Background (), defaultTimeout )
6870 defer cancel ()
6971 if containers , err := s .client .ListContainers (ctx , s .filter ); err != nil {
7072 return err
@@ -88,7 +90,7 @@ func (s *ContainerStore) checkConnectivity() error {
8890 }
8991 go func (c Container , i int ) {
9092 defer sem .Release (1 )
91- ctx , cancel := context .WithTimeout (context .Background (), 2 * time . Second ) // 2s is hardcoded timeout for fetching container
93+ ctx , cancel := context .WithTimeout (context .Background (), defaultTimeout )
9294 defer cancel ()
9395 if container , err := s .client .FindContainer (ctx , c .ID ); err == nil {
9496 s .containers .Store (c .ID , & container )
@@ -114,22 +116,29 @@ func (s *ContainerStore) ListContainers(filter ContainerFilter) ([]Container, er
114116 return nil , err
115117 }
116118
117- validContainers , err := s .client .ListContainers (s .ctx , filter )
118- if err != nil {
119- return nil , err
120- }
119+ containers := make ([]Container , 0 )
120+ if filter .Exists () {
121+ validContainers , err := s .client .ListContainers (s .ctx , filter )
122+ if err != nil {
123+ return nil , err
124+ }
121125
122- validIDMap := lo .KeyBy (validContainers , func (item Container ) string {
123- return item .ID
124- })
126+ validIDMap := lo .KeyBy (validContainers , func (item Container ) string {
127+ return item .ID
128+ })
125129
126- containers := make ([]Container , 0 )
127- s .containers .Range (func (_ string , c * Container ) bool {
128- if _ , ok := validIDMap [c .ID ]; ok {
130+ s .containers .Range (func (_ string , c * Container ) bool {
131+ if _ , ok := validIDMap [c .ID ]; ok {
132+ containers = append (containers , * c )
133+ }
134+ return true
135+ })
136+ } else {
137+ s .containers .Range (func (_ string , c * Container ) bool {
129138 containers = append (containers , * c )
130- }
131- return true
132- })
139+ return true
140+ })
141+ }
133142
134143 return containers , nil
135144}
@@ -158,7 +167,7 @@ func (s *ContainerStore) FindContainer(id string, filter ContainerFilter) (Conta
158167 log .Debug ().Str ("id" , id ).Msg ("container doesn't have detailed information, fetching it" )
159168 if newContainer , ok := s .containers .Compute (id , func (c * Container , loaded bool ) (* Container , bool ) {
160169 if loaded {
161- ctx , cancel := context .WithTimeout (context .Background (), 3 * time . Second )
170+ ctx , cancel := context .WithTimeout (context .Background (), defaultTimeout )
162171 defer cancel ()
163172 if newContainer , err := s .client .FindContainer (ctx , id ); err == nil {
164173 return & newContainer , false
@@ -237,7 +246,7 @@ func (s *ContainerStore) init() {
237246 log .Trace ().Str ("event" , event .Name ).Str ("id" , event .ActorID ).Msg ("received container event" )
238247 switch event .Name {
239248 case "start" :
240- ctx , cancel := context .WithTimeout (context .Background (), 3 * time . Second )
249+ ctx , cancel := context .WithTimeout (context .Background (), defaultTimeout )
241250
242251 if container , err := s .client .FindContainer (ctx , event .ActorID ); err == nil {
243252 list , _ := s .client .ListContainers (ctx , s .filter )
0 commit comments