|
| 1 | +package multi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/goccy/go-json" |
| 8 | + |
| 9 | + "github.com/mimuret/dtap/v2/pkg/plugin" |
| 10 | + "github.com/mimuret/dtap/v2/pkg/plugin/registry" |
| 11 | + "github.com/mimuret/dtap/v2/pkg/types" |
| 12 | + "github.com/pkg/errors" |
| 13 | +) |
| 14 | + |
| 15 | +func init() { |
| 16 | + _ = registry.RegisterOutputPlugin("multi", Setup) |
| 17 | +} |
| 18 | + |
| 19 | +func Setup(bs json.RawMessage) (types.OutputPlugin, error) { |
| 20 | + s := &MultiRunner{ |
| 21 | + Concurency: 1, |
| 22 | + } |
| 23 | + if err := json.Unmarshal(bs, s); err != nil { |
| 24 | + return nil, errors.Wrap(err, "failed to decode config") |
| 25 | + } |
| 26 | + if s.Concurency == 0 { |
| 27 | + return nil, errors.New("Concurency must be greater than 0") |
| 28 | + } |
| 29 | + var pluginName string |
| 30 | + if err := json.Unmarshal(s.Plugin["Name"], &pluginName); err != nil { |
| 31 | + return nil, errors.Wrap(err, "failed to unmarshal Plugin.Name") |
| 32 | + } |
| 33 | + if pluginName == "" { |
| 34 | + return nil, errors.New("`Plugin.Name` must not be empty") |
| 35 | + } |
| 36 | + for i := 0; i < int(s.Concurency); i++ { |
| 37 | + var err error |
| 38 | + s.Plugin["ID"], err = json.Marshal(fmt.Sprintf(`%s-%d`, s.GetID(), i)) |
| 39 | + if err != nil { |
| 40 | + return nil, errors.Wrapf(err, "failed to marshal Plugin.ID for concurrency %d", i) |
| 41 | + } |
| 42 | + cfg, err := json.Marshal(s.Plugin) |
| 43 | + if err != nil { |
| 44 | + return nil, errors.Wrapf(err, "failed to marshal plugin config: %s", pluginName) |
| 45 | + } |
| 46 | + p, err := registry.CreateOutputPlugin(pluginName, cfg) |
| 47 | + if err != nil { |
| 48 | + return nil, errors.Wrapf(err, "failed to create output plugin %s", s.PluginCommon.Name) |
| 49 | + } |
| 50 | + s.plugns = append(s.plugns, p) |
| 51 | + } |
| 52 | + return s, nil |
| 53 | +} |
| 54 | + |
| 55 | +// MultiRunner is a plugin that creates multiple instances of an Output Plugin |
| 56 | +// defined in the `Plugin` field. It is useful for load balancing or distributing |
| 57 | +// output to multiple destinations. |
| 58 | +// |
| 59 | +// The number of instances is controlled by the `Concurency` field. Each instance |
| 60 | +// of the plugin will have a unique ID, which is derived from the MultiRunner's ID |
| 61 | +// followed by a hyphen and a sequential number starting from 0. |
| 62 | +// |
| 63 | +// For example, the following configurations are equivalent: |
| 64 | +// |
| 65 | +// Configuration 1: |
| 66 | +// - Name: multi |
| 67 | +// ID: test-multi |
| 68 | +// Concurency: 2 |
| 69 | +// Plugin: |
| 70 | +// Name: dummy |
| 71 | +// |
| 72 | +// Configuration 2: |
| 73 | +// - Name: dummy |
| 74 | +// ID: test-multi-0 |
| 75 | +// - Name: dummy |
| 76 | +// ID: test-multi-1 |
| 77 | +// |
| 78 | +// Fields: |
| 79 | +// - `Concurency`: Specifies the number of concurrent plugin instances to create. |
| 80 | +// - `Plugin`: Defines the configuration for the child plugin to be instantiated. |
| 81 | +// |
| 82 | +// The `Start` method initializes all created plugins, and errors are returned if |
| 83 | +// any of the plugins fail to start. |
| 84 | + |
| 85 | +type MultiRunner struct { |
| 86 | + plugin.PluginCommon |
| 87 | + |
| 88 | + Concurency uint |
| 89 | + Plugin map[string]json.RawMessage |
| 90 | + |
| 91 | + plugns []types.OutputPlugin |
| 92 | +} |
| 93 | + |
| 94 | +var _ types.OutputPlugin = &MultiRunner{} |
| 95 | + |
| 96 | +func (m *MultiRunner) GetName() string { |
| 97 | + return m.PluginCommon.GetName() |
| 98 | +} |
| 99 | +func (m *MultiRunner) GetID() string { |
| 100 | + return m.PluginCommon.GetID() |
| 101 | +} |
| 102 | + |
| 103 | +func (m *MultiRunner) Start(ctx context.Context, oc *types.OutputContext) error { |
| 104 | + for _, p := range m.plugns { |
| 105 | + if err := p.Start(ctx, oc); err != nil { |
| 106 | + return errors.Wrapf(err, "failed to start output plugin %s", p.GetName()) |
| 107 | + } |
| 108 | + } |
| 109 | + return nil |
| 110 | +} |
0 commit comments