Merge branch 'feat/nl-rate-limit' into feat/newsletter
This commit is contained in:
commit
3d25f73d9b
8 changed files with 584 additions and 0 deletions
|
|
@ -229,6 +229,7 @@ func Start(cfg config.Config, apiToken string) (*App, error) {
|
|||
Username: cfg.Relay.Username, Password: relayPassword, Helo: heloName(cfg.Relay.Helo),
|
||||
},
|
||||
Concurrency: cfg.Relay.Concurrency,
|
||||
Rate: float64(cfg.Relay.RatePerSecond),
|
||||
Sign: sign,
|
||||
}
|
||||
rt.OnEnqueue = worker.Wake
|
||||
|
|
|
|||
|
|
@ -1310,3 +1310,52 @@ func TestHTTPServersCarryTheTimeouts(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheRelayRateReachesTheWorker(t *testing.T) {
|
||||
relayAddr, relay := startSink(t)
|
||||
cfg := testConfig(t, relayAddr)
|
||||
cfg.Relay.RatePerSecond = 20 // 50 ms between two sends
|
||||
a, err := Start(cfg, "tok")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { a.Close() })
|
||||
call(t, a, "POST", "/api/v1/lists", `{"name":"dev","domain":"example.org"}`, "tok")
|
||||
const members = 8
|
||||
for i := 0; i < members; i++ {
|
||||
call(t, a, "POST", "/api/v1/lists/1/members", fmt.Sprintf(`{"address":"m%d@example.com"}`, i), "tok")
|
||||
}
|
||||
start := time.Now()
|
||||
msg := "From: M0 <m0@example.com>\r\nSubject: paced\r\nMessage-ID: <p1@x>\r\n\r\nbody\r\n"
|
||||
if err := lmtpSend(t, a.LMTPAddr, "m0@example.com", "dev@example.org", msg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
relay.waitCount(t, members)
|
||||
// 8 sends started 50 ms apart: the last one cannot start before 350 ms.
|
||||
if took := time.Since(start); took < 330*time.Millisecond {
|
||||
t.Fatalf("%d deliveries took %v: the rate did not reach the worker", members, took)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithoutARateTheRelayIsNotPaced(t *testing.T) {
|
||||
relayAddr, relay := startSink(t)
|
||||
a, err := Start(testConfig(t, relayAddr), "tok")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { a.Close() })
|
||||
call(t, a, "POST", "/api/v1/lists", `{"name":"dev","domain":"example.org"}`, "tok")
|
||||
const members = 8
|
||||
for i := 0; i < members; i++ {
|
||||
call(t, a, "POST", "/api/v1/lists/1/members", fmt.Sprintf(`{"address":"m%d@example.com"}`, i), "tok")
|
||||
}
|
||||
start := time.Now()
|
||||
msg := "From: M0 <m0@example.com>\r\nSubject: free\r\nMessage-ID: <p2@x>\r\n\r\nbody\r\n"
|
||||
if err := lmtpSend(t, a.LMTPAddr, "m0@example.com", "dev@example.org", msg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
relay.waitCount(t, members)
|
||||
if took := time.Since(start); took > 3*time.Second {
|
||||
t.Fatalf("%d deliveries took %v without a rate", members, took)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
81
internal/queue/pacer.go
Normal file
81
internal/queue/pacer.go
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
package queue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// maxInterval caps the spacing of a very low rate, so the arithmetic on time never overflows.
|
||||
const maxInterval = 365 * 24 * time.Hour
|
||||
|
||||
// pacer spaces the start of sends: at most one every interval, with no burst. Relays such as
|
||||
// Amazon SES refuse mail sent faster than their rate.
|
||||
type pacer struct {
|
||||
interval time.Duration
|
||||
now func() time.Time
|
||||
sleep func(ctx context.Context, d time.Duration) error
|
||||
|
||||
mu sync.Mutex
|
||||
next time.Time // the first free slot
|
||||
}
|
||||
|
||||
// newPacer returns a pacer for perSecond sends per second. It returns nil, which never waits, when
|
||||
// perSecond is not a positive finite number: the rate is unlimited.
|
||||
func newPacer(perSecond float64, now func() time.Time, sleep func(ctx context.Context, d time.Duration) error) *pacer {
|
||||
if !(perSecond > 0) || math.IsInf(perSecond, 0) {
|
||||
return nil
|
||||
}
|
||||
interval := maxInterval
|
||||
if f := float64(time.Second) / perSecond; f < float64(maxInterval) {
|
||||
interval = time.Duration(f)
|
||||
}
|
||||
return &pacer{interval: interval, now: now, sleep: sleep}
|
||||
}
|
||||
|
||||
// Wait blocks until the caller's slot. Each caller reserves the next free slot under the lock and
|
||||
// sleeps outside it. A cancelled context returns its error at once; a slot reserved by a caller
|
||||
// that gives up is released when no later caller has reserved after it.
|
||||
func (p *pacer) Wait(ctx context.Context) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
p.mu.Lock()
|
||||
now := p.now()
|
||||
slot := p.next
|
||||
if slot.Before(now) {
|
||||
slot = now
|
||||
}
|
||||
p.next = slot.Add(p.interval)
|
||||
p.mu.Unlock()
|
||||
|
||||
d := slot.Sub(now)
|
||||
if d <= 0 {
|
||||
return nil
|
||||
}
|
||||
if err := p.sleep(ctx, d); err != nil {
|
||||
p.mu.Lock()
|
||||
if p.next.Equal(slot.Add(p.interval)) {
|
||||
p.next = slot
|
||||
}
|
||||
p.mu.Unlock()
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// sleepCtx sleeps for d, or until ctx is done.
|
||||
func sleepCtx(ctx context.Context, d time.Duration) error {
|
||||
t := time.NewTimer(d)
|
||||
defer t.Stop()
|
||||
select {
|
||||
case <-t.C:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
293
internal/queue/pacer_test.go
Normal file
293
internal/queue/pacer_test.go
Normal file
|
|
@ -0,0 +1,293 @@
|
|||
package queue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"math"
|
||||
"sort"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// fakeTime is a clock and a sleep that never really sleeps. With advance set, a sleep moves the clock
|
||||
// (a single caller); otherwise the clock stands still and the durations asked for are recorded
|
||||
// (many callers reserving slots at the same instant).
|
||||
type fakeTime struct {
|
||||
mu sync.Mutex
|
||||
t time.Time
|
||||
advance bool
|
||||
slept []time.Duration
|
||||
err error // returned by sleep when set
|
||||
}
|
||||
|
||||
func (f *fakeTime) now() time.Time {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
return f.t
|
||||
}
|
||||
|
||||
func (f *fakeTime) move(d time.Duration) {
|
||||
f.mu.Lock()
|
||||
f.t = f.t.Add(d)
|
||||
f.mu.Unlock()
|
||||
}
|
||||
|
||||
func (f *fakeTime) sleep(ctx context.Context, d time.Duration) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
f.slept = append(f.slept, d)
|
||||
if f.err != nil {
|
||||
return f.err
|
||||
}
|
||||
if f.advance {
|
||||
f.t = f.t.Add(d)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeTime) durations() []time.Duration {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
d := append([]time.Duration(nil), f.slept...)
|
||||
sort.Slice(d, func(i, j int) bool { return d[i] < d[j] })
|
||||
return d
|
||||
}
|
||||
|
||||
func TestPacerSpacesSendsByTheInterval(t *testing.T) {
|
||||
f := &fakeTime{t: t0, advance: true}
|
||||
p := newPacer(10, f.now, f.sleep) // one slot every 100 ms
|
||||
for i := 0; i < 4; i++ {
|
||||
if err := p.Wait(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if want := t0.Add(time.Duration(i) * 100 * time.Millisecond); !f.now().Equal(want) {
|
||||
t.Fatalf("send %d starts at +%v, want +%v", i, f.now().Sub(t0), want.Sub(t0))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPacerFirstSendDoesNotWait(t *testing.T) {
|
||||
f := &fakeTime{t: t0, advance: true}
|
||||
p := newPacer(1, f.now, f.sleep)
|
||||
p.Wait(context.Background())
|
||||
if len(f.slept) != 0 {
|
||||
t.Fatalf("the first send waited: %v", f.slept)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPacerAllowsNoBurstAfterAnIdlePeriod(t *testing.T) {
|
||||
f := &fakeTime{t: t0, advance: true}
|
||||
p := newPacer(10, f.now, f.sleep)
|
||||
p.Wait(context.Background())
|
||||
f.move(10 * time.Second) // idle: slots do not pile up
|
||||
start := f.now()
|
||||
p.Wait(context.Background())
|
||||
p.Wait(context.Background())
|
||||
if got := f.now().Sub(start); got != 100*time.Millisecond {
|
||||
t.Fatalf("two sends after an idle period took %v, want exactly one interval", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPacerSleepsOnlyWhatIsLeftOfTheInterval(t *testing.T) {
|
||||
f := &fakeTime{t: t0, advance: true}
|
||||
p := newPacer(10, f.now, f.sleep)
|
||||
p.Wait(context.Background())
|
||||
f.move(30 * time.Millisecond) // the send itself took 30 ms
|
||||
p.Wait(context.Background())
|
||||
if len(f.slept) != 1 || f.slept[0] != 70*time.Millisecond {
|
||||
t.Fatalf("slept %v, want [70ms]", f.slept)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPacerGivesConcurrentCallersDistinctSlots(t *testing.T) {
|
||||
const n = 16
|
||||
f := &fakeTime{t: t0} // the clock stands still: every caller reserves at the same instant
|
||||
p := newPacer(4, f.now, f.sleep)
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < n; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if err := p.Wait(context.Background()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
got := f.durations() // the first caller does not sleep, so n-1 sleeps: 250ms, 500ms...
|
||||
if len(got) != n-1 {
|
||||
t.Fatalf("%d sleeps, want %d: %v", len(got), n-1, got)
|
||||
}
|
||||
for i, d := range got {
|
||||
if want := time.Duration(i+1) * 250 * time.Millisecond; d != want {
|
||||
t.Fatalf("sleeps %v: slot %d is %v, want %v", got, i+1, d, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPacerCancelledBeforeWaitingReturnsAtOnceAndTakesNoSlot(t *testing.T) {
|
||||
f := &fakeTime{t: t0}
|
||||
p := newPacer(1, f.now, f.sleep)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if err := p.Wait(ctx); !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
if len(f.slept) != 0 {
|
||||
t.Fatalf("slept %v", f.slept)
|
||||
}
|
||||
// No slot was used: the next caller goes first, without waiting.
|
||||
if err := p.Wait(context.Background()); err != nil || len(f.slept) != 0 {
|
||||
t.Fatalf("err=%v slept=%v", err, f.slept)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPacerCancelledWhileWaitingGivesTheSlotBack(t *testing.T) {
|
||||
f := &fakeTime{t: t0}
|
||||
p := newPacer(10, f.now, f.sleep)
|
||||
p.Wait(context.Background()) // takes slot 0; the next free slot is +100ms
|
||||
f.err = context.Canceled
|
||||
if err := p.Wait(context.Background()); !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
f.err = nil
|
||||
f.slept = nil
|
||||
p.Wait(context.Background())
|
||||
if len(f.slept) != 1 || f.slept[0] != 100*time.Millisecond {
|
||||
t.Fatalf("after a cancelled wait the next caller sleeps %v, want [100ms]: the slot was not given back", f.slept)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPacerCancelledCallerInTheMiddleNeverBlocksTheOthers(t *testing.T) {
|
||||
f := &fakeTime{t: t0}
|
||||
p := newPacer(10, f.now, f.sleep)
|
||||
p.Wait(context.Background())
|
||||
// B and C reserve +100ms and +200ms; B is cancelled and C still gets out at +200ms at most.
|
||||
var mu sync.Mutex
|
||||
first := true
|
||||
f2 := func(ctx context.Context, d time.Duration) error {
|
||||
if first {
|
||||
first = false
|
||||
return context.Canceled
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
f.slept = append(f.slept, d)
|
||||
return nil
|
||||
}
|
||||
p.sleep = f2
|
||||
if err := p.Wait(context.Background()); err == nil {
|
||||
t.Fatal("B should fail")
|
||||
}
|
||||
if err := p.Wait(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, d := range f.slept {
|
||||
if d > 200*time.Millisecond {
|
||||
t.Fatalf("slept %v: later than the slot it reserved", d)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoPacerForARateOfZeroOrLess(t *testing.T) {
|
||||
for _, r := range []float64{0, -1, math.NaN(), math.Inf(-1), math.Inf(1)} {
|
||||
f := &fakeTime{t: t0}
|
||||
p := newPacer(r, f.now, f.sleep)
|
||||
for i := 0; i < 5; i++ {
|
||||
if err := p.Wait(context.Background()); err != nil {
|
||||
t.Fatalf("rate %v: %v", r, err)
|
||||
}
|
||||
}
|
||||
if len(f.slept) != 0 {
|
||||
t.Fatalf("rate %v: slept %v", r, f.slept)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoPacerStillStopsForACancelledContext(t *testing.T) {
|
||||
var p *pacer
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if err := p.Wait(ctx); err == nil {
|
||||
t.Fatal("a cancelled context must not pass")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPacerAtAVeryLowRateDoesNotOverflow(t *testing.T) {
|
||||
f := &fakeTime{t: t0, advance: true}
|
||||
p := newPacer(1e-300, f.now, f.sleep)
|
||||
for i := 0; i < 3; i++ {
|
||||
p.Wait(context.Background())
|
||||
}
|
||||
if len(f.slept) != 2 {
|
||||
t.Fatalf("slept %v", f.slept)
|
||||
}
|
||||
for _, d := range f.slept {
|
||||
if d <= 0 || d > maxInterval {
|
||||
t.Fatalf("sleep %v out of range (0, %v]", d, maxInterval)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPacerAtAVeryHighRateNeverWaits(t *testing.T) {
|
||||
for _, r := range []float64{1e10, 1e12, 1e300, math.MaxFloat64} {
|
||||
f := &fakeTime{t: t0}
|
||||
p := newPacer(r, f.now, f.sleep)
|
||||
for i := 0; i < 100; i++ {
|
||||
if err := p.Wait(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if len(f.slept) != 0 {
|
||||
t.Fatalf("rate %v: slept %v", r, f.slept)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSleepCtxStopsWhenTheContextIsCancelled(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
start := time.Now()
|
||||
if err := sleepCtx(ctx, time.Hour); !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
if time.Since(start) > time.Second {
|
||||
t.Fatal("did not return at once")
|
||||
}
|
||||
if err := sleepCtx(context.Background(), time.Millisecond); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPacerKeepsTheSlotsOfLaterCallersWhenAnEarlierOneGivesUp(t *testing.T) {
|
||||
f := &fakeTime{t: t0}
|
||||
release := make(chan struct{})
|
||||
p := newPacer(10, f.now, nil)
|
||||
p.sleep = func(ctx context.Context, d time.Duration) error {
|
||||
f.sleep(ctx, d)
|
||||
if d == 100*time.Millisecond {
|
||||
<-release // B waits until C has reserved its slot, then gives up
|
||||
return context.Canceled
|
||||
}
|
||||
return nil
|
||||
}
|
||||
p.Wait(context.Background()) // A: slot 0
|
||||
done := make(chan error)
|
||||
go func() { done <- p.Wait(context.Background()) }() // B: slot +100ms
|
||||
for len(f.durations()) < 1 {
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
p.Wait(context.Background()) // C: slot +200ms
|
||||
close(release)
|
||||
if err := <-done; err == nil {
|
||||
t.Fatal("B should have given up")
|
||||
}
|
||||
f.mu.Lock()
|
||||
f.slept = nil
|
||||
f.mu.Unlock()
|
||||
p.Wait(context.Background()) // D must come after C, not collide with it
|
||||
if got := f.durations(); len(got) != 1 || got[0] != 300*time.Millisecond {
|
||||
t.Fatalf("D sleeps %v, want [300ms]: it took a slot another caller holds", got)
|
||||
}
|
||||
}
|
||||
|
|
@ -60,6 +60,15 @@ type Worker struct {
|
|||
// Sign, when set, signs the final message (DKIM). On error the message is sent unsigned:
|
||||
// not delivering is worse than a missed signature.
|
||||
Sign func(msg []byte) ([]byte, error)
|
||||
// Rate is the most sends started per second, for relays that throttle (Amazon SES). Zero or
|
||||
// negative means unlimited. It is read once, at the first send.
|
||||
Rate float64
|
||||
|
||||
pacerOnce sync.Once
|
||||
pacer *pacer
|
||||
// Hooks for tests; nil means the real clock and sleep.
|
||||
pacerNow func() time.Time
|
||||
pacerSleep func(ctx context.Context, d time.Duration) error
|
||||
|
||||
wakeOnce sync.Once
|
||||
wake chan struct{}
|
||||
|
|
@ -240,6 +249,10 @@ func (w *Worker) deliver(ctx context.Context, cache *bodies, it store.OutboxItem
|
|||
if timeout <= 0 {
|
||||
timeout = defaultTimeout
|
||||
}
|
||||
// Wait for the slot before starting the timeout, so waiting does not eat into the send.
|
||||
if err := w.pace().Wait(ctx); err != nil {
|
||||
return Stats{} // shutting down: the delivery stays queued, unchanged
|
||||
}
|
||||
sctx, cancel := context.WithTimeout(ctx, timeout)
|
||||
err = w.Sender.Send(sctx, it.From, []string{it.Rcpt}, msg)
|
||||
cancel()
|
||||
|
|
@ -277,6 +290,21 @@ func (w *Worker) deliver(ctx context.Context, cache *bodies, it store.OutboxItem
|
|||
return Stats{Retried: 1}
|
||||
}
|
||||
|
||||
// pace returns the pacer in front of every send; nil (never waits) when Rate is not set.
|
||||
func (w *Worker) pace() *pacer {
|
||||
w.pacerOnce.Do(func() {
|
||||
now, sleep := w.pacerNow, w.pacerSleep
|
||||
if now == nil {
|
||||
now = time.Now
|
||||
}
|
||||
if sleep == nil {
|
||||
sleep = sleepCtx
|
||||
}
|
||||
w.pacer = newPacer(w.Rate, now, sleep)
|
||||
})
|
||||
return w.pacer
|
||||
}
|
||||
|
||||
// giveUp marks a delivery as failed for good and logs it.
|
||||
func (w *Worker) giveUp(it store.OutboxItem, reason string) Stats {
|
||||
log.Printf("queue: %s: %s", it.Rcpt, reason)
|
||||
|
|
|
|||
|
|
@ -668,3 +668,91 @@ func TestAMissingBodyIsDroppedForEveryRecipientWithOneRead(t *testing.T) {
|
|||
t.Fatalf("%d reads", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRateSpacesTheSendsOfAWorker(t *testing.T) {
|
||||
e := setup(t)
|
||||
e.enqueue(t, 5)
|
||||
e.w.Rate = 2 // one send every 500 ms
|
||||
f := &fakeTime{t: t0}
|
||||
e.w.pacerNow, e.w.pacerSleep = f.now, f.sleep
|
||||
st, _ := e.w.RunOnce(context.Background())
|
||||
if st.Sent != 5 {
|
||||
t.Fatalf("%+v", st)
|
||||
}
|
||||
got := f.durations() // 5 deliveries: slots at 0, 1, 2, 3 and 4 intervals
|
||||
want := []time.Duration{500 * time.Millisecond, time.Second, 1500 * time.Millisecond, 2 * time.Second}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("sleeps %v, want %v", got, want)
|
||||
}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Fatalf("sleeps %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRateOfZeroNeverWaits(t *testing.T) {
|
||||
e := setup(t)
|
||||
e.enqueue(t, 5)
|
||||
f := &fakeTime{t: t0}
|
||||
e.w.pacerNow, e.w.pacerSleep = f.now, f.sleep
|
||||
e.w.RunOnce(context.Background())
|
||||
if len(f.slept) != 0 || e.snd.count() != 5 {
|
||||
t.Fatalf("slept %v, sent %d", f.slept, e.snd.count())
|
||||
}
|
||||
}
|
||||
|
||||
func TestEverySendAttemptIsPacedEvenARetry(t *testing.T) {
|
||||
e := setup(t)
|
||||
e.enqueue(t, 1)
|
||||
e.w.Rate = 2
|
||||
f := &fakeTime{t: t0}
|
||||
e.w.pacerNow, e.w.pacerSleep = f.now, f.sleep
|
||||
e.snd.fn = func(context.Context, string, []string) error { return errors.New("try later") }
|
||||
e.w.RunOnce(context.Background())
|
||||
e.clk.set(t0.Add(time.Hour))
|
||||
e.w.RunOnce(context.Background())
|
||||
if e.snd.calls != 2 || len(f.slept) != 1 {
|
||||
t.Fatalf("calls=%d sleeps=%v: the second attempt shares the pacer with the first", e.snd.calls, f.slept)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPacingIsCancelledWithoutRecordingAnAttempt(t *testing.T) {
|
||||
e := setup(t)
|
||||
e.enqueue(t, 2)
|
||||
e.w.Rate = 1
|
||||
e.w.Concurrency = 1
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
f := &fakeTime{t: t0}
|
||||
e.w.pacerNow = f.now
|
||||
e.w.pacerSleep = func(ctx context.Context, d time.Duration) error {
|
||||
cancel() // shutdown arrives while the delivery waits for its slot
|
||||
return ctx.Err()
|
||||
}
|
||||
st, _ := e.w.RunOnce(ctx)
|
||||
if st != (Stats{Sent: 1}) {
|
||||
t.Fatalf("stats %+v: the delivery that did not get its slot is not counted", st)
|
||||
}
|
||||
list, _ := e.s.ListOutbox(10)
|
||||
if len(list) != 1 || list[0].Attempts != 0 || list[0].LastError != "" {
|
||||
t.Fatalf("a shutdown while waiting is not a delivery failure: %+v", list)
|
||||
}
|
||||
if e.snd.calls != 1 {
|
||||
t.Fatalf("calls = %d: only the first delivery, which did not wait, was sent", e.snd.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRateHoldsInRealTime(t *testing.T) {
|
||||
e := setup(t)
|
||||
e.enqueue(t, 20)
|
||||
e.w.Rate = 200 // 5 ms apart: 19 intervals take 95 ms
|
||||
start := time.Now()
|
||||
st, _ := e.w.RunOnce(context.Background())
|
||||
took := time.Since(start)
|
||||
if st.Sent != 20 {
|
||||
t.Fatalf("%+v", st)
|
||||
}
|
||||
if took < 90*time.Millisecond || took > time.Second {
|
||||
t.Fatalf("20 sends at 200/s took %v, want about 95 ms", took)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue