Skip to content

Commit

Permalink
fix(reminder): decrement limit properly (#53)
Browse files Browse the repository at this point in the history
* fix(reminder): decrement limit properly

* fix(reminder): handle error for decrement
  • Loading branch information
aldy505 authored Dec 26, 2023
1 parent 3e99120 commit f7ce5b4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
12 changes: 11 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()

template := fmt.Sprintf(
"Hi %s! I'm reminding you to %s. Have a great day!",
strings.Join(reminder.Subject, ", "),
Expand All @@ -141,9 +146,14 @@ 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)
if err != nil {
sentry.GetHubFromContext(ctx).CaptureException(err)
}
}(c, reminder)

err = d.IncrementUserLimit(ctx, c.Sender().ID, reminderCount+1)
err = d.IncrementUserLimit(ctx, c.Sender().ID)
if err != nil {
sentry.GetHubFromContext(ctx).CaptureException(err)
}
Expand Down
45 changes: 43 additions & 2 deletions reminder/user_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,52 @@ 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)
}

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

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

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)
}

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)
}

if value == nil || string(value) == "" {
// Don't decrement anything that's empty
return nil
}

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

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 f7ce5b4

Please sign in to comment.