Skip to content

Commit

Permalink
Make email.SesThrottle handle unlimited quota
Browse files Browse the repository at this point in the history
Per:

- https://docs.aws.amazon.com/ses/latest/APIReference-V2/API_SendQuota.html

  Max24HourSend

  The maximum number of emails that you can send in the current AWS
  Region over a 24-hour period. A value of -1 signifies an unlimited
  quota. (This value is also referred to as your sending quota.)
  • Loading branch information
mbland committed May 27, 2023
1 parent ade8bee commit a5b6ac8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
8 changes: 6 additions & 2 deletions email/throttle.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewSesThrottle(
func (t *SesThrottle) BulkCapacityAvailable(
ctx context.Context, numToSend int,
) (err error) {
if err = t.refresh(ctx); err != nil {
if err = t.refresh(ctx); err != nil || t.unlimited() {
return
} else if (t.MaxBulkSendable - t.SentLast24Hours) < numToSend {
const errFmt = "%w: %d total send max, %s desired bulk capacity, " +
Expand All @@ -82,7 +82,7 @@ func (t *SesThrottle) BulkCapacityAvailable(
func (t *SesThrottle) PauseBeforeNextSend(ctx context.Context) (err error) {
if err = t.refresh(ctx); err != nil {
return
} else if t.SentLast24Hours >= t.Max24HourSend {
} else if !t.unlimited() && t.SentLast24Hours >= t.Max24HourSend {
err = fmt.Errorf(
"%w: %d max, %d sent",
ErrExceededMax24HourSend,
Expand All @@ -103,6 +103,10 @@ func (t *SesThrottle) PauseBeforeNextSend(ctx context.Context) (err error) {
return
}

func (t *SesThrottle) unlimited() bool {
return t.Max24HourSend == -1
}

func (t *SesThrottle) refresh(ctx context.Context) (err error) {
now := t.Now()

Expand Down
31 changes: 31 additions & 0 deletions email/throttle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"errors"
"fmt"
"math"
"testing"
"time"

Expand Down Expand Up @@ -84,11 +85,21 @@ func TestNewSesThrottleIncludingRefresh(t *testing.T) {
assert.Equal(t, time.Second, f.sleepDuration)
assert.Equal(t, f.refresh, throttle.RefreshInterval)
assert.Equal(t, int(f.quota.Max24HourSend), throttle.Max24HourSend)
assert.Assert(t, throttle.unlimited() == false)
assert.Equal(t, int(f.quota.SentLast24Hours), throttle.SentLast24Hours)
assert.Equal(t, f.capacity.Value(), throttle.MaxBulkCapacity.Value())
assert.Equal(t, 37500, throttle.MaxBulkSendable)
})

t.Run("SucceedsWithUnlimitedQuota", func(t *testing.T) {
f := newSesThrottleFixture()
f.quota.Max24HourSend = float64(-1)

throttle, err := f.NewSesThrottle()
assert.NilError(t, err)
assert.Assert(t, throttle.unlimited() == true)
})

t.Run("FailsIfRefreshFails", func(t *testing.T) {
f := newSesThrottleFixture()
f.client.getAccountError = errors.New("test error")
Expand Down Expand Up @@ -148,6 +159,15 @@ func TestBulkCapacityAvailable(t *testing.T) {
assert.NilError(t, err)
})

t.Run("AlwaysSucceedsIfUnlimited", func(t *testing.T) {
f, throttle := setup(t)
throttle.Max24HourSend = -1

err := throttle.BulkCapacityAvailable(f.ctx, math.MaxInt)

assert.NilError(t, err)
})

t.Run("ErrorsIfRefreshFails", func(t *testing.T) {
f, throttle := setup(t)
numToSend := throttle.MaxBulkSendable - throttle.SentLast24Hours
Expand Down Expand Up @@ -215,6 +235,17 @@ func TestPauseBeforeNextSend(t *testing.T) {
assert.Equal(t, throttle.Max24HourSend, throttle.SentLast24Hours)
})

t.Run("SucceedsWithUnlimitedQuota", func(t *testing.T) {
f, throttle := setup(t)
throttle.Max24HourSend = -1
throttle.SentLast24Hours = math.MaxInt - 1

err := throttle.PauseBeforeNextSend(f.ctx)

assert.NilError(t, err)
assert.Equal(t, math.MaxInt, throttle.SentLast24Hours)
})

t.Run("ErrorsIfRefreshFails", func(t *testing.T) {
f, throttle := setup(t)
f.now = throttle.Updated.Add(f.refresh)
Expand Down

0 comments on commit a5b6ac8

Please sign in to comment.