diff --git a/internal/config/config.go b/internal/config/config.go index 569048d..7f8f8e8 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -6,6 +6,7 @@ import ( "crypto/x509" "errors" "fmt" + "math" "net" "net/url" "os" @@ -37,6 +38,9 @@ type Config struct { Relay struct { Addr string `yaml:"addr"` // host:port of the outgoing SMTP relay Concurrency int `yaml:"concurrency"` // simultaneous deliveries + // RatePerSecond is the most sends started per second, for relays that throttle (Amazon + // SES). Zero, the default, is unlimited. + RatePerSecond Rate `yaml:"rate_per_second"` // TLS is "none" (default: a local Postfix), "starttls" (required, never silently skipped) // or "tls" (encrypted from the first byte, port 465). TLS string `yaml:"tls"` @@ -50,6 +54,20 @@ type Config struct { } `yaml:"relay"` } +// Rate is a number of sends per second. It exists so that a value that is not a number is reported +// with the name of the key, as the other checks are. +type Rate float64 + +// UnmarshalYAML reads a YAML number. +func (r *Rate) UnmarshalYAML(n *yaml.Node) error { + var f float64 + if err := n.Decode(&f); err != nil { + return fmt.Errorf("line %d: relay.rate_per_second must be a number of sends per second, not %q", n.Line, n.Value) + } + *r = Rate(f) + return nil +} + // DKIMKey is the signing key of one domain. type DKIMKey struct { Domain string `yaml:"domain"` @@ -138,6 +156,9 @@ func (c Config) validateRelay() error { default: return fmt.Errorf("relay.tls must be none, starttls or tls, not %q", r.TLS) } + if math.IsNaN(float64(r.RatePerSecond)) || math.IsInf(float64(r.RatePerSecond), 0) || r.RatePerSecond < 0 { + return fmt.Errorf("relay.rate_per_second must be a number of sends per second, 0 or more (0 is unlimited), not %v", r.RatePerSecond) + } if (r.Username == "") != (r.PasswordFile == "") { return errors.New("relay.username and relay.password_file go together") } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 9a27087..5ff9076 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -170,6 +170,28 @@ func TestLoadRejectsBadRelaySettings(t *testing.T) { } } +func TestRelayRatePerSecond(t *testing.T) { + c, err := Load(write(t, "{}\n")) + if err != nil || float64(c.Relay.RatePerSecond) != 0 { + t.Fatalf("default is unlimited: %v %v", c.Relay.RatePerSecond, err) + } + for y, want := range map[string]float64{"14": 14, "0.5": 0.5, "0": 0, "0.0": 0, "1e2": 100} { + c, err := Load(write(t, "relay:\n rate_per_second: "+y+"\n")) + if err != nil || float64(c.Relay.RatePerSecond) != want { + t.Errorf("rate_per_second %s: got %v, %v; want %v", y, c.Relay.RatePerSecond, err, want) + } + } +} + +func TestLoadRejectsABadRelayRate(t *testing.T) { + for _, v := range []string{"-1", "-0.5", ".nan", ".NaN", ".inf", "-.inf", `"nan"`, `"fast"`, "fast", "nan", "[]", "{}"} { + _, err := Load(write(t, "relay:\n rate_per_second: "+v+"\n")) + if err == nil || !strings.Contains(err.Error(), "relay.rate_per_second") { + t.Errorf("rate_per_second %s: want an error naming the key, got %v", v, err) + } + } +} + func TestCredentialsToALoopbackRelayNeedNoTLS(t *testing.T) { for _, addr := range []string{"127.0.0.1:587", "localhost:587", "[::1]:587"} { if _, err := Load(write(t, "relay:\n addr: \""+addr+"\"\n username: u\n password_file: /p\n")); err != nil {