package controller

// AdaptivePacingController derives request pacing based on worker capacity.
type AdaptivePacingController struct {
	DesiredRPSPerWorker int
}

// NewAdaptivePacingController constructs a controller with sane defaults.
func NewAdaptivePacingController(desired int) AdaptivePacingController {
	if desired < 1 {
		desired = 1
	}
	return AdaptivePacingController{DesiredRPSPerWorker: desired}
}

// RequestsPerSec returns max(1, maxWorkers * desiredRPSPerWorker).
func (a AdaptivePacingController) RequestsPerSec(maxWorkers int) int {
	if maxWorkers < 1 {
		maxWorkers = 1
	}

	rps := maxWorkers * a.DesiredRPSPerWorker
	if rps < 1 {
		rps = 1
	}

	return rps
}
