1
0
mirror of https://github.com/amir20/dozzle.git synced 2025-12-21 13:23:07 +01:00

chore: uses wg.Go where possible (#4074)

This commit is contained in:
Amir Raminfar
2025-08-15 08:29:25 -07:00
committed by GitHub
parent af93918f8c
commit 7be0515648
5 changed files with 27 additions and 47 deletions

View File

@@ -290,10 +290,8 @@ func (s *server) ContainerExec(stream pb.AgentService_ContainerExecServer) error
} }
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(2)
go func() { wg.Go(func() {
defer wg.Done()
defer cancel() defer cancel()
defer containerWriter.Close() defer containerWriter.Close()
for { for {
@@ -306,10 +304,9 @@ func (s *server) ContainerExec(stream pb.AgentService_ContainerExecServer) error
return return
} }
} }
}() })
go func() { wg.Go(func() {
defer wg.Done()
defer cancel() defer cancel()
buffer := make([]byte, 1024) buffer := make([]byte, 1024)
for { for {
@@ -322,7 +319,7 @@ func (s *server) ContainerExec(stream pb.AgentService_ContainerExecServer) error
return return
} }
} }
}() })
wg.Wait() wg.Wait()

View File

@@ -238,12 +238,10 @@ func (k *K8sClient) ContainerEvents(ctx context.Context, ch chan<- container.Con
} }
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(len(watchers))
for _, watcher := range watchers { for _, watcher := range watchers {
go func(w watch.Interface) { wg.Go(func() {
defer wg.Done() for event := range watcher.ResultChan() {
for event := range w.ResultChan() {
log.Debug().Interface("event.type", event.Type).Msg("Received kubernetes event") log.Debug().Interface("event.type", event.Type).Msg("Received kubernetes event")
pod, ok := event.Object.(*corev1.Pod) pod, ok := event.Object.(*corev1.Pod)
if !ok { if !ok {
@@ -270,7 +268,7 @@ func (k *K8sClient) ContainerEvents(ctx context.Context, ch chan<- container.Con
} }
} }
} }
}(watcher) })
} }
wg.Wait() wg.Wait()

View File

@@ -87,24 +87,21 @@ func (a *agentService) Exec(ctx context.Context, container container.Container,
} }
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(2)
go func() { wg.Go(func() {
defer wg.Done()
if _, err := io.Copy(containerWriter, stdin); err != nil { if _, err := io.Copy(containerWriter, stdin); err != nil {
log.Error().Err(err).Msg("error while reading from ws using agent") log.Error().Err(err).Msg("error while reading from ws using agent")
} }
cancel() cancel()
containerWriter.Close() containerWriter.Close()
}() })
go func() { wg.Go(func() {
defer wg.Done()
if _, err := stdcopy.StdCopy(stdout, stdout, containerReader); err != nil { if _, err := stdcopy.StdCopy(stdout, stdout, containerReader); err != nil {
log.Error().Err(err).Msg("error while writing to ws using agent") log.Error().Err(err).Msg("error while writing to ws using agent")
} }
cancel() cancel()
}() })
wg.Wait() wg.Wait()

View File

@@ -120,19 +120,17 @@ func (d *DockerClientService) Attach(ctx context.Context, container container.Co
} }
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(2)
go func() { wg.Go(func() {
defer wg.Done() defer wg.Done()
if _, err := io.Copy(containerWriter, stdin); err != nil { if _, err := io.Copy(containerWriter, stdin); err != nil {
log.Error().Err(err).Msg("error while reading from ws") log.Error().Err(err).Msg("error while reading from ws")
} }
cancel() cancel()
containerWriter.Close() containerWriter.Close()
}() })
go func() { wg.Go(func() {
defer wg.Done()
if container.Tty { if container.Tty {
if _, err := io.Copy(stdout, containerReader); err != nil { if _, err := io.Copy(stdout, containerReader); err != nil {
log.Error().Err(err).Msg("error while writing to ws") log.Error().Err(err).Msg("error while writing to ws")
@@ -143,7 +141,7 @@ func (d *DockerClientService) Attach(ctx context.Context, container container.Co
} }
} }
cancel() cancel()
}() })
wg.Wait() wg.Wait()
@@ -159,24 +157,20 @@ func (d *DockerClientService) Exec(ctx context.Context, container container.Cont
} }
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(2) wg.Go(func() {
go func() {
defer wg.Done()
if _, err := io.Copy(containerWriter, stdin); err != nil { if _, err := io.Copy(containerWriter, stdin); err != nil {
log.Error().Err(err).Msg("error while reading from ws") log.Error().Err(err).Msg("error while reading from ws")
} }
cancel() cancel()
containerWriter.Close() containerWriter.Close()
}() })
go func() { wg.Go(func() {
defer wg.Done()
if _, err := stdcopy.StdCopy(stdout, stdout, containerReader); err != nil { if _, err := stdcopy.StdCopy(stdout, stdout, containerReader); err != nil {
log.Error().Err(err).Msg("error while writing to ws") log.Error().Err(err).Msg("error while writing to ws")
} }
cancel() cancel()
}() })
wg.Wait() wg.Wait()

View File

@@ -101,24 +101,21 @@ func (k *K8sClientService) Attach(ctx context.Context, container container.Conta
} }
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(2)
go func() { wg.Go(func() {
defer writer.Close() defer writer.Close()
defer cancel() defer cancel()
defer wg.Done()
if _, err := io.Copy(writer, stdin); err != nil { if _, err := io.Copy(writer, stdin); err != nil {
log.Error().Err(err).Msg("error copying stdin") log.Error().Err(err).Msg("error copying stdin")
} }
}() })
go func() { wg.Go(func() {
defer cancel() defer cancel()
defer wg.Done()
if _, err := io.Copy(stdout, reader); err != nil { if _, err := io.Copy(stdout, reader); err != nil {
log.Error().Err(err).Msg("error copying stdout") log.Error().Err(err).Msg("error copying stdout")
} }
}() })
wg.Wait() wg.Wait()
return nil return nil
@@ -133,24 +130,21 @@ func (k *K8sClientService) Exec(ctx context.Context, container container.Contain
} }
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(2)
go func() { wg.Go(func() {
defer writer.Close() defer writer.Close()
defer cancel() defer cancel()
defer wg.Done()
if _, err := io.Copy(writer, stdin); err != nil { if _, err := io.Copy(writer, stdin); err != nil {
log.Error().Err(err).Msg("error copying stdin") log.Error().Err(err).Msg("error copying stdin")
} }
}() })
go func() { wg.Go(func() {
defer cancel() defer cancel()
defer wg.Done()
if _, err := io.Copy(stdout, reader); err != nil { if _, err := io.Copy(stdout, reader); err != nil {
log.Error().Err(err).Msg("error copying stdout") log.Error().Err(err).Msg("error copying stdout")
} }
}() })
wg.Wait() wg.Wait()
return nil return nil