Skip to content

Commit eba87fa

Browse files
authored
fix: updates default timeout and adds user agent. see #3610 (#3611)
1 parent 7ae4099 commit eba87fa

File tree

5 files changed

+31
-21
lines changed

5 files changed

+31
-21
lines changed

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ services:
3030
- DOZZLE_FILTER=name=dozzle
3131
- DOZZLE_NO_ANALYTICS=1
3232
- DOZZLE_HOSTNAME=localhost
33+
- DOZZLE_LEVEL=debug
3334
ports:
3435
- 7070:8080
3536
build:

internal/container/container_store.go

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type ContainerStore struct {
2626
filter ContainerFilter
2727
}
2828

29+
const defaultTimeout = 10 * time.Second
30+
2931
func 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)

internal/container/stats_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (sc *StatsCollector) Start(parentCtx context.Context) bool {
9999
ctx, sc.stopper = context.WithCancel(parentCtx)
100100
sc.mu.Unlock()
101101

102-
timeoutCtx, cancel := context.WithTimeout(parentCtx, 3*time.Second) // 3 seconds to list containers is hard limit
102+
timeoutCtx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
103103
if containers, err := sc.client.ListContainers(timeoutCtx, sc.filter); err == nil {
104104
for _, c := range containers {
105105
if c.State == "running" {

internal/docker/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func NewClient(cli DockerCLI, host container.Host) container.Client {
6969

7070
// NewClientWithFilters creates a new instance of Client with docker filters
7171
func NewLocalClient(hostname string) (container.Client, error) {
72-
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
72+
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation(), client.WithUserAgent("Docker-Client/Dozzle"))
7373

7474
if err != nil {
7575
return nil, err
@@ -109,7 +109,7 @@ func NewRemoteClient(host container.Host) (container.Client, error) {
109109
log.Debug().Msg("Not using TLS for remote client")
110110
}
111111

112-
opts = append(opts, client.WithAPIVersionNegotiation())
112+
opts = append(opts, client.WithAPIVersionNegotiation(), client.WithUserAgent("Docker-Client/Dozzle"))
113113

114114
cli, err := client.NewClientWithOpts(opts...)
115115

internal/support/cli/args.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type Args struct {
2727
RemoteAgent []string `arg:"env:DOZZLE_REMOTE_AGENT,--remote-agent,separate" help:"list of agents to connect remotely"`
2828
NoAnalytics bool `arg:"--no-analytics,env:DOZZLE_NO_ANALYTICS" help:"disables anonymous analytics"`
2929
Mode string `arg:"env:DOZZLE_MODE" default:"server" help:"sets the mode to run in (server, swarm)"`
30-
TimeoutString string `arg:"--timeout,env:DOZZLE_TIMEOUT" default:"3s" help:"sets the timeout for docker client"`
30+
TimeoutString string `arg:"--timeout,env:DOZZLE_TIMEOUT" default:"10s" help:"sets the timeout for docker client"`
3131
Timeout time.Duration `arg:"-"`
3232
Healthcheck *HealthcheckCmd `arg:"subcommand:healthcheck" help:"checks if the server is running"`
3333
Generate *GenerateCmd `arg:"subcommand:generate" help:"generates a configuration file for simple auth"`

0 commit comments

Comments
 (0)