Skip to content

Commit

Permalink
chore(refactor): isolate plugin loading function (cloudnative-pg#6312)
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Cecchi <[email protected]>
  • Loading branch information
leonardoce authored Dec 11, 2024
1 parent bd6e545 commit da2d034
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions internal/cnpi/plugin/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cnpi/plugin/repository/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit da2d034

Please sign in to comment.