Skip to content

Commit 59ae99b

Browse files
Klaus Wagnerpoiana
authored andcommitted
Optimize pull request - avoid unnecessary go routines and move constant definition
Signed-off-by: Klaus Wagner <Klaus.Wagner@erstegroup.com>
1 parent 4a03991 commit 59ae99b

File tree

6 files changed

+23
-22
lines changed

6 files changed

+23
-22
lines changed

plugins/container/go-worker/pkg/container/cri.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,6 @@ func (c *criEngine) List(ctx context.Context) ([]event.Event, error) {
450450
func (c *criEngine) Listen(ctx context.Context, wg *sync.WaitGroup) (<-chan event.Event, error) {
451451
containerEventsCh := make(chan *v1.ContainerEventResponse)
452452
containerEventsErrorCh := make(chan error)
453-
const containerEventsErrorTimeout = 10 * time.Millisecond
454453
wg.Add(1)
455454
go func() {
456455
defer close(containerEventsCh)
@@ -464,14 +463,7 @@ func (c *criEngine) Listen(ctx context.Context, wg *sync.WaitGroup) (<-chan even
464463
case err := <-containerEventsErrorCh:
465464
return nil, err
466465
case <-time.After(containerEventsErrorTimeout):
467-
// continue reading of error channel to avoid blocking initial go-routine
468-
go func() {
469-
for {
470-
if _, ok := <-containerEventsErrorCh; !ok {
471-
break
472-
}
473-
}
474-
}()
466+
break
475467
}
476468

477469
outCh := make(chan event.Event)
@@ -483,6 +475,11 @@ func (c *criEngine) Listen(ctx context.Context, wg *sync.WaitGroup) (<-chan even
483475
select {
484476
case <-ctx.Done():
485477
return
478+
case _, ok := <- containerEventsErrorCh:
479+
if !ok {
480+
// containerEventsErrorCh has been closed - block further reads from channel
481+
containerEventsErrorCh = nil
482+
}
486483
case evt, ok := <-containerEventsCh:
487484
if !ok {
488485
// containerEventsCh has been closed - block further reads from channel

plugins/container/go-worker/pkg/container/engine.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const (
2828
typeCri engineType = "cri"
2929
typeCrio engineType = "cri-o"
3030
typeContainerd engineType = "containerd"
31+
32+
containerEventsErrorTimeout = 10 * time.Millisecond
3133
)
3234

3335
type engineType string

plugins/container/go-worker/pkg/container/fetcher.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func (f *fetcher) List(_ context.Context) ([]event.Event, error) {
6767
// a retry is set up every containerFetchRetryInterval until containerFetchRetryTimeout is reached.
6868
// On success, publish event on output channel.
6969
func (f *fetcher) Listen(ctx context.Context, wg *sync.WaitGroup) (<-chan event.Event, error) {
70-
const containerFetchRetryInterval = 10 * time.Millisecond
71-
const containerFetchRetryTimeout = time.Second
70+
const containerFetchRetryInterval = 30 * time.Millisecond
71+
const containerFetchRetryTimeout = 150 * time.Millisecond
7272
outCh := make(chan event.Event)
7373
wg.Add(1)
7474
go func() {

plugins/container/go-worker/pkg/container/podman.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,11 @@ func (pc *podmanEngine) Listen(ctx context.Context, wg *sync.WaitGroup) (<-chan
272272

273273
evChn := make(chan types.Event)
274274
evErrorChn := make(chan error)
275-
const eventsErrorTimeout = 10 * time.Millisecond
276275
cancelChan := make(chan bool)
277276
wg.Add(1)
278277
// producers
279278
go func(ch chan types.Event) {
279+
defer close(evErrorChn)
280280
defer wg.Done()
281281
evErrorChn <- system.Events(pc.pCtx, ch, cancelChan, &system.EventsOptions{
282282
Filters: filters,
@@ -288,15 +288,8 @@ func (pc *podmanEngine) Listen(ctx context.Context, wg *sync.WaitGroup) (<-chan
288288
select {
289289
case err := <-evErrorChn:
290290
return nil, err
291-
case <-time.After(eventsErrorTimeout):
292-
// continue reading of error channel to avoid blocking initial go-routine
293-
go func() {
294-
for {
295-
if _, ok := <-evErrorChn; !ok {
296-
break
297-
}
298-
}
299-
}()
291+
case <-time.After(containerEventsErrorTimeout):
292+
break
300293
}
301294

302295
outCh := make(chan event.Event)
@@ -312,6 +305,11 @@ func (pc *podmanEngine) Listen(ctx context.Context, wg *sync.WaitGroup) (<-chan
312305
select {
313306
case <-ctx.Done():
314307
return
308+
case _, ok := <-evErrorChn:
309+
if !ok {
310+
// evErrorChn has been closed - block further reads from channel
311+
evErrorChn = nil
312+
}
315313
case ev, ok := <-evChn:
316314
var (
317315
ctr *define.InspectContainerData

plugins/container/go-worker/worker_api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,7 @@ func AskForContainerInfo(pCtx unsafe.Pointer, containerId *C.cchar_t) bool {
132132
return false
133133
}
134134
}
135+
// In case the fetch channel is nil a retry from falco
136+
// does not make sense, report the containerId as handled
135137
return true
136138
}

plugins/container/src/plugin.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,9 @@ void my_plugin::on_new_process(const falcosecurity::table_entry& thread_entry,
386386
if(AskForContainerInfo(m_async_ctx, container_id.c_str()))
387387
{
388388
m_asked_containers.insert(container_id);
389-
} else {
389+
}
390+
else
391+
{
390392
m_logger.log(
391393
fmt::format("failed to ask the plugin to fetch "
392394
"info for "

0 commit comments

Comments
 (0)