Skip to content

refactor(traffic): replace package-vars with Scheduler interface and struct - #28

Merged
maansaake merged 2 commits into
mainfrom
refactor/traffic-scheduler-struct
May 24, 2026
Merged

refactor(traffic): replace package-vars with Scheduler interface and struct#28
maansaake merged 2 commits into
mainfrom
refactor/traffic-scheduler-struct

Conversation

@maansaake

Copy link
Copy Markdown
Owner

Summary

Refactors pkg/traffic to eliminate all package-level mutable state, replacing it with a proper struct-based design backed by an exported Scheduler interface.

Changes

pkg/traffic/scheduler.go

  • Add Opts struct with Logger, WorkerLimit, SampleTolerancePerc fields
  • Add Scheduler interface with Run(ctx, metadata, reporter) error and Stop() error methods
  • Add unexported scheduler struct implementing Scheduler
  • Add New(opts *Opts) Scheduler constructor with defaulting
  • Remove all package-level variables: reporter, workloads, stopChan, logger, SampleTolerancePerc
  • Remove package-level Run() and Stop() functions

pkg/traffic/workload.go

  • Add reporter, stopChan, logger as injected struct fields
  • Replace all references to package-level reporter, stopChan, and logger

pkg/traffic/worker.go

  • Replace logger.X calls with worker.parent.logger.X

pkg/traffic/scheduler_test.go

  • Add newTestScheduler() helper using New(&Opts{...})
  • Update all tests to call .Run() / .Stop() on the scheduler instance

arbiter.go

  • Create sched := traffic.New(&traffic.Opts{Logger: logger, WorkerLimit: ...}) at the start of run()
  • Replace traffic.Run() / traffic.Stop() calls with sched.Run() / sched.Stop()

Validation

make validate passes: lint clean, all unit tests green, sample example runs successfully.

GitHub Copilot and others added 2 commits May 24, 2026 04:04
…struct

Move pkg/traffic from package-level mutable state to a proper
struct-based design with an exported Scheduler interface.

Changes:
- Add Opts struct (Logger, WorkerLimit, SampleTolerancePerc)
- Add Scheduler interface with Run/Stop methods
- Add unexported scheduler struct implementing Scheduler
- Add New(opts *Opts) Scheduler constructor with defaulting
- Remove package-level Run(), Stop() functions and all package vars
  (reporter, workloads, stopChan, logger, SampleTolerancePerc)
- Inject reporter, stopChan, logger into workload struct fields
- workload.worker uses parent.logger instead of package-level logger
- Update arbiter.go to create traffic.New(&traffic.Opts{...}) and
  call sched.Run() / sched.Stop() on the instance
- Update all tests to instantiate via New() and call methods on instance

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@maansaake
maansaake merged commit 65de2a7 into main May 24, 2026
15 checks passed
@maansaake
maansaake deleted the refactor/traffic-scheduler-struct branch May 24, 2026 02:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant