Skip to content

Commit

Permalink
linter
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianchaparroa committed Feb 26, 2024
1 parent 81a687f commit 539c703
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
12 changes: 6 additions & 6 deletions internal/usecase/base_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type BaseHandler struct {
MaxPerUnit int // Maximum number of notifications allowed per unit (e.g., 2)
Unit time.Duration // Unit of time for the rate limit (e.g., time.Minute)
lastSentTimes map[string]time.Time // Maps recipient emails to the last time sent
mutex sync.Mutex // Protects lastSentTimes from concurrent access
next NotificationRateLimitHandler // Next handler in the chain
mutex *sync.Mutex // Protects lastSentTimes from concurrent access
Next NotificationRateLimitHandler // Next handler in the chain
}

func (h *BaseHandler) Validate(notification *entity.Notification) error {
Expand All @@ -45,16 +45,16 @@ func BuildRateLimitChain() NotificationRateLimitHandler {
// TODO: abstract the storage to be scalable
// i.e: Redis, Memcache...(ephemeral storage with high response)
lastSentTimes := make(map[string]time.Time)
m := sync.Mutex{}
m := &sync.Mutex{}

sh := NewStatusHandler(lastSentTimes, rules[entity.StatusNotificationType], m)
nh := NewNewsHandler(lastSentTimes, rules[entity.NewsNotificationType], m)
mh := NewMarketingHandler(lastSentTimes, rules[entity.MarketingNotificationType], m)
uh := &UnknownHandler{}

sh.next = nh
nh.next = mh
mh.next = uh // the UnknownHandler should be the last one
sh.Next = nh
nh.Next = mh
mh.Next = uh // the UnknownHandler should be the last one

return sh
}
1 change: 1 addition & 0 deletions internal/usecase/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package usecase defines the service business logic
package usecase

import (
Expand Down
18 changes: 9 additions & 9 deletions internal/usecase/limit_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type StatusHandler struct {
*BaseHandler
}

func NewStatusHandler(lastSentTimes map[string]time.Time, r RateLimitRule, m sync.Mutex) *StatusHandler {
func NewStatusHandler(lastSentTimes map[string]time.Time, r RateLimitRule, m *sync.Mutex) *StatusHandler {
return &StatusHandler{&BaseHandler{
MaxPerUnit: r.MaxPerUnit,
Unit: r.Unit,
Expand All @@ -23,21 +23,21 @@ func NewStatusHandler(lastSentTimes map[string]time.Time, r RateLimitRule, m syn

func (h *StatusHandler) Handle(n *entity.Notification) error {
if entity.StatusNotificationType != n.Type {
return h.next.Handle(n) // Pass to next handler
return h.Next.Handle(n) // Pass to Next handler
}
return h.Validate(n)
}

func (h *StatusHandler) SetNext(next NotificationRateLimitHandler) {
h.next = next
h.Next = next
}

// MarketingHandler handles rate limits for the "marketing" notification type.
type MarketingHandler struct {
*BaseHandler
}

func NewMarketingHandler(lastSentTimes map[string]time.Time, r RateLimitRule, m sync.Mutex) *MarketingHandler {
func NewMarketingHandler(lastSentTimes map[string]time.Time, r RateLimitRule, m *sync.Mutex) *MarketingHandler {
return &MarketingHandler{&BaseHandler{
MaxPerUnit: r.MaxPerUnit,
Unit: r.Unit,
Expand All @@ -48,21 +48,21 @@ func NewMarketingHandler(lastSentTimes map[string]time.Time, r RateLimitRule, m

func (h *MarketingHandler) Handle(n *entity.Notification) error {
if entity.MarketingNotificationType != n.Type {
return h.next.Handle(n) // Pass to next handler
return h.Next.Handle(n) // Pass to Next handler
}
return h.Validate(n)
}

func (h *MarketingHandler) SetNext(next NotificationRateLimitHandler) {
h.next = next
h.Next = next
}

// NewsHandler handles rate limits for the "news" notification type.
type NewsHandler struct {
*BaseHandler
}

func NewNewsHandler(lastSentTimes map[string]time.Time, r RateLimitRule, m sync.Mutex) *NewsHandler {
func NewNewsHandler(lastSentTimes map[string]time.Time, r RateLimitRule, m *sync.Mutex) *NewsHandler {
return &NewsHandler{&BaseHandler{
MaxPerUnit: r.MaxPerUnit,
Unit: r.Unit,
Expand All @@ -73,13 +73,13 @@ func NewNewsHandler(lastSentTimes map[string]time.Time, r RateLimitRule, m sync.

func (h *NewsHandler) Handle(n *entity.Notification) error {
if entity.NewsNotificationType != n.Type {
return h.next.Handle(n) // Pass to next handler
return h.Next.Handle(n) // Pass to Next handler
}
return h.Validate(n)
}

func (h *NewsHandler) SetNext(next NotificationRateLimitHandler) {
h.next = next
h.Next = next
}

type UnknownHandler struct {
Expand Down
4 changes: 1 addition & 3 deletions internal/usecase/limit_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ type limitHandlerSuite struct {
suite.Suite
}

func (s limitHandlerSuite) setupTest() {}

func TestLimitHandlerSuite(t *testing.T) {
suite.Run(t, new(limitHandlerSuite))
}

func (s limitHandlerSuite) TestChainOfResponsibility() {
func (s *limitHandlerSuite) TestChainOfResponsibility() {
handler := BuildRateLimitChain()

// Sending notifications (some will be rejected due to rate limits)
Expand Down

0 comments on commit 539c703

Please sign in to comment.