High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.
JavaScript campaigncampaign-managementemail-marketingemail-subscriptionlistmonkmailing-listnewsletternewsletter-managementnewsletter-softwareself-hostedsms-gatewaysmtptransactional-emails
Add throttling to opt-in confirmation e-mails.
Kailash Nadh5007410ce231
diff --git a/.playwright-mcp/console-2026-04-30T05-12-01-830Z.log b/.playwright-mcp/console-2026-04-30T05-12-01-830Z.logdeleted file mode 100644index b7390366..00000000--- a/.playwright-mcp/console-2026-04-30T05-12-01-830Z.log+++ /dev/null@@ -1,3 +0,0 @@-[ 137466ms] [ERROR] Failed to load resource: net::ERR_CONNECTION_REFUSED @ http://localhost:9000/api/events?type=error:0-[ 140470ms] [ERROR] Failed to load resource: net::ERR_CONNECTION_REFUSED @ http://localhost:9000/api/events?type=error:0-[ 143474ms] [ERROR] Failed to load resource: net::ERR_CONNECTION_REFUSED @ http://localhost:9000/api/events?type=error:0diff --git a/internal/core/core.go b/internal/core/core.goindex 32a67e3b..1641b3f5 100644--- a/internal/core/core.go+++ b/internal/core/core.go@@ -14,6 +14,7 @@ import ("github.com/jmoiron/sqlx""github.com/knadh/listmonk/internal/i18n"+ "github.com/knadh/listmonk/internal/throttle""github.com/knadh/listmonk/models""github.com/labstack/echo/v4""github.com/lib/pq"@@ -30,11 +31,12 @@ const (type Core struct {h *Hooks- consts Constants- i18n *i18n.I18n- db *sqlx.DB- q *models.Queries- log *log.Logger+ consts Constants+ optinThrottle *throttle.Throttle+ i18n *i18n.I18n+ db *sqlx.DB+ q *models.Queries+ log *log.Logger}// Constants represents constant config.@@ -78,12 +80,13 @@ var (// New returns a new instance of the core.func New(o *Opt, h *Hooks) *Core {return &Core{- h: h,- consts: o.Constants,- i18n: o.I18n,- db: o.DB,- q: o.Queries,- log: o.Log,+ h: h,+ consts: o.Constants,+ optinThrottle: throttle.New(optinThrottleTTL, optinThrottleCap),+ i18n: o.I18n,+ db: o.DB,+ q: o.Queries,+ log: o.Log,}}diff --git a/internal/core/subscribers.go b/internal/core/subscribers.goindex d7665e0b..70f404fc 100644--- a/internal/core/subscribers.go+++ b/internal/core/subscribers.go@@ -6,7 +6,9 @@ import ("encoding/json""fmt""net/http"+ "slices""strings"+ "time""github.com/gofrs/uuid/v5""github.com/jmoiron/sqlx"@@ -17,6 +19,11 @@ import ("github.com/lib/pq")+const (+ optinThrottleTTL = 10 * time.Minute+ optinThrottleCap = 5000+)+var (allowedSubQueryTables = map[string]struct{}{"subscribers": {},@@ -383,7 +390,7 @@ func (c *Core) InsertSubscriber(sub models.Subscriber, listIDs []int, listUUIDshasOptin := falseif !preconfirm && c.consts.SendOptinConfirmation {// Send a confirmation e-mail (if there are any double opt-in lists).- num, err := c.h.SendOptinConfirmation(out, listIDs)+ num, err := c.sendOptinConfirmation(out, listIDs, assertOptin)if assertOptin && err != nil {return out, hasOptin, err}@@ -474,7 +481,7 @@ func (c *Core) UpdateSubscriberWithLists(id int, sub models.Subscriber, listIDshasOptin := falseif !preconfirm && c.consts.SendOptinConfirmation {// Send a confirmation e-mail (if there are any double opt-in lists).- num, err := c.h.SendOptinConfirmation(out, listIDs)+ num, err := c.sendOptinConfirmation(out, listIDs, assertOptin)if assertOptin && err != nil {return out, hasOptin, err}@@ -770,3 +777,29 @@ func (c *Core) execSubQueryTpl(searchStr, queryExp, baseQueryTpl string, listIDsreturn nil}++// sendOptinConfirmation throttles opt-in mails sent on public subscriptions.+func (c *Core) sendOptinConfirmation(sub models.Subscriber, listIDs []int, public bool) (int, error) {+ if public {+ var pending []int+ for _, l := range sub.ParsedLists() {+ if l.Optin == models.ListOptinDouble && l.SubscriptionStatus == models.SubscriptionStatusUnconfirmed {+ pending = append(pending, l.ID)+ }+ }+ if len(pending) == 0 {+ return 0, nil+ }++ // Make the throttle key from the e-mail + the normalized list of pending list IDs+ // so that new subscriptions don't get throttled.+ slices.Sort(pending)+ key := fmt.Sprintf("%s:%v", strings.ToLower(strings.TrimSpace(sub.Email)), pending)+ if !c.optinThrottle.Insert(key, true) {+ return len(pending), nil+ }+ listIDs = pending+ }++ return c.h.SendOptinConfirmation(sub, listIDs)+}diff --git a/internal/throttle/throttle.go b/internal/throttle/throttle.gonew file mode 100644index 00000000..0fd7ec78--- /dev/null+++ b/internal/throttle/throttle.go@@ -0,0 +1,66 @@+// Package throttle provides a simple in-memory key store to prevent actions from+// being performed frequently.+package throttle++import (+ "sync"+ "time"+)++// Throttle tracks a set of keys in-memory until they expire at the given TTL.+type Throttle struct {+ mu sync.Mutex+ ttl time.Duration+ maxLen int+ expires map[string]time.Time+ cleanAt time.Time+}++// New creates a throttle store that stores max cap items.+func New(ttl time.Duration, cap int) *Throttle {+ if ttl <= 0 {+ ttl = time.Minute * 10+ }+ if cap <= 0 {+ cap = 5000+ }++ return &Throttle{+ ttl: ttl,+ maxLen: cap,+ expires: make(map[string]time.Time),+ }+}++// Insert reserves the given key for the configured TTL. It returns true if the+// key isn't currently throttled. If exceedCap is true, new keys are allowed+// without being stored when the store is full.+func (c *Throttle) Insert(key string, exceedCap bool) bool {+ c.mu.Lock()+ defer c.mu.Unlock()++ // Clean up expired keys at every ttl.+ now := time.Now()+ if !now.Before(c.cleanAt) {+ for key, expiry := range c.expires {+ if !now.Before(expiry) {+ delete(c.expires, key)+ }+ }++ c.cleanAt = now.Add(min(c.ttl, time.Minute))+ }++ // Key exists and is still within the TTL.+ if expiry, ok := c.expires[key]; ok {+ if now.Before(expiry) {+ return false+ }+ } else if len(c.expires) >= c.maxLen {+ return exceedCap+ }+ c.expires[key] = now.Add(c.ttl)++ // New unthrottled key.+ return true+}