Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reminder): decrement limit properly #53

Merged
merged 2 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
if err != nil {
sentry.GetHubFromContext(ctx).CaptureException(err)
}

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

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
Fixed Show fixed Hide fixed
}(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 @@
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)
}
}