config.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package semaphores
  2. import (
  3. "fmt"
  4. "runtime"
  5. "github.com/imgproxy/imgproxy/v3/config"
  6. "github.com/imgproxy/imgproxy/v3/ensure"
  7. )
  8. // Config represents handler config
  9. type Config struct {
  10. RequestsQueueSize int // Request queue size
  11. Workers int // Number of workers
  12. }
  13. // NewDefaultConfig creates a new configuration with defaults
  14. func NewDefaultConfig() Config {
  15. return Config{
  16. RequestsQueueSize: 0,
  17. Workers: runtime.GOMAXPROCS(0) * 2,
  18. }
  19. }
  20. // LoadConfigFromEnv loads config from environment variables
  21. func LoadConfigFromEnv(c *Config) (*Config, error) {
  22. c = ensure.Ensure(c, NewDefaultConfig)
  23. c.RequestsQueueSize = config.RequestsQueueSize
  24. c.Workers = config.Workers
  25. return c, nil
  26. }
  27. // Validate checks configuration values
  28. func (c *Config) Validate() error {
  29. if c.RequestsQueueSize < 0 {
  30. return fmt.Errorf("requests queue size should be greater than or equal 0, now - %d", c.RequestsQueueSize)
  31. }
  32. if c.Workers <= 0 {
  33. return fmt.Errorf("workers number should be greater than 0, now - %d", c.Workers)
  34. }
  35. return nil
  36. }