Skip to content

Commit

Permalink
fix(reminder): decrement limit properly
Browse files Browse the repository at this point in the history
  • Loading branch information
aldy505 committed Dec 26, 2023
1 parent 3e99120 commit 5d03dc9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
9 changes: 8 additions & 1 deletion reminder/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ func (d *Dependency) Handler(ctx context.Context, c tb.Context) error {
go func(c tb.Context, reminder Reminder) {
time.Sleep(time.Until(reminder.Time))

ctx := sentry.SetHubOnContext(context.Background(), sentry.CurrentHub())
span := sentry.StartSpan(ctx, "reminder.trigger", sentry.WithTransactionName("Reminder Trigger"), sentry.WithTransactionSource(sentry.SourceTask))
defer span.Finish()
ctx = span.Context()

Check warning on line 139 in reminder/handler.go

View check run for this annotation

Codecov / codecov/patch

reminder/handler.go#L135-L139

Added lines #L135 - L139 were not covered by tests
template := fmt.Sprintf(
"Hi %s! I'm reminding you to %s. Have a great day!",
strings.Join(reminder.Subject, ", "),
Expand All @@ -141,9 +146,11 @@ func (d *Dependency) Handler(ctx context.Context, c tb.Context) error {
if err != nil {
sentry.GetHubFromContext(ctx).CaptureException(err)
}

err = d.DecrementUserLimit(ctx, c.Sender().ID)

Check warning

Code scanning / CodeQL

Useless assignment to local variable Warning

This definition of err is never used.

Check warning on line 150 in reminder/handler.go

View check run for this annotation

Codecov / codecov/patch

reminder/handler.go#L150

Added line #L150 was not covered by tests
}(c, reminder)

err = d.IncrementUserLimit(ctx, c.Sender().ID, reminderCount+1)
err = d.IncrementUserLimit(ctx, c.Sender().ID)

Check warning on line 153 in reminder/handler.go

View check run for this annotation

Codecov / codecov/patch

reminder/handler.go#L153

Added line #L153 was not covered by tests
if err != nil {
sentry.GetHubFromContext(ctx).CaptureException(err)
}
Expand Down
44 changes: 42 additions & 2 deletions reminder/user_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,51 @@ func (d *Dependency) CheckUserLimit(ctx context.Context, id int64) (n int, err e
return strconv.Atoi(string(value))
}

func (d *Dependency) IncrementUserLimit(ctx context.Context, id int64, value int) error {
func (d *Dependency) IncrementUserLimit(ctx context.Context, id int64) error {
span := sentry.StartSpan(ctx, "reminder.increment_user_limit")
defer span.Finish()

err := d.memory.Set(fmt.Sprintf("reminder:user_limit:%d", id), []byte(strconv.Itoa(value)))
value, err := d.memory.Get(fmt.Sprintf("reminder:user_limit:%d", id))
if err != nil && !errors.Is(err, bigcache.ErrEntryNotFound) {
return fmt.Errorf("acquiring value from memory: %w", err)
}

Check warning on line 36 in reminder/user_limiter.go

View check run for this annotation

Codecov / codecov/patch

reminder/user_limiter.go#L35-L36

Added lines #L35 - L36 were not covered by tests

if value == nil || string(value) == "" {
value = []byte("0")
}

i, err := strconv.Atoi(string(value))
if err != nil {
return fmt.Errorf("invalid value: %s", value)
}

Check warning on line 45 in reminder/user_limiter.go

View check run for this annotation

Codecov / codecov/patch

reminder/user_limiter.go#L44-L45

Added lines #L44 - L45 were not covered by tests

err = d.memory.Set(fmt.Sprintf("reminder:user_limit:%d", id), []byte(strconv.Itoa(i+1)))
if err != nil {
return fmt.Errorf("setting value to memory: %w", err)
}

Check warning on line 50 in reminder/user_limiter.go

View check run for this annotation

Codecov / codecov/patch

reminder/user_limiter.go#L49-L50

Added lines #L49 - L50 were not covered by tests

return nil
}

func (d *Dependency) DecrementUserLimit(ctx context.Context, id int64) error {
span := sentry.StartSpan(ctx, "reminder.decrement_user_limit")
defer span.Finish()

value, err := d.memory.Get(fmt.Sprintf("reminder:user_limit:%d", id))
if err != nil && !errors.Is(err, bigcache.ErrEntryNotFound) {
return fmt.Errorf("acquiring value from memory: %w", err)
}

Check warning on line 62 in reminder/user_limiter.go

View check run for this annotation

Codecov / codecov/patch

reminder/user_limiter.go#L61-L62

Added lines #L61 - L62 were not covered by tests

if value == nil || string(value) == "" {
value = []byte("0")
}

Check warning on line 66 in reminder/user_limiter.go

View check run for this annotation

Codecov / codecov/patch

reminder/user_limiter.go#L65-L66

Added lines #L65 - L66 were not covered by tests

i, err := strconv.Atoi(string(value))
if err != nil {
return fmt.Errorf("invalid value: %s", value)
}

Check warning on line 71 in reminder/user_limiter.go

View check run for this annotation

Codecov / codecov/patch

reminder/user_limiter.go#L70-L71

Added lines #L70 - L71 were not covered by tests

err = d.memory.Set(fmt.Sprintf("reminder:user_limit:%d", id), []byte(strconv.Itoa(i-1)))
if err != nil {
return fmt.Errorf("setting value to memory: %w", err)
}
Expand Down
16 changes: 15 additions & 1 deletion reminder/user_limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestUserLimit(t *testing.T) {
t.Errorf("expecting userLimit to be 0, instead got %d", userLimit)
}

err = dependency.IncrementUserLimit(ctx, 123, 1)
err = dependency.IncrementUserLimit(ctx, 123)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
Expand All @@ -48,4 +48,18 @@ func TestUserLimit(t *testing.T) {
if userLimit2 != 1 {
t.Errorf("expecting userLimit2 to be 1, instead got %d", userLimit2)
}

err = dependency.DecrementUserLimit(ctx, 123)
if err != nil {
t.Errorf("unexpected error: %v", err)
}

userLimit3, err := dependency.CheckUserLimit(ctx, 123)
if err != nil {
t.Errorf("unexpected error: %s", err.Error())
}

if userLimit3 != 0 {
t.Errorf("expecting userLimit3 to be 0, instead got %d", userLimit2)
}
}

0 comments on commit 5d03dc9

Please sign in to comment.