Files
warmbly/internal/app/worker/service.go
T
Matthew Meszaros 73cabf6f5e worker: WorkerHealth event, capacity view, smarter SelectSharedWorker
Workers emit a WorkerHealth event every 30s with assigned mailbox count,
IMAP IDLE connections, memory, goroutines, and rolling 1h send/bounce/
complaint/auth-error/rate-limit counters. Consumer writes them to
worker_health_samples.

Schema additions on workers: egress_kind (cold_smtp / oauth_api /
warmup_only), health_state (healthy / watch / throttled / quarantined /
blocked), load_score (weighted utilization).

worker_capacity_view aggregates the latest hour of samples into a
per-worker capacity row used by the assignment loop. Effective capacity
= base_ceiling(egress_kind) × health_multiplier × age_ramp_multiplier
so a fresh worker earns its way up to base capacity over 72h, and a
worker with rising bounces or complaints automatically gets less load.

MailboxWeight returns 1.0 for cold_smtp, 0.05 for Gmail/Graph API
(worker IP doesn't matter), 0.4 for warmup-only. AssignWorkerToEmail
resolves the mailbox's weight and SelectSharedWorker filters by
headroom + sorts by utilization, so a 200-mailbox OAuth worker and a
16-mailbox cold worker balance fairly.

UnassignWorkerFromEmail refunds the load_score symmetrically.
2026-05-27 15:56:01 +00:00

61 lines
1.7 KiB
Go

package worker
import (
"context"
"sync"
"github.com/warmbly/warmbly/internal/app/cipher"
"github.com/warmbly/warmbly/internal/app/worker/mailmanager"
"github.com/warmbly/warmbly/internal/infrastructure/cache"
"github.com/warmbly/warmbly/internal/infrastructure/codec"
"github.com/warmbly/warmbly/internal/infrastructure/eventbus"
"github.com/warmbly/warmbly/internal/infrastructure/storage"
"github.com/warmbly/warmbly/internal/models"
"github.com/warmbly/warmbly/internal/repository"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type WorkerService struct {
ID string
CipherService cipher.CipherService
Bus eventbus.EventBus
Codec codec.Codec
QueueURL string
Cache *cache.Cache
Storage storage.Store
EmailMessageMapRepository repository.EmailMessageMapRepository
mailManager *mailmanager.MailManager
// HealthCounters tracks the per-window send-side telemetry the worker
// reports via JobEventTypeWorkerHealth. Lazily initialised by
// ensureCounters so accessors are safe to call before RunHealth has
// started.
HealthCounters *HealthCounters
healthOnce sync.Once
errorEvents chan zapcore.Entry
logger *zap.Logger
eventHandlers map[models.WorkerEventType]func(ctx context.Context, body any) error
}
func (s *WorkerService) Init() error {
s.errorEvents = make(chan zapcore.Entry)
var err error
s.logger, err = NewLoggerWithHandler(s.HandleError)
if err != nil {
return err
}
s.mailManager = mailmanager.NewMailManager(
s.Produce,
s.Cache,
s.Storage,
s.EmailMessageMapRepository,
s.CipherService,
)
return nil
}