Configuring the Rate Limiters.

This commit is contained in:
Maxime Delporte
2025-11-18 20:10:55 +01:00
parent 7de9ef89b7
commit 83bb7294db
2 changed files with 42 additions and 25 deletions

View File

@@ -28,17 +28,23 @@ For now, the only configuration settings will be the network port that we want t
and the name of the current operating environment for the application (development, staging, production, etc.)
We'll read in the configuration settings from command-line flags when the application starts.
*/
// Add a db struct field to hold the configuration settings for our database connection pool. For now, this only holds the DSN, which we will read in from a command-line flag.
// Add maxOpenConns, maxIdleConns and maxIdleTime fields to hold the configuration settings for the connection pool
type config struct {
port int
env string
db struct {
dsn string
// Add a db struct field to hold the configuration settings for our database connection pool. For now, this only holds the DSN, which we will read in from a command-line flag.
db struct {
dsn string
// Add maxOpenConns, maxIdleConns and maxIdleTime fields to hold the configuration settings for the connection pool
maxOpenConns int
maxIdleConns int
maxIdleTime time.Duration
}
// Add a new limiter struct containing fields for the requests-per-second and burst values, and a boolean field which we can use to enable/disable rate limiting altogether
limiter struct {
rps float64
burst int
enabled bool
}
}
// application struct hold the dependencies for our HTTP handlers, helpers, and middleware.
@@ -68,6 +74,11 @@ func main() {
flag.IntVar(&cfg.db.maxIdleConns, "db-max-idle-conns", 25, "PostgreSQL max idle connections")
flag.DurationVar(&cfg.db.maxIdleTime, "db-max-idle-time", 15*time.Minute, "PostgreSQL max connection idle time")
// Create command line flags to read the setting values into the config struct. Notice taht we use true as the default for the 'enabled' setting
flag.Float64Var(&cfg.limiter.rps, "limiter-rps", 2, "Rate limiter maximum requests per second")
flag.IntVar(&cfg.limiter.burst, "limiter-burst", 4, "Rate limiter maximum burst")
flag.BoolVar(&cfg.limiter.enabled, "limiter-enabled", true, "Enable rate limiter")
flag.Parse()
// Initialize a new structured logger which writes log entries to standard out stream.