Skip to content

BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr

Adjust the api shipping rate limit

lotk6ce7491b2fac
diff --git a/core/internal/service/batch_mail/api_mail_send.go b/core/internal/service/batch_mail/api_mail_send.go
index 728d4f72..68d27330 100644
--- a/core/internal/service/batch_mail/api_mail_send.go
+++ b/core/internal/service/batch_mail/api_mail_send.go
@@ -46,21 +46,18 @@ type CacheData struct {
// Worker pool structure
type WorkerPool struct {
- workers int
- jobs chan ApiMailLog
- wg sync.WaitGroup
- cache *CacheData
- rateLimiter <-chan time.Time
+ workers int
+ jobs chan ApiMailLog
+ wg sync.WaitGroup
+ cache *CacheData
}
// Create a new worker pool
-func NewWorkerPool(workers int, cache *CacheData, ratePerMinute int) *WorkerPool {
- interval := time.Minute / time.Duration(ratePerMinute)
+func NewWorkerPool(workers int, cache *CacheData) *WorkerPool {
return &WorkerPool{
- workers: workers,
- jobs: make(chan ApiMailLog, BatchSize),
- cache: cache,
- rateLimiter: time.Tick(interval), // The interval between each email
+ workers: workers,
+ jobs: make(chan ApiMailLog, BatchSize),
+ cache: cache,
}
}
@@ -84,7 +81,6 @@ func (p *WorkerPool) worker(ctx context.Context) {
if !ok {
return
}
- <-p.rateLimiter // Rate control, no more than 200 packets per minute
p.processMail(ctx, log)
}
}
@@ -176,7 +172,7 @@ func ProcessApiMailQueueWithLock(ctx context.Context) {
func ProcessApiMailQueue(ctx context.Context) {
ctx, cancel := context.WithTimeout(ctx, QueryTimeout*time.Second)
defer cancel()
-
+ rateController := NewSimpleRateController(200)
// Process all pending emails in batche
offset := 0
for {
@@ -201,11 +197,15 @@ func ProcessApiMailQueue(ctx context.Context) {
break
}
- pool := NewWorkerPool(WorkerCount, cache, 200)
+ pool := NewWorkerPool(WorkerCount, cache)
pool.Start(ctx)
// Add emails to the worker pool
for _, log := range mailLogs {
+ // <-pool.rateLimiter
+ if err := rateController.Wait(ctx); err != nil {
+ return
+ }
select {
case <-ctx.Done():
return