From d682ea87e78cf6db125e73821b87165f4889c249 Mon Sep 17 00:00:00 2001 From: Elia Renzoni Date: Sun, 5 Jul 2026 10:36:33 +0200 Subject: [PATCH 1/2] refactor(alb): avoid options in ALB constructors Signed-off-by: Elia Renzoni --- pkg/backends/alb/alb_502_test.go | 10 +++---- pkg/backends/alb/mech/fr/first_response.go | 14 +++++----- .../alb/mech/nlm/newest_last_modified.go | 6 ++--- pkg/backends/alb/mech/registry/registry.go | 3 ++- .../registry/registry_consistency_test.go | 4 +-- pkg/backends/alb/mech/rr/round_robin.go | 2 +- .../alb/mech/tsm/time_series_merge.go | 14 +++++----- .../mech/tsm/time_series_merge_ctor_test.go | 6 ++--- pkg/backends/alb/mech/types/types.go | 2 +- .../alb/mech/types/types_interfaces_test.go | 6 ++--- pkg/backends/alb/mech/ur/user_router.go | 8 +++--- pkg/backends/alb/options/options.go | 27 +++++++++++++++++++ 12 files changed, 65 insertions(+), 37 deletions(-) diff --git a/pkg/backends/alb/alb_502_test.go b/pkg/backends/alb/alb_502_test.go index 2b32fe5be..b0d9a1ba6 100644 --- a/pkg/backends/alb/alb_502_test.go +++ b/pkg/backends/alb/alb_502_test.go @@ -22,8 +22,8 @@ import ( "testing" "github.com/stretchr/testify/require" - mtypes "github.com/trickstercache/trickster/v2/pkg/backends/alb/mech/types" "github.com/trickstercache/trickster/v2/pkg/backends/alb/mech/tsm" + mtypes "github.com/trickstercache/trickster/v2/pkg/backends/alb/mech/types" "github.com/trickstercache/trickster/v2/pkg/backends/alb/names" ao "github.com/trickstercache/trickster/v2/pkg/backends/alb/options" "github.com/trickstercache/trickster/v2/pkg/backends/prometheus" @@ -108,12 +108,12 @@ func TestALB502WithMultiplePrometheusBackends(t *testing.T) { pool1, _, _ := albpool.NewHealthy([]http.Handler{handler1}) albpool.WaitHealthy(t, pool1, 1) - albOpts := &ao.Options{ + albConfigs := &ao.ALBConfigs{ MechanismName: names.MechanismTSM, OutputFormat: providers.Prometheus, } - tsmMech, err := tsm.New(albOpts, types.Lookup{providers.Prometheus: prometheus.NewClient}) + tsmMech, err := tsm.New(albConfigs, types.Lookup{providers.Prometheus: prometheus.NewClient}) require.NoError(t, err) tsmPM := tsmMech.(mtypes.PoolMechanism) tsmPM.SetPool(pool1) @@ -135,12 +135,12 @@ func TestALB502WithMultiplePrometheusBackends(t *testing.T) { pool2, _, _ := albpool.NewHealthy([]http.Handler{handler1, handler2}) albpool.WaitHealthy(t, pool2, 2) - albOpts := &ao.Options{ + albConfigs := &ao.ALBConfigs{ MechanismName: names.MechanismTSM, OutputFormat: providers.Prometheus, } - tsmMech, err := tsm.New(albOpts, types.Lookup{providers.Prometheus: prometheus.NewClient}) + tsmMech, err := tsm.New(albConfigs, types.Lookup{providers.Prometheus: prometheus.NewClient}) require.NoError(t, err) tsmPM := tsmMech.(mtypes.PoolMechanism) tsmPM.SetPool(pool2) diff --git a/pkg/backends/alb/mech/fr/first_response.go b/pkg/backends/alb/mech/fr/first_response.go index 40dafd54a..df5895345 100644 --- a/pkg/backends/alb/mech/fr/first_response.go +++ b/pkg/backends/alb/mech/fr/first_response.go @@ -53,19 +53,19 @@ func RegistryEntryFGR() types.RegistryEntry { return types.RegistryEntry{Name: FGRName, ShortName: names.MechanismFGR, New: NewFGR} } -func NewFGR(o *options.Options, _ rt.Lookup) (types.Mechanism, error) { +func NewFGR(conf *options.ALBConfigs, _ rt.Lookup) (types.Mechanism, error) { return &handler{ fgr: true, - fgrCodes: o.FgrCodesLookup, - options: o.FGROptions, - maxCaptureBytes: o.MaxCaptureBytes, + fgrCodes: conf.FgrCodesLookup, + options: conf.FirstGoodResponseOptions, + maxCaptureBytes: conf.MaxCaptureBytes, }, nil } -func New(o *options.Options, _ rt.Lookup) (types.Mechanism, error) { +func New(conf *options.ALBConfigs, _ rt.Lookup) (types.Mechanism, error) { h := &handler{} - if o != nil { - h.maxCaptureBytes = o.MaxCaptureBytes + if conf != nil { + h.maxCaptureBytes = conf.MaxCaptureBytes } return h, nil } diff --git a/pkg/backends/alb/mech/nlm/newest_last_modified.go b/pkg/backends/alb/mech/nlm/newest_last_modified.go index 66d98799f..467c8e1aa 100644 --- a/pkg/backends/alb/mech/nlm/newest_last_modified.go +++ b/pkg/backends/alb/mech/nlm/newest_last_modified.go @@ -44,10 +44,10 @@ func RegistryEntry() types.RegistryEntry { return types.RegistryEntry{Name: Name, ShortName: names.MechanismNLM, New: New} } -func New(o *options.Options, _ rt.Lookup) (types.Mechanism, error) { +func New(conf *options.ALBConfigs, _ rt.Lookup) (types.Mechanism, error) { return &handler{ - options: o.NLMOptions, - maxCaptureBytes: o.MaxCaptureBytes, + options: conf.NewestLastModifiedOptions, + maxCaptureBytes: conf.MaxCaptureBytes, }, nil } diff --git a/pkg/backends/alb/mech/registry/registry.go b/pkg/backends/alb/mech/registry/registry.go index e62d1656f..dec5f574a 100644 --- a/pkg/backends/alb/mech/registry/registry.go +++ b/pkg/backends/alb/mech/registry/registry.go @@ -62,7 +62,8 @@ func New(name types.Name, opts *options.Options, factories rt.Lookup, ) (types.Mechanism, error) { if f, ok := registryByName[name]; ok && f != nil { - return f(opts, factories) + configs := options.NewALBConfigsFromOptions(opts) + return f(configs, factories) } return nil, errors.ErrUnsupportedMechanism } diff --git a/pkg/backends/alb/mech/registry/registry_consistency_test.go b/pkg/backends/alb/mech/registry/registry_consistency_test.go index 8e2e23df4..2cdeb21da 100644 --- a/pkg/backends/alb/mech/registry/registry_consistency_test.go +++ b/pkg/backends/alb/mech/registry/registry_consistency_test.go @@ -27,7 +27,7 @@ import ( func TestCompileSupportedByNamePanicsOnDuplicate(t *testing.T) { t.Parallel() - noop := func(_ *options.Options, _ rt.Lookup) (types.Mechanism, error) { + noop := func(_ *options.ALBConfigs, _ rt.Lookup) (types.Mechanism, error) { return nil, nil } entries := []types.RegistryEntry{ @@ -49,7 +49,7 @@ func TestCompileSupportedByNamePanicsOnDuplicate(t *testing.T) { func TestCompileSupportedByNamePanicsOnNameShortNameCollision(t *testing.T) { t.Parallel() - noop := func(_ *options.Options, _ rt.Lookup) (types.Mechanism, error) { + noop := func(_ *options.ALBConfigs, _ rt.Lookup) (types.Mechanism, error) { return nil, nil } entries := []types.RegistryEntry{ diff --git a/pkg/backends/alb/mech/rr/round_robin.go b/pkg/backends/alb/mech/rr/round_robin.go index dd1b273c0..d1c1943ed 100644 --- a/pkg/backends/alb/mech/rr/round_robin.go +++ b/pkg/backends/alb/mech/rr/round_robin.go @@ -40,7 +40,7 @@ func RegistryEntry() types.RegistryEntry { return types.RegistryEntry{Name: Name, ShortName: names.MechanismRR, New: New} } -func New(_ *options.Options, _ rt.Lookup) (types.Mechanism, error) { +func New(_ *options.ALBConfigs, _ rt.Lookup) (types.Mechanism, error) { return &handler{}, nil } diff --git a/pkg/backends/alb/mech/tsm/time_series_merge.go b/pkg/backends/alb/mech/tsm/time_series_merge.go index e4b02c694..2e9784327 100644 --- a/pkg/backends/alb/mech/tsm/time_series_merge.go +++ b/pkg/backends/alb/mech/tsm/time_series_merge.go @@ -99,20 +99,20 @@ func RegistryEntry() types.RegistryEntry { return types.RegistryEntry{Name: Name, ShortName: ShortName, New: New} } -func New(o *options.Options, factories rt.Lookup) (types.Mechanism, error) { +func New(conf *options.ALBConfigs, factories rt.Lookup) (types.Mechanism, error) { out := &handler{ - tsmOptions: o.TSMOptions, - maxCaptureBytes: o.MaxCaptureBytes, - maxFanoutCaptureBytes: o.MaxFanoutCaptureBytes, + tsmOptions: conf.TimeSeriesMergeOptions, + maxCaptureBytes: conf.MaxCaptureBytes, + maxFanoutCaptureBytes: conf.MaxFanoutCaptureBytes, } // this validates the merge configuration for the ALB client as it sets it up // First, verify the output format is a support merge provider - if !providers.IsSupportedTimeSeriesMergeProvider(o.OutputFormat) { + if !providers.IsSupportedTimeSeriesMergeProvider(conf.OutputFormat) { return nil, errors.ErrInvalidTimeSeriesMergeProvider } // next, get the factory function required to create a backend handler for the supplied format - f, ok := factories[o.OutputFormat] + f, ok := factories[conf.OutputFormat] if !ok { return nil, errors.ErrInvalidTimeSeriesMergeProvider } @@ -128,7 +128,7 @@ func New(o *options.Options, factories rt.Lookup) (types.Mechanism, error) { } // set the merge paths in the ALB client out.mergePaths = mc2.MergeablePaths() - out.outputFormat = o.OutputFormat + out.outputFormat = conf.OutputFormat return out, nil } diff --git a/pkg/backends/alb/mech/tsm/time_series_merge_ctor_test.go b/pkg/backends/alb/mech/tsm/time_series_merge_ctor_test.go index 56787eece..e553706f4 100644 --- a/pkg/backends/alb/mech/tsm/time_series_merge_ctor_test.go +++ b/pkg/backends/alb/mech/tsm/time_series_merge_ctor_test.go @@ -45,7 +45,7 @@ func TestNew(t *testing.T) { t.Run("invalid output format", func(t *testing.T) { t.Parallel() - _, err := New(&options.Options{OutputFormat: "not-a-provider"}, nil) + _, err := New(&options.ALBConfigs{OutputFormat: "not-a-provider"}, nil) if !errors.Is(err, alberr.ErrInvalidTimeSeriesMergeProvider) { t.Fatalf("New() error = %v, want ErrInvalidTimeSeriesMergeProvider", err) } @@ -53,7 +53,7 @@ func TestNew(t *testing.T) { t.Run("missing factory", func(t *testing.T) { t.Parallel() - _, err := New(&options.Options{OutputFormat: providers.Prometheus}, rt.Lookup{}) + _, err := New(&options.ALBConfigs{OutputFormat: providers.Prometheus}, rt.Lookup{}) if !errors.Is(err, alberr.ErrInvalidTimeSeriesMergeProvider) { t.Fatalf("New() error = %v, want ErrInvalidTimeSeriesMergeProvider", err) } @@ -62,7 +62,7 @@ func TestNew(t *testing.T) { t.Run("valid prometheus provider", func(t *testing.T) { t.Parallel() m, err := New( - &options.Options{OutputFormat: providers.Prometheus}, + &options.ALBConfigs{OutputFormat: providers.Prometheus}, rt.Lookup{providers.Prometheus: prometheus.NewClient}, ) if err != nil { diff --git a/pkg/backends/alb/mech/types/types.go b/pkg/backends/alb/mech/types/types.go index 0191d4b7a..d6646b1da 100644 --- a/pkg/backends/alb/mech/types/types.go +++ b/pkg/backends/alb/mech/types/types.go @@ -29,7 +29,7 @@ type Name = string // NewMechanismFunc defines a function that returns a new Mechanism from the // provided Options -type NewMechanismFunc func(*options.Options, types.Lookup) (Mechanism, error) +type NewMechanismFunc func(*options.ALBConfigs, types.Lookup) (Mechanism, error) // Mechanism represents a specific ALB Implementation (e.g., a Round Robiner). // Pool-aware mechanisms additionally implement PoolMechanism; callers that diff --git a/pkg/backends/alb/mech/types/types_interfaces_test.go b/pkg/backends/alb/mech/types/types_interfaces_test.go index 73f8f7d9e..881b1b6ad 100644 --- a/pkg/backends/alb/mech/types/types_interfaces_test.go +++ b/pkg/backends/alb/mech/types/types_interfaces_test.go @@ -57,21 +57,21 @@ func TestPoolMechanismMembership(t *testing.T) { return m }, true}, {"nlm", func(t *testing.T) types.Mechanism { - m, err := nlm.New(&options.Options{}, nil) + m, err := nlm.New(&options.ALBConfigs{}, nil) if err != nil { t.Fatalf("nlm.New: %v", err) } return m }, true}, {"ur", func(t *testing.T) types.Mechanism { - m, err := ur.New(&options.Options{UserRouter: &uropt.Options{}}, nil) + m, err := ur.New(&options.ALBConfigs{UserRouter: &uropt.Options{}}, nil) if err != nil { t.Fatalf("ur.New: %v", err) } return m }, false}, {"tsm", func(t *testing.T) types.Mechanism { - o := &options.Options{OutputFormat: providers.Prometheus} + o := &options.ALBConfigs{OutputFormat: providers.Prometheus} factories := rt.Lookup{providers.Prometheus: prometheus.NewClient} m, err := tsm.New(o, factories) if err != nil { diff --git a/pkg/backends/alb/mech/ur/user_router.go b/pkg/backends/alb/mech/ur/user_router.go index 66e2418ca..21dbf92cd 100644 --- a/pkg/backends/alb/mech/ur/user_router.go +++ b/pkg/backends/alb/mech/ur/user_router.go @@ -69,13 +69,13 @@ func RegistryEntry() types.RegistryEntry { } } -func New(o *options.Options, _ rt.Lookup) (types.Mechanism, error) { - if o == nil || o.UserRouter == nil { +func New(conf *options.ALBConfigs, _ rt.Lookup) (types.Mechanism, error) { + if conf == nil || conf.UserRouter == nil { return nil, errors.ErrInvalidOptions } out := &Handler{ - noRouteStatusCode: o.UserRouter.NoRouteStatusCode, - options: o.UserRouter, + noRouteStatusCode: conf.UserRouter.NoRouteStatusCode, + options: conf.UserRouter, } return out, nil } diff --git a/pkg/backends/alb/options/options.go b/pkg/backends/alb/options/options.go index 7551fe1b5..0c30b9e98 100644 --- a/pkg/backends/alb/options/options.go +++ b/pkg/backends/alb/options/options.go @@ -141,6 +141,33 @@ var ( ErrOutputFormatOnlyForTSM = errors.New("'output_format' option is only valid for provider 'alb' and mechanism 'tsmerge'") ) +type ALBConfigs struct { + TimeSeriesMergeOptions + NewestLastModifiedOptions + FirstGoodResponseOptions + UserRouter *ur.Options + + MechanismName string + OutputFormat string + MaxCaptureBytes int + MaxFanoutCaptureBytes int + FgrCodesLookup sets.Set[int] +} + +func NewALBConfigsFromOptions(o *Options) *ALBConfigs { + return &ALBConfigs{ + TimeSeriesMergeOptions: o.TSMOptions, + NewestLastModifiedOptions: o.NLMOptions, + FirstGoodResponseOptions: o.FGROptions, + UserRouter: o.UserRouter, + MechanismName: o.MechanismName, + OutputFormat: o.OutputFormat, + MaxCaptureBytes: o.MaxCaptureBytes, + MaxFanoutCaptureBytes: o.MaxFanoutCaptureBytes, + FgrCodesLookup: o.FgrCodesLookup, + } +} + // NewErrInvalidALBOptions returns an invalid ALB Options error func NewErrInvalidALBOptions(backendName string) error { return &InvalidALBOptionsError{ From 60cb0ced283c4e0a5b409ec6bd085bf882885b0f Mon Sep 17 00:00:00 2001 From: Elia Renzoni Date: Sat, 11 Jul 2026 19:08:44 +0200 Subject: [PATCH 2/2] refactor: remove ALBConfigs Signed-off-by: Elia Renzoni --- pkg/backends/alb/alb_502_test.go | 4 +-- pkg/backends/alb/client.go | 1 + pkg/backends/alb/mech/fr/first_response.go | 14 ++++---- .../alb/mech/nlm/newest_last_modified.go | 6 ++-- pkg/backends/alb/mech/registry/registry.go | 35 ++++++++++++++----- .../registry/registry_consistency_test.go | 4 +-- pkg/backends/alb/mech/rr/round_robin.go | 2 +- .../alb/mech/tsm/time_series_merge.go | 4 +-- .../mech/tsm/time_series_merge_ctor_test.go | 8 ++--- pkg/backends/alb/mech/types/types.go | 6 +++- .../alb/mech/types/types_interfaces_test.go | 6 ++-- pkg/backends/alb/mech/ur/user_router.go | 8 ++--- pkg/backends/alb/options/options.go | 29 ++++++--------- 13 files changed, 71 insertions(+), 56 deletions(-) diff --git a/pkg/backends/alb/alb_502_test.go b/pkg/backends/alb/alb_502_test.go index b0d9a1ba6..7ff8a3c10 100644 --- a/pkg/backends/alb/alb_502_test.go +++ b/pkg/backends/alb/alb_502_test.go @@ -108,7 +108,7 @@ func TestALB502WithMultiplePrometheusBackends(t *testing.T) { pool1, _, _ := albpool.NewHealthy([]http.Handler{handler1}) albpool.WaitHealthy(t, pool1, 1) - albConfigs := &ao.ALBConfigs{ + albConfigs := &ao.TSMConfigs{ MechanismName: names.MechanismTSM, OutputFormat: providers.Prometheus, } @@ -135,7 +135,7 @@ func TestALB502WithMultiplePrometheusBackends(t *testing.T) { pool2, _, _ := albpool.NewHealthy([]http.Handler{handler1, handler2}) albpool.WaitHealthy(t, pool2, 2) - albConfigs := &ao.ALBConfigs{ + albConfigs := &ao.TSMConfigs{ MechanismName: names.MechanismTSM, OutputFormat: providers.Prometheus, } diff --git a/pkg/backends/alb/client.go b/pkg/backends/alb/client.go index 88e47bd22..afeb5cc8e 100644 --- a/pkg/backends/alb/client.go +++ b/pkg/backends/alb/client.go @@ -93,6 +93,7 @@ func NewClient(name string, o *bo.Options, router http.Handler, if err != nil { return nil, err } + c.handler = m } return c, nil diff --git a/pkg/backends/alb/mech/fr/first_response.go b/pkg/backends/alb/mech/fr/first_response.go index df5895345..40dafd54a 100644 --- a/pkg/backends/alb/mech/fr/first_response.go +++ b/pkg/backends/alb/mech/fr/first_response.go @@ -53,19 +53,19 @@ func RegistryEntryFGR() types.RegistryEntry { return types.RegistryEntry{Name: FGRName, ShortName: names.MechanismFGR, New: NewFGR} } -func NewFGR(conf *options.ALBConfigs, _ rt.Lookup) (types.Mechanism, error) { +func NewFGR(o *options.Options, _ rt.Lookup) (types.Mechanism, error) { return &handler{ fgr: true, - fgrCodes: conf.FgrCodesLookup, - options: conf.FirstGoodResponseOptions, - maxCaptureBytes: conf.MaxCaptureBytes, + fgrCodes: o.FgrCodesLookup, + options: o.FGROptions, + maxCaptureBytes: o.MaxCaptureBytes, }, nil } -func New(conf *options.ALBConfigs, _ rt.Lookup) (types.Mechanism, error) { +func New(o *options.Options, _ rt.Lookup) (types.Mechanism, error) { h := &handler{} - if conf != nil { - h.maxCaptureBytes = conf.MaxCaptureBytes + if o != nil { + h.maxCaptureBytes = o.MaxCaptureBytes } return h, nil } diff --git a/pkg/backends/alb/mech/nlm/newest_last_modified.go b/pkg/backends/alb/mech/nlm/newest_last_modified.go index 467c8e1aa..66d98799f 100644 --- a/pkg/backends/alb/mech/nlm/newest_last_modified.go +++ b/pkg/backends/alb/mech/nlm/newest_last_modified.go @@ -44,10 +44,10 @@ func RegistryEntry() types.RegistryEntry { return types.RegistryEntry{Name: Name, ShortName: names.MechanismNLM, New: New} } -func New(conf *options.ALBConfigs, _ rt.Lookup) (types.Mechanism, error) { +func New(o *options.Options, _ rt.Lookup) (types.Mechanism, error) { return &handler{ - options: conf.NewestLastModifiedOptions, - maxCaptureBytes: conf.MaxCaptureBytes, + options: o.NLMOptions, + maxCaptureBytes: o.MaxCaptureBytes, }, nil } diff --git a/pkg/backends/alb/mech/registry/registry.go b/pkg/backends/alb/mech/registry/registry.go index dec5f574a..1b73b93eb 100644 --- a/pkg/backends/alb/mech/registry/registry.go +++ b/pkg/backends/alb/mech/registry/registry.go @@ -40,20 +40,34 @@ var registry = []types.RegistryEntry{ var registryByName = compileSupportedByName(registry) +type funcs struct { + tsmMechanismFunc types.NewTSMMechanismFunc + mechanismFunc types.NewMechanismFunc +} + // compileSupportedByName indexes registry entries by both Name and ShortName. // Panics on duplicate registration: silently last-write-wins would mask a // configuration error that only surfaces at request time. -func compileSupportedByName(entries []types.RegistryEntry) map[types.Name]types.NewMechanismFunc { - out := make(map[types.Name]types.NewMechanismFunc, len(entries)*2) - add := func(name types.Name, fn types.NewMechanismFunc) { +func compileSupportedByName(entries []types.RegistryEntry) map[types.Name]*funcs { + out := make(map[types.Name]*funcs, len(entries)*2) + add := func(name types.Name, fns *funcs) { if _, exists := out[name]; exists { panic("alb/mech/registry: duplicate mechanism name " + name) } - out[name] = fn + out[name] = fns } + for _, entry := range entries { - add(entry.ShortName, entry.New) - add(entry.Name, entry.New) + if entry.New == nil && entry.NewTSM != nil { + fns := &funcs{tsmMechanismFunc: entry.NewTSM} + add(entry.ShortName, fns) + add(entry.Name, fns) + continue + } + + fns := &funcs{mechanismFunc: entry.New} + add(entry.ShortName, fns) + add(entry.Name, fns) } return out } @@ -62,9 +76,14 @@ func New(name types.Name, opts *options.Options, factories rt.Lookup, ) (types.Mechanism, error) { if f, ok := registryByName[name]; ok && f != nil { - configs := options.NewALBConfigsFromOptions(opts) - return f(configs, factories) + if name == tsm.Name || name == tsm.ShortName { + tsmConfigs := options.NewTSMConfigs(opts) + return f.tsmMechanismFunc(tsmConfigs, factories) + } + + return f.mechanismFunc(opts, factories) } + return nil, errors.ErrUnsupportedMechanism } diff --git a/pkg/backends/alb/mech/registry/registry_consistency_test.go b/pkg/backends/alb/mech/registry/registry_consistency_test.go index 2cdeb21da..8e2e23df4 100644 --- a/pkg/backends/alb/mech/registry/registry_consistency_test.go +++ b/pkg/backends/alb/mech/registry/registry_consistency_test.go @@ -27,7 +27,7 @@ import ( func TestCompileSupportedByNamePanicsOnDuplicate(t *testing.T) { t.Parallel() - noop := func(_ *options.ALBConfigs, _ rt.Lookup) (types.Mechanism, error) { + noop := func(_ *options.Options, _ rt.Lookup) (types.Mechanism, error) { return nil, nil } entries := []types.RegistryEntry{ @@ -49,7 +49,7 @@ func TestCompileSupportedByNamePanicsOnDuplicate(t *testing.T) { func TestCompileSupportedByNamePanicsOnNameShortNameCollision(t *testing.T) { t.Parallel() - noop := func(_ *options.ALBConfigs, _ rt.Lookup) (types.Mechanism, error) { + noop := func(_ *options.Options, _ rt.Lookup) (types.Mechanism, error) { return nil, nil } entries := []types.RegistryEntry{ diff --git a/pkg/backends/alb/mech/rr/round_robin.go b/pkg/backends/alb/mech/rr/round_robin.go index d1c1943ed..dd1b273c0 100644 --- a/pkg/backends/alb/mech/rr/round_robin.go +++ b/pkg/backends/alb/mech/rr/round_robin.go @@ -40,7 +40,7 @@ func RegistryEntry() types.RegistryEntry { return types.RegistryEntry{Name: Name, ShortName: names.MechanismRR, New: New} } -func New(_ *options.ALBConfigs, _ rt.Lookup) (types.Mechanism, error) { +func New(_ *options.Options, _ rt.Lookup) (types.Mechanism, error) { return &handler{}, nil } diff --git a/pkg/backends/alb/mech/tsm/time_series_merge.go b/pkg/backends/alb/mech/tsm/time_series_merge.go index 2e9784327..ebe2f2d48 100644 --- a/pkg/backends/alb/mech/tsm/time_series_merge.go +++ b/pkg/backends/alb/mech/tsm/time_series_merge.go @@ -96,10 +96,10 @@ type stripKeysSnapshot struct { } func RegistryEntry() types.RegistryEntry { - return types.RegistryEntry{Name: Name, ShortName: ShortName, New: New} + return types.RegistryEntry{Name: Name, ShortName: ShortName, NewTSM: New} } -func New(conf *options.ALBConfigs, factories rt.Lookup) (types.Mechanism, error) { +func New(conf *options.TSMConfigs, factories rt.Lookup) (types.Mechanism, error) { out := &handler{ tsmOptions: conf.TimeSeriesMergeOptions, maxCaptureBytes: conf.MaxCaptureBytes, diff --git a/pkg/backends/alb/mech/tsm/time_series_merge_ctor_test.go b/pkg/backends/alb/mech/tsm/time_series_merge_ctor_test.go index e553706f4..8354f51a9 100644 --- a/pkg/backends/alb/mech/tsm/time_series_merge_ctor_test.go +++ b/pkg/backends/alb/mech/tsm/time_series_merge_ctor_test.go @@ -35,7 +35,7 @@ func TestRegistryEntry(t *testing.T) { if entry.Name != Name || entry.ShortName != ShortName { t.Fatalf("RegistryEntry = %+v, want Name=%q ShortName=%q", entry, Name, ShortName) } - if entry.New == nil { + if entry.NewTSM == nil { t.Fatal("RegistryEntry.New is nil") } } @@ -45,7 +45,7 @@ func TestNew(t *testing.T) { t.Run("invalid output format", func(t *testing.T) { t.Parallel() - _, err := New(&options.ALBConfigs{OutputFormat: "not-a-provider"}, nil) + _, err := New(&options.TSMConfigs{OutputFormat: "not-a-provider"}, nil) if !errors.Is(err, alberr.ErrInvalidTimeSeriesMergeProvider) { t.Fatalf("New() error = %v, want ErrInvalidTimeSeriesMergeProvider", err) } @@ -53,7 +53,7 @@ func TestNew(t *testing.T) { t.Run("missing factory", func(t *testing.T) { t.Parallel() - _, err := New(&options.ALBConfigs{OutputFormat: providers.Prometheus}, rt.Lookup{}) + _, err := New(&options.TSMConfigs{OutputFormat: providers.Prometheus}, rt.Lookup{}) if !errors.Is(err, alberr.ErrInvalidTimeSeriesMergeProvider) { t.Fatalf("New() error = %v, want ErrInvalidTimeSeriesMergeProvider", err) } @@ -62,7 +62,7 @@ func TestNew(t *testing.T) { t.Run("valid prometheus provider", func(t *testing.T) { t.Parallel() m, err := New( - &options.ALBConfigs{OutputFormat: providers.Prometheus}, + &options.TSMConfigs{OutputFormat: providers.Prometheus}, rt.Lookup{providers.Prometheus: prometheus.NewClient}, ) if err != nil { diff --git a/pkg/backends/alb/mech/types/types.go b/pkg/backends/alb/mech/types/types.go index d6646b1da..f17cec23b 100644 --- a/pkg/backends/alb/mech/types/types.go +++ b/pkg/backends/alb/mech/types/types.go @@ -29,7 +29,10 @@ type Name = string // NewMechanismFunc defines a function that returns a new Mechanism from the // provided Options -type NewMechanismFunc func(*options.ALBConfigs, types.Lookup) (Mechanism, error) +type NewMechanismFunc func(*options.Options, types.Lookup) (Mechanism, error) + +// NewTSMMechanismFUn defines a function that returns a Time Series Merge mechanism +type NewTSMMechanismFunc func(*options.TSMConfigs, types.Lookup) (Mechanism, error) // Mechanism represents a specific ALB Implementation (e.g., a Round Robiner). // Pool-aware mechanisms additionally implement PoolMechanism; callers that @@ -54,4 +57,5 @@ type RegistryEntry struct { Name Name ShortName Name New NewMechanismFunc + NewTSM NewTSMMechanismFunc } diff --git a/pkg/backends/alb/mech/types/types_interfaces_test.go b/pkg/backends/alb/mech/types/types_interfaces_test.go index 881b1b6ad..ef7bd77a1 100644 --- a/pkg/backends/alb/mech/types/types_interfaces_test.go +++ b/pkg/backends/alb/mech/types/types_interfaces_test.go @@ -57,21 +57,21 @@ func TestPoolMechanismMembership(t *testing.T) { return m }, true}, {"nlm", func(t *testing.T) types.Mechanism { - m, err := nlm.New(&options.ALBConfigs{}, nil) + m, err := nlm.New(&options.Options{}, nil) if err != nil { t.Fatalf("nlm.New: %v", err) } return m }, true}, {"ur", func(t *testing.T) types.Mechanism { - m, err := ur.New(&options.ALBConfigs{UserRouter: &uropt.Options{}}, nil) + m, err := ur.New(&options.Options{UserRouter: &uropt.Options{}}, nil) if err != nil { t.Fatalf("ur.New: %v", err) } return m }, false}, {"tsm", func(t *testing.T) types.Mechanism { - o := &options.ALBConfigs{OutputFormat: providers.Prometheus} + o := &options.TSMConfigs{OutputFormat: providers.Prometheus} factories := rt.Lookup{providers.Prometheus: prometheus.NewClient} m, err := tsm.New(o, factories) if err != nil { diff --git a/pkg/backends/alb/mech/ur/user_router.go b/pkg/backends/alb/mech/ur/user_router.go index 21dbf92cd..66e2418ca 100644 --- a/pkg/backends/alb/mech/ur/user_router.go +++ b/pkg/backends/alb/mech/ur/user_router.go @@ -69,13 +69,13 @@ func RegistryEntry() types.RegistryEntry { } } -func New(conf *options.ALBConfigs, _ rt.Lookup) (types.Mechanism, error) { - if conf == nil || conf.UserRouter == nil { +func New(o *options.Options, _ rt.Lookup) (types.Mechanism, error) { + if o == nil || o.UserRouter == nil { return nil, errors.ErrInvalidOptions } out := &Handler{ - noRouteStatusCode: conf.UserRouter.NoRouteStatusCode, - options: conf.UserRouter, + noRouteStatusCode: o.UserRouter.NoRouteStatusCode, + options: o.UserRouter, } return out, nil } diff --git a/pkg/backends/alb/options/options.go b/pkg/backends/alb/options/options.go index 0c30b9e98..f2f925391 100644 --- a/pkg/backends/alb/options/options.go +++ b/pkg/backends/alb/options/options.go @@ -141,30 +141,21 @@ var ( ErrOutputFormatOnlyForTSM = errors.New("'output_format' option is only valid for provider 'alb' and mechanism 'tsmerge'") ) -type ALBConfigs struct { +type TSMConfigs struct { TimeSeriesMergeOptions - NewestLastModifiedOptions - FirstGoodResponseOptions - UserRouter *ur.Options - - MechanismName string - OutputFormat string MaxCaptureBytes int MaxFanoutCaptureBytes int - FgrCodesLookup sets.Set[int] + OutputFormat string + MechanismName string } -func NewALBConfigsFromOptions(o *Options) *ALBConfigs { - return &ALBConfigs{ - TimeSeriesMergeOptions: o.TSMOptions, - NewestLastModifiedOptions: o.NLMOptions, - FirstGoodResponseOptions: o.FGROptions, - UserRouter: o.UserRouter, - MechanismName: o.MechanismName, - OutputFormat: o.OutputFormat, - MaxCaptureBytes: o.MaxCaptureBytes, - MaxFanoutCaptureBytes: o.MaxFanoutCaptureBytes, - FgrCodesLookup: o.FgrCodesLookup, +func NewTSMConfigs(o *Options) *TSMConfigs { + return &TSMConfigs{ + TimeSeriesMergeOptions: o.TSMOptions, + MaxCaptureBytes: o.MaxCaptureBytes, + MaxFanoutCaptureBytes: o.MaxFanoutCaptureBytes, + OutputFormat: o.OutputFormat, + MechanismName: o.MechanismName, } }