From da2d0341a4a47a4e7e40950acde3ff501943b79d Mon Sep 17 00:00:00 2001 From: Leonardo Cecchi Date: Wed, 11 Dec 2024 14:32:37 +0100 Subject: [PATCH] chore(refactor): isolate plugin loading function (#6312) Signed-off-by: Leonardo Cecchi --- internal/cnpi/plugin/client/client.go | 26 +++++++++---------- internal/cnpi/plugin/repository/connection.go | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/internal/cnpi/plugin/client/client.go b/internal/cnpi/plugin/client/client.go index e13515668c..100a0e027b 100644 --- a/internal/cnpi/plugin/client/client.go +++ b/internal/cnpi/plugin/client/client.go @@ -43,18 +43,6 @@ func (data *data) getPlugin(pluginName string) (connection.Interface, error) { return nil, ErrPluginNotLoaded } -func (data *data) load(ctx context.Context, names ...string) error { - for _, name := range names { - pluginData, err := data.repository.GetConnection(ctx, name) - if err != nil { - return err - } - - data.plugins = append(data.plugins, pluginData) - } - return nil -} - func (data *data) MetadataList() []connection.Metadata { result := make([]connection.Metadata, len(data.plugins)) for i := range data.plugins { @@ -85,13 +73,25 @@ func WithPlugins(ctx context.Context, repository repository.Interface, names ... repository: repository, } + load := func(names ...string) error { + for _, name := range names { + pluginData, err := result.repository.GetConnection(ctx, name) + if err != nil { + return err + } + + result.plugins = append(result.plugins, pluginData) + } + return nil + } + // The following ensures that each plugin is loaded just one // time, even when the same plugin has been requested multiple // times. loadingPlugins := stringset.From(names) uniqueSortedPluginName := loadingPlugins.ToSortedList() - if err := result.load(ctx, uniqueSortedPluginName...); err != nil { + if err := load(uniqueSortedPluginName...); err != nil { result.Close(ctx) return nil, err } diff --git a/internal/cnpi/plugin/repository/connection.go b/internal/cnpi/plugin/repository/connection.go index fb8e478c70..a71586f530 100644 --- a/internal/cnpi/plugin/repository/connection.go +++ b/internal/cnpi/plugin/repository/connection.go @@ -64,7 +64,7 @@ func (r *data) GetConnection(ctx context.Context, name string) (connection.Inter err = resource.Value().Ping(ctx) if err != nil { - contextLogger.Info( + contextLogger.Warning( "Detected stale/broken plugin connection, closing and trying again", "pluginName", name, "err", err)