more refactoring
This commit is contained in:
@@ -14,7 +14,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type EmailClient interface {
|
||||
type Client interface {
|
||||
SendEmail(Email) error
|
||||
}
|
||||
|
||||
@@ -103,22 +103,22 @@ func (s *SESClient) SendEmail(email Email) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// EmailQueue represents the email queue with rate limiting
|
||||
type EmailQueue struct {
|
||||
// Queue represents the email queue with rate limiting
|
||||
type Queue struct {
|
||||
stop bool
|
||||
emailQueue chan Email // Email queue
|
||||
rateLimit <-chan time.Time // Rate limiter
|
||||
client EmailClient // SES client to send emails
|
||||
client Client // SES client to send emails
|
||||
wg sync.WaitGroup // To wait for all emails to be processed
|
||||
FailedSendCount int
|
||||
}
|
||||
|
||||
// NewEmailQueue creates a new rate-limited email queue
|
||||
func NewEmailQueue(bufferSize int, emailsPerSecond int, client EmailClient) *EmailQueue {
|
||||
func NewEmailQueue(bufferSize int, emailsPerSecond int, client Client) *Queue {
|
||||
// Create a ticker that ticks every second to limit the rate of sending emails
|
||||
rateLimit := time.Tick(time.Second / time.Duration(emailsPerSecond))
|
||||
|
||||
return &EmailQueue{
|
||||
return &Queue{
|
||||
stop: false,
|
||||
emailQueue: make(chan Email, bufferSize),
|
||||
rateLimit: rateLimit,
|
||||
@@ -128,7 +128,7 @@ func NewEmailQueue(bufferSize int, emailsPerSecond int, client EmailClient) *Ema
|
||||
}
|
||||
|
||||
// AddEmail queues a new email to be sent
|
||||
func (q *EmailQueue) AddEmail(email Email) {
|
||||
func (q *Queue) AddEmail(email Email) {
|
||||
if q.stop {
|
||||
log.Printf("email %s with subject %s not add. Stopping queue", email.Recipient, email.Subject)
|
||||
return
|
||||
@@ -138,7 +138,7 @@ func (q *EmailQueue) AddEmail(email Email) {
|
||||
}
|
||||
|
||||
// Start begins processing the email queue with rate limiting
|
||||
func (q *EmailQueue) Start() {
|
||||
func (q *Queue) Start() {
|
||||
q.stop = false
|
||||
// Worker goroutine that processes emails from the queue
|
||||
go func() {
|
||||
@@ -151,7 +151,7 @@ func (q *EmailQueue) Start() {
|
||||
}
|
||||
|
||||
// sendEmail sends an email using the SES client
|
||||
func (q *EmailQueue) sendEmail(email Email) {
|
||||
func (q *Queue) sendEmail(email Email) {
|
||||
if err := q.client.SendEmail(email); err != nil {
|
||||
q.FailedSendCount += 1
|
||||
log.Printf("Failed to send email to %s: %v\n", email.Recipient, err)
|
||||
@@ -159,7 +159,7 @@ func (q *EmailQueue) sendEmail(email Email) {
|
||||
}
|
||||
|
||||
// Stop stops the queue after all emails have been processed
|
||||
func (q *EmailQueue) Stop() {
|
||||
func (q *Queue) Stop() {
|
||||
q.stop = true
|
||||
// Wait for all emails to be processed
|
||||
q.wg.Wait()
|
||||
|
||||
Reference in New Issue
Block a user